sheet-edit 公式自动重算(评审确认版):写操作产物交付前经 LibreOffice 同格式转换强制求值写回缓存——openpyxl 产物无缓存、LO 保存必须求值,实测重算必达且公式文本保留,产物可直接进预览/转PDF/data_only 读取,不再依赖用户手动打开;_libreoffice 全链路加 -env:UserInstallation 隔离 profile(实测无隔离时 read-only rootfs 下 rc=77 必崩,顺带根治并发转换单实例锁冲突);重算作为可用性增强置于正确性门禁之后且需再过一次自检,任何失败自动回退交付已验证的未重算版并标 recalc=failed,结果加 recalc 字段(done/skipped/failed);否决评审的宏 calculateAll 方案(实测连 OOXMLRecalcMode 都不用设);csv 跳过;SKILL.md 边界同步改口径

This commit is contained in:
2026-09-28 10:23:42 +08:00
parent d4f83b0694
commit afea58fe17
3 changed files with 78 additions and 3 deletions
+30
View File
@@ -1,5 +1,6 @@
"""sheet-edit 的各个 op 实现。"""
import datetime
import shutil
import tempfile
from pathlib import Path
@@ -144,10 +145,12 @@ def apply(args):
except Exception:
_drop(out_path)
raise
recalc_note = _try_recalc(out_path, Path(td), lambda p: _verify_apply(p, before, plan))
return {
'ok': True,
'output': out_path.name,
'recalc': recalc_note,
'changes': [
{
'sheet': it['sheet'],
@@ -304,10 +307,13 @@ def insert_rows(args):
except Exception:
_drop(out_path)
raise
recalc_note = _try_recalc(out_path, Path(td),
lambda p: _verify_struct(p, before, sheet, 'row', insert_at, count, False, expected_new))
return {
'ok': True,
'output': out_path.name,
'recalc': recalc_note,
'sheet': sheet,
'inserted_at': insert_at,
'count': count,
@@ -345,10 +351,13 @@ def delete_rows(args):
except Exception:
_drop(out_path)
raise
recalc_note = _try_recalc(out_path, Path(td),
lambda p: _verify_struct(p, before, sheet, 'row', start, count, True, {}))
return {
'ok': True,
'output': out_path.name,
'recalc': recalc_note,
'sheet': sheet,
'deleted_at': start,
'count': count,
@@ -400,10 +409,13 @@ def insert_cols(args):
except Exception:
_drop(out_path)
raise
recalc_note = _try_recalc(out_path, Path(td),
lambda p: _verify_struct(p, before, sheet, 'col', insert_at, count, False, expected_new))
return {
'ok': True,
'output': out_path.name,
'recalc': recalc_note,
'sheet': sheet,
'inserted_at': insert_at,
'count': count,
@@ -443,10 +455,13 @@ def delete_cols(args):
except Exception:
_drop(out_path)
raise
recalc_note = _try_recalc(out_path, Path(td),
lambda p: _verify_struct(p, before, sheet, 'col', start, count, True, {}))
return {
'ok': True,
'output': out_path.name,
'recalc': recalc_note,
'sheet': sheet,
'deleted_at': start,
'count': count,
@@ -533,6 +548,21 @@ def _drop(path: Path):
pass
def _try_recalc(out_path, work_dir, verify):
"""正确性门禁通过后尝试 LO 重算;重算产物同样过一遍 verify 才能覆盖交付,
否则保持未重算版(仍带 fullCalcOnLoad 标记,Excel/WPS 打开会自动算)。
重算是可用性增强不是正确性条件,故失败只降级不报错。返回状态供结果 JSON。"""
cand, err = convert.recalc(out_path, work_dir)
if cand is None:
return 'skipped' if '无需重算' in (err or '') else f'failed: {err}'
try:
verify(cand)
except Exception as e:
return f'failed: 重算产物未过自检,交付未重算版({str(e)[:120]})'
shutil.copy2(cand, out_path)
return 'done'
def _adjust_formulas(wb, target_sheet, axis, at, count, delete):
"""全工作簿调整公式:含「其他表引用目标表」的跨表公式;目标表引用他表的不动(同 Excel)。