From afea58fe17d4cbe86b851595d2cc2be954d638e7 Mon Sep 17 00:00:00 2001 From: kicer Date: Mon, 28 Sep 2026 10:23:42 +0800 Subject: [PATCH] =?UTF-8?q?sheet-edit=20=E5=85=AC=E5=BC=8F=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E9=87=8D=E7=AE=97=EF=BC=88=E8=AF=84=E5=AE=A1=E7=A1=AE?= =?UTF-8?q?=E8=AE=A4=E7=89=88=EF=BC=89=EF=BC=9A=E5=86=99=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E4=BA=A7=E7=89=A9=E4=BA=A4=E4=BB=98=E5=89=8D=E7=BB=8F=20LibreO?= =?UTF-8?q?ffice=20=E5=90=8C=E6=A0=BC=E5=BC=8F=E8=BD=AC=E6=8D=A2=E5=BC=BA?= =?UTF-8?q?=E5=88=B6=E6=B1=82=E5=80=BC=E5=86=99=E5=9B=9E=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E2=80=94=E2=80=94openpyxl=20=E4=BA=A7=E7=89=A9=E6=97=A0?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E3=80=81LO=20=E4=BF=9D=E5=AD=98=E5=BF=85?= =?UTF-8?q?=E9=A1=BB=E6=B1=82=E5=80=BC=EF=BC=8C=E5=AE=9E=E6=B5=8B=E9=87=8D?= =?UTF-8?q?=E7=AE=97=E5=BF=85=E8=BE=BE=E4=B8=94=E5=85=AC=E5=BC=8F=E6=96=87?= =?UTF-8?q?=E6=9C=AC=E4=BF=9D=E7=95=99=EF=BC=8C=E4=BA=A7=E7=89=A9=E5=8F=AF?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E8=BF=9B=E9=A2=84=E8=A7=88/=E8=BD=ACPDF/data?= =?UTF-8?q?=5Fonly=20=E8=AF=BB=E5=8F=96=EF=BC=8C=E4=B8=8D=E5=86=8D?= =?UTF-8?q?=E4=BE=9D=E8=B5=96=E7=94=A8=E6=88=B7=E6=89=8B=E5=8A=A8=E6=89=93?= =?UTF-8?q?=E5=BC=80=EF=BC=9B=5Flibreoffice=20=E5=85=A8=E9=93=BE=E8=B7=AF?= =?UTF-8?q?=E5=8A=A0=20-env:UserInstallation=20=E9=9A=94=E7=A6=BB=20profil?= =?UTF-8?q?e=EF=BC=88=E5=AE=9E=E6=B5=8B=E6=97=A0=E9=9A=94=E7=A6=BB?= =?UTF-8?q?=E6=97=B6=20read-only=20rootfs=20=E4=B8=8B=20rc=3D77=20?= =?UTF-8?q?=E5=BF=85=E5=B4=A9=EF=BC=8C=E9=A1=BA=E5=B8=A6=E6=A0=B9=E6=B2=BB?= =?UTF-8?q?=E5=B9=B6=E5=8F=91=E8=BD=AC=E6=8D=A2=E5=8D=95=E5=AE=9E=E4=BE=8B?= =?UTF-8?q?=E9=94=81=E5=86=B2=E7=AA=81=EF=BC=89=EF=BC=9B=E9=87=8D=E7=AE=97?= =?UTF-8?q?=E4=BD=9C=E4=B8=BA=E5=8F=AF=E7=94=A8=E6=80=A7=E5=A2=9E=E5=BC=BA?= =?UTF-8?q?=E7=BD=AE=E4=BA=8E=E6=AD=A3=E7=A1=AE=E6=80=A7=E9=97=A8=E7=A6=81?= =?UTF-8?q?=E4=B9=8B=E5=90=8E=E4=B8=94=E9=9C=80=E5=86=8D=E8=BF=87=E4=B8=80?= =?UTF-8?q?=E6=AC=A1=E8=87=AA=E6=A3=80=EF=BC=8C=E4=BB=BB=E4=BD=95=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E8=87=AA=E5=8A=A8=E5=9B=9E=E9=80=80=E4=BA=A4=E4=BB=98?= =?UTF-8?q?=E5=B7=B2=E9=AA=8C=E8=AF=81=E7=9A=84=E6=9C=AA=E9=87=8D=E7=AE=97?= =?UTF-8?q?=E7=89=88=E5=B9=B6=E6=A0=87=20recalc=3Dfailed=EF=BC=8C=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=E5=8A=A0=20recalc=20=E5=AD=97=E6=AE=B5(done/skipped/f?= =?UTF-8?q?ailed)=EF=BC=9B=E5=90=A6=E5=86=B3=E8=AF=84=E5=AE=A1=E7=9A=84?= =?UTF-8?q?=E5=AE=8F=20calculateAll=20=E6=96=B9=E6=A1=88=EF=BC=88=E5=AE=9E?= =?UTF-8?q?=E6=B5=8B=E8=BF=9E=20OOXMLRecalcMode=20=E9=83=BD=E4=B8=8D?= =?UTF-8?q?=E7=94=A8=E8=AE=BE=EF=BC=89=EF=BC=9Bcsv=20=E8=B7=B3=E8=BF=87?= =?UTF-8?q?=EF=BC=9BSKILL.md=20=E8=BE=B9=E7=95=8C=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E6=94=B9=E5=8F=A3=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- skills/sheet-edit/SKILL.md | 5 ++-- skills/sheet-edit/lib/convert.py | 46 +++++++++++++++++++++++++++++++- skills/sheet-edit/lib/ops.py | 30 +++++++++++++++++++++ 3 files changed, 78 insertions(+), 3 deletions(-) diff --git a/skills/sheet-edit/SKILL.md b/skills/sheet-edit/SKILL.md index 9154f23..ef6509b 100644 --- a/skills/sheet-edit/SKILL.md +++ b/skills/sheet-edit/SKILL.md @@ -210,8 +210,9 @@ description: 结构化修改表格文件。内容定位(列名 + 值),最 - 公式调整范围:目标表内引用、他表→目标表的跨表引用都会改;目标表→他表的引用不动(同 Excel)。 被删区域端点的区间按 Excel 语义收缩;命名范围(Tax2023 之类)不改写,引用它们的结果可能错位,需人工核对。 - 删除跨越合并区域边界的行/列会被拒绝(行为无法推断),改为整块删或先拆合并。 -- 公式缓存值不由本工具计算:交付文件带「打开时全量重算」标记,Excel/WPS 打开会自动算; - 若产物要进纯预览链路(截图/转 PDF/脚本读数),先用 LibreOffice 打开另存一次。 +- 产物交付前已自动重算:LibreOffice 对 xlsx/xls/ods 求值并把结果写入公式缓存,可直接进预览/转 PDF/脚本读取; + 重算失败时自动回退交付「未重算版」并在结果 `recalc` 字段标注 `failed:...`(未重算版仍带「打开时全量重算」标记, + Excel/WPS 打开结果一样正确);结果里看到 recalc=failed 时简短告知用户一句即可,不要重试。 - CSV 单元格一律按文本保留(「007」「1.10」不会变成 7/1.1);数字与数字文本在定位比较时互认。 - 多级表头、复杂合并:可能定位失败,需手工处理。 diff --git a/skills/sheet-edit/lib/convert.py b/skills/sheet-edit/lib/convert.py index c5a8221..6f64a63 100644 --- a/skills/sheet-edit/lib/convert.py +++ b/skills/sheet-edit/lib/convert.py @@ -76,11 +76,21 @@ def from_workbook(xlsx: Path, dst: Path, original_ext: str): # ---------- 内部 ---------- +def _lo_profile(out_dir: Path) -> Path: + """LO 隔离 profile:执行容器根文件系统只读,默认 ~/.config 写入会直接 rc=77 崩(实测); + 每次调用独立 profile 同时解决并发转换的 LO 单实例锁冲突。""" + prof = out_dir / 'loprofile' + (prof / 'user').mkdir(parents=True, exist_ok=True) + return prof + + def _libreoffice(src: Path, out_dir: Path, target_ext: str) -> Path: out_dir.mkdir(parents=True, exist_ok=True) target = target_ext.lstrip('.') r = subprocess.run( - ['libreoffice', '--headless', '--convert-to', target, + ['libreoffice', '--headless', '--norestore', + '-env:UserInstallation=' + _lo_profile(out_dir).as_uri(), + '--convert-to', target, '--outdir', str(out_dir), str(src)], capture_output=True, text=True, timeout=180, ) @@ -93,6 +103,40 @@ def _libreoffice(src: Path, out_dir: Path, target_ext: str) -> Path: return expected +# 重算适用的二进制/文档格式(csv 无缓存概念,跳过) +RECALC_EXTS = {'.xlsx', '.xls', '.ods'} + + +def recalc(path: Path, work_dir: Path): + """LibreOffice 强制重算公式缓存值:openpyxl 产物无缓存,LO 加载时必须求值才能保存, + 实测(LO 7.4)重算结果会写入缓存元素且公式文本保留。 + + 返回 (重算产物路径, None) 或 (None, 失败原因);不抛异常,是否回退由调用方决策。 + 注意返回的文件在 work_dir 内,调用方需在同一生命周期内用完。""" + ext = path.suffix.lower() + if ext not in RECALC_EXTS: + return None, f"{ext} 格式无需重算" + out_dir = work_dir / 'recalc' + out_dir.mkdir(parents=True, exist_ok=True) + expected = out_dir / path.name + try: + r = subprocess.run( + ['libreoffice', '--headless', '--norestore', + '-env:UserInstallation=' + _lo_profile(out_dir).as_uri(), + '--convert-to', ext[1:], + '--outdir', str(out_dir), str(path)], + capture_output=True, text=True, timeout=180, + ) + except FileNotFoundError: + return None, "LibreOffice 不可用" + except subprocess.TimeoutExpired: + return None, "LibreOffice 重算超时(180s)" + if r.returncode != 0 or not expected.exists(): + msg = (r.stderr or r.stdout).strip()[:150] or '无输出' + return None, f"LibreOffice 重算失败: {msg}" + return expected, None + + def _read_csv(path: Path): last_err = None for enc in _CSV_ENCODINGS: diff --git a/skills/sheet-edit/lib/ops.py b/skills/sheet-edit/lib/ops.py index c653a44..26d487b 100644 --- a/skills/sheet-edit/lib/ops.py +++ b/skills/sheet-edit/lib/ops.py @@ -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)。