sheet-edit 技能评审修复(P0/P1 全绿):公式调整弃裸正则、改用官方 Tokenizer 按 subtype=RANGE 逐 token 重写——LOG10/ATAN2 函数名、"B2" 字符串、本表引用他表不再被错改,他表→目标表跨表引用获得调整(同 Excel),区间删除按 Excel 语义收缩而非裸 #REF!,已失效 #REF! 透传不炸;合并区域随插删移动(修 mv 误同时移行移列致 D4:D5→E5:E6 吞数据的笔误;跨界删除拒绝)、合并区非锚点写入显式报错;insert/delete 补结构化自检(逐格坐标映射+公式预期+尺寸+新带 fill 校验),校验失败自动删产物兑现「不交付」;inspect 日期/ArrayFormula JSON 化(旧版含日期表直接崩)、formulas 上限 500+truncated、返回 merged;CSV 不再 coerce(007/1.10 保真),value_eq 数字↔数字文本互认;openpyxl 丢公式缓存值的对策=保存置 fullCalcOnLoad;output 文件名消毒;SKILL.md 边界/常见错误同步重写

This commit is contained in:
2026-09-28 09:57:50 +08:00
parent 4544dba705
commit 17d6292dfa
7 changed files with 1315 additions and 0 deletions
+75
View File
@@ -0,0 +1,75 @@
"""内容定位:用列名 + 值找目标行/列。"""
from . import UserError
def is_formula(v) -> bool:
return isinstance(v, str) and v.startswith('=')
def value_eq(a, b) -> bool:
if a is None and b is None:
return True
if isinstance(a, (int, float)) and isinstance(b, (int, float)):
return abs(float(a) - float(b)) < 1e-9
# 数字↔数字文本(CSV 往返后数值变文本,定位比较要能对上)
if isinstance(a, str) != isinstance(b, str):
try:
return abs(float(a) - float(b)) < 1e-9
except (TypeError, ValueError):
return False
return a == b
def build_col_map(ws, header_row: int) -> dict:
"""从表头行构建 {列名: 列号}。"""
col_map = {}
for c in range(1, ws.max_column + 1):
v = ws.cell(header_row, c).value
if v is None:
continue
name = str(v).strip()
if not name:
continue
if name in col_map:
raise UserError(f"表头行 {header_row} 存在重复列名: '{name}'")
col_map[name] = c
if not col_map:
raise UserError(f"表头行 {header_row} 为空,无法识别列名")
return col_map
def find_row(ws, col_map: dict, match: dict, header_row: int) -> int:
"""用 match(列名 → 值)找唯一匹配的行号。"""
if not isinstance(match, dict) or not match:
raise UserError("match 必须是非空对象")
for k in match:
if k not in col_map:
raise UserError(
f"表头无此列: '{k}'(可用: {', '.join(col_map.keys())})"
)
hits = []
for r in range(header_row + 1, ws.max_row + 1):
ok = True
for col_name, want in match.items():
v = ws.cell(r, col_map[col_name]).value
if not value_eq(v, want):
ok = False
break
if ok:
hits.append(r)
if not hits:
raise UserError(f"找不到匹配行: {match}")
if len(hits) > 1:
raise UserError(f"匹配到 {len(hits)} 行,需增加条件: {match}")
return hits[0]
def find_col(col_map: dict, name: str) -> int:
if name not in col_map:
raise UserError(
f"表头无此列: '{name}'(可用: {', '.join(col_map.keys())})"
)
return col_map[name]