audit-e2w 0.0.0-stage → 1.11.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +147 -2
- package/cordis.patch.yml +3 -0
- package/dsh_plugins/audit-e2w/lib/engine.mjs +174 -0
- package/dsh_plugins/audit-e2w/lib/index.js +114 -0
- package/dsh_plugins/audit-e2w/lib/skill.js +41 -0
- package/excel_word/README.md +380 -0
- package/excel_word/qoder_engine.py +438 -0
- package/excel_word/requirements.txt +4 -0
- package/excel_word/wesync/__init__.py +19 -0
- package/excel_word/wesync/__main__.py +6 -0
- package/excel_word/wesync/align.py +154 -0
- package/excel_word/wesync/api.py +840 -0
- package/excel_word/wesync/backup_history.py +65 -0
- package/excel_word/wesync/changelog.py +58 -0
- package/excel_word/wesync/cli.py +173 -0
- package/excel_word/wesync/commands/__init__.py +8 -0
- package/excel_word/wesync/commands/blank_cmd.py +250 -0
- package/excel_word/wesync/commands/bookmark_cmd.py +115 -0
- package/excel_word/wesync/commands/clear_cmd.py +48 -0
- package/excel_word/wesync/commands/export_cmd.py +220 -0
- package/excel_word/wesync/commands/marks_cmd.py +152 -0
- package/excel_word/wesync/commands/placeholders_cmd.py +87 -0
- package/excel_word/wesync/commands/writeback_cmd.py +513 -0
- package/excel_word/wesync/config.py +96 -0
- package/excel_word/wesync/console.py +78 -0
- package/excel_word/wesync/docx_blank.py +156 -0
- package/excel_word/wesync/docx_bookmarks.py +287 -0
- package/excel_word/wesync/docx_columns.py +257 -0
- package/excel_word/wesync/docx_edit.py +77 -0
- package/excel_word/wesync/docx_grid.py +212 -0
- package/excel_word/wesync/docx_marks.py +291 -0
- package/excel_word/wesync/docx_meta.py +157 -0
- package/excel_word/wesync/docx_newtable.py +220 -0
- package/excel_word/wesync/docx_placeholders.py +161 -0
- package/excel_word/wesync/docx_rows.py +67 -0
- package/excel_word/wesync/errors.py +5 -0
- package/excel_word/wesync/excel_blocks.py +543 -0
- package/excel_word/wesync/excel_export.py +136 -0
- package/excel_word/wesync/excel_marks.py +193 -0
- package/excel_word/wesync/excel_totals.py +53 -0
- package/excel_word/wesync/fsops.py +214 -0
- package/excel_word/wesync/gui/__init__.py +34 -0
- package/excel_word/wesync/gui/__main__.py +6 -0
- package/excel_word/wesync/gui/app.py +399 -0
- package/excel_word/wesync/gui/manual.py +68 -0
- package/excel_word/wesync/gui/pages/__init__.py +31 -0
- package/excel_word/wesync/gui/pages/_page.py +89 -0
- package/excel_word/wesync/gui/pages/batch_page.py +423 -0
- package/excel_word/wesync/gui/pages/blank_page.py +465 -0
- package/excel_word/wesync/gui/pages/bookmark_page.py +37 -0
- package/excel_word/wesync/gui/pages/export_page.py +88 -0
- package/excel_word/wesync/gui/pages/format_page.py +429 -0
- package/excel_word/wesync/gui/pages/help_page.py +92 -0
- package/excel_word/wesync/gui/pages/history_page.py +145 -0
- package/excel_word/wesync/gui/pages/marks_page.py +178 -0
- package/excel_word/wesync/gui/pages/writeback_page.py +452 -0
- package/excel_word/wesync/gui/resources.py +22 -0
- package/excel_word/wesync/gui/runner.py +74 -0
- package/excel_word/wesync/gui/store.py +34 -0
- package/excel_word/wesync/gui/theme.py +100 -0
- package/excel_word/wesync/gui/widgets/__init__.py +8 -0
- package/excel_word/wesync/gui/widgets/cards.py +20 -0
- package/excel_word/wesync/gui/widgets/filebar.py +58 -0
- package/excel_word/wesync/gui/widgets/logpane.py +110 -0
- package/excel_word/wesync/gui/widgets/nav.py +39 -0
- package/excel_word/wesync/report_format.py +1577 -0
- package/excel_word/wesync/sort_rules.py +244 -0
- package/excel_word/wesync/valuefmt.py +462 -0
- package/package.json +49 -4
- package/qoder_plugins/audit-toolbox/README.md +156 -0
- package/qoder_plugins/audit-toolbox/catalog.mjs +265 -0
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
"""数值与文本互转:Excel 显示文本渲染、列宽估算、轻量公式求值与公式缓存读取。"""
|
|
2
|
+
|
|
3
|
+
import ctypes
|
|
4
|
+
import re
|
|
5
|
+
import sys
|
|
6
|
+
from datetime import date, datetime, time, timedelta
|
|
7
|
+
from decimal import Decimal, ROUND_HALF_UP
|
|
8
|
+
from functools import lru_cache
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from openpyxl import load_workbook
|
|
11
|
+
from openpyxl.styles.numbers import is_date_format
|
|
12
|
+
from openpyxl.utils import column_index_from_string
|
|
13
|
+
from openpyxl.utils.datetime import from_excel
|
|
14
|
+
from .config import ARITH_OK_RE, CELL_REF_RE, NUMBER_RE, RANGE_REF_RE, SHEET_NAME, SUM_RE
|
|
15
|
+
from .console import warn
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def decimals_of_format(number_format: str | None) -> int:
|
|
19
|
+
if not number_format:
|
|
20
|
+
return 0
|
|
21
|
+
section = str(number_format).split(";")[0]
|
|
22
|
+
if "." not in section:
|
|
23
|
+
return 0
|
|
24
|
+
frac = section.split(".", 1)[1]
|
|
25
|
+
count = 0
|
|
26
|
+
for ch in frac:
|
|
27
|
+
if ch in "0#":
|
|
28
|
+
count += 1
|
|
29
|
+
elif ch in "%":
|
|
30
|
+
continue
|
|
31
|
+
else:
|
|
32
|
+
break
|
|
33
|
+
return count
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def to_excel_value(text: str) -> tuple[object, str | None]:
|
|
37
|
+
"""Word 单元格文本 → (Excel 值, 数字格式)。数字文本转真数值,其余保持文本。"""
|
|
38
|
+
stripped = text.strip()
|
|
39
|
+
if stripped == "":
|
|
40
|
+
return None, None
|
|
41
|
+
if NUMBER_RE.match(stripped):
|
|
42
|
+
raw = stripped.replace(",", "")
|
|
43
|
+
decimals = len(raw.split(".")[1]) if "." in raw else 0
|
|
44
|
+
try:
|
|
45
|
+
value = float(raw)
|
|
46
|
+
except ValueError:
|
|
47
|
+
return text, None
|
|
48
|
+
number_format = "#,##0" if decimals == 0 else "#,##0." + "0" * decimals
|
|
49
|
+
return value, number_format
|
|
50
|
+
return text, None
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@lru_cache(maxsize=1)
|
|
54
|
+
def _windows_short_date_format() -> str | None:
|
|
55
|
+
if sys.platform != "win32":
|
|
56
|
+
return None
|
|
57
|
+
try:
|
|
58
|
+
buffer = ctypes.create_unicode_buffer(128)
|
|
59
|
+
if ctypes.windll.kernel32.GetLocaleInfoEx(None, 0x1F, buffer, len(buffer)):
|
|
60
|
+
return buffer.value.replace("M", "m")
|
|
61
|
+
except Exception:
|
|
62
|
+
pass
|
|
63
|
+
return None
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def display_text(value: object, number_format: str | None, num_fmt_id: int | None = None) -> str:
|
|
67
|
+
"""Excel 单元格 → 回写 Word 的显示文本(模拟 Excel 的显示效果)。
|
|
68
|
+
|
|
69
|
+
覆盖审计常用格式子集:日期/时间(含中文年月日字面量)、千分位有无、小数位、
|
|
70
|
+
百分比、负数括号段、零值段;子集外的格式回退旧口径并按格式警告一次。
|
|
71
|
+
"""
|
|
72
|
+
if value is None:
|
|
73
|
+
return ""
|
|
74
|
+
if num_fmt_id == 14:
|
|
75
|
+
number_format = _windows_short_date_format() or number_format
|
|
76
|
+
if isinstance(value, bool):
|
|
77
|
+
return "TRUE" if value else "FALSE"
|
|
78
|
+
if isinstance(value, timedelta):
|
|
79
|
+
# openpyxl 把 [h]:mm 等耗时格式读成 timedelta;还原为序列值走下方数字/回退口径
|
|
80
|
+
value = value.days + value.seconds / 86400 + value.microseconds / 86400e6
|
|
81
|
+
fmt = str(number_format) if number_format else "General"
|
|
82
|
+
if isinstance(value, (datetime, date, time)):
|
|
83
|
+
if isinstance(value, datetime):
|
|
84
|
+
dt = value
|
|
85
|
+
elif isinstance(value, date):
|
|
86
|
+
dt = datetime.combine(value, datetime.min.time())
|
|
87
|
+
else:
|
|
88
|
+
dt = datetime.combine(date.min, value)
|
|
89
|
+
rendered = _render_date(fmt, dt)
|
|
90
|
+
if rendered is not None:
|
|
91
|
+
return rendered
|
|
92
|
+
_warn_format(fmt)
|
|
93
|
+
return str(value)
|
|
94
|
+
if isinstance(value, (int, float)):
|
|
95
|
+
if is_date_format(fmt):
|
|
96
|
+
try:
|
|
97
|
+
dt = from_excel(value)
|
|
98
|
+
except (ValueError, OverflowError, TypeError):
|
|
99
|
+
dt = None
|
|
100
|
+
if dt is not None:
|
|
101
|
+
rendered = _render_date(fmt, dt)
|
|
102
|
+
if rendered is not None:
|
|
103
|
+
return rendered
|
|
104
|
+
_warn_format(fmt)
|
|
105
|
+
return _legacy_number(value, number_format)
|
|
106
|
+
if fmt == "General":
|
|
107
|
+
if isinstance(value, float) and value.is_integer():
|
|
108
|
+
return str(int(value))
|
|
109
|
+
return str(value)
|
|
110
|
+
sections = _split_sections(fmt)
|
|
111
|
+
if value < 0 and len(sections) > 1:
|
|
112
|
+
rendered = _render_number(sections[1], abs(value))
|
|
113
|
+
elif value == 0 and len(sections) > 2:
|
|
114
|
+
rendered = _render_number(sections[2], 0.0)
|
|
115
|
+
else:
|
|
116
|
+
rendered = _render_number(sections[0], value)
|
|
117
|
+
if rendered is not None:
|
|
118
|
+
return rendered
|
|
119
|
+
_warn_format(fmt)
|
|
120
|
+
return _legacy_number(value, number_format)
|
|
121
|
+
return str(value)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
_CELL_VALUE = object()
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def display_cell(cell, value=_CELL_VALUE) -> str:
|
|
128
|
+
if value is _CELL_VALUE:
|
|
129
|
+
value = cell.value
|
|
130
|
+
style = getattr(cell, "_style", None)
|
|
131
|
+
return display_text(value, cell.number_format, getattr(style, "numFmtId", None))
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
_WARNED_FORMATS: set[str] = set()
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _warn_format(fmt: str) -> None:
|
|
138
|
+
if fmt in _WARNED_FORMATS:
|
|
139
|
+
return
|
|
140
|
+
_WARNED_FORMATS.add(fmt)
|
|
141
|
+
warn(f"单元格格式 {fmt} 超出内置显示渲染子集,按千分位+小数位旧口径回写,请核对 Word 显示效果。")
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _legacy_number(value: float, number_format: str | None) -> str:
|
|
145
|
+
decimals = decimals_of_format(number_format)
|
|
146
|
+
if decimals:
|
|
147
|
+
return f"{value:,.{decimals}f}"
|
|
148
|
+
return f"{value:,.0f}"
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def _split_sections(fmt: str) -> list[str]:
|
|
152
|
+
sections: list[str] = []
|
|
153
|
+
buf: list[str] = []
|
|
154
|
+
quoted = escaped = False
|
|
155
|
+
for ch in fmt:
|
|
156
|
+
if escaped:
|
|
157
|
+
escaped = False
|
|
158
|
+
elif ch == "\\":
|
|
159
|
+
escaped = True
|
|
160
|
+
elif ch == '"':
|
|
161
|
+
quoted = not quoted
|
|
162
|
+
elif ch == ";" and not quoted:
|
|
163
|
+
sections.append("".join(buf))
|
|
164
|
+
buf = []
|
|
165
|
+
continue
|
|
166
|
+
buf.append(ch)
|
|
167
|
+
sections.append("".join(buf))
|
|
168
|
+
return sections
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def _tokens(section: str) -> list[tuple[str, str]] | None:
|
|
172
|
+
"""格式段 → 令牌流;遇到子集外记号返回 None(调用方回退+警告)。"""
|
|
173
|
+
toks: list[tuple[str, str]] = []
|
|
174
|
+
i = 0
|
|
175
|
+
while i < len(section):
|
|
176
|
+
ch = section[i]
|
|
177
|
+
if ch == '"':
|
|
178
|
+
j = section.find('"', i + 1)
|
|
179
|
+
if j < 0:
|
|
180
|
+
return None
|
|
181
|
+
toks.append(("lit", section[i + 1 : j]))
|
|
182
|
+
i = j + 1
|
|
183
|
+
elif ch == "\\":
|
|
184
|
+
if i + 1 >= len(section):
|
|
185
|
+
return None
|
|
186
|
+
toks.append(("lit", section[i + 1]))
|
|
187
|
+
i += 2
|
|
188
|
+
elif ch in "0#":
|
|
189
|
+
toks.append(("dig", ch))
|
|
190
|
+
i += 1
|
|
191
|
+
elif ch in ",.%":
|
|
192
|
+
toks.append((ch, ch))
|
|
193
|
+
i += 1
|
|
194
|
+
elif ch in "ymdhs":
|
|
195
|
+
toks.append((ch, ch))
|
|
196
|
+
i += 1
|
|
197
|
+
elif ch == "@":
|
|
198
|
+
toks.append(("at", ch))
|
|
199
|
+
i += 1
|
|
200
|
+
elif ch == "[":
|
|
201
|
+
j = section.find("]", i + 1)
|
|
202
|
+
if j < 0:
|
|
203
|
+
return None
|
|
204
|
+
if not re.fullmatch(r"\$-[0-9A-Fa-f]+", section[i + 1 : j]):
|
|
205
|
+
return None
|
|
206
|
+
i = j + 1 # 区域标记(如 [$-804]),无可见字符
|
|
207
|
+
elif ch in "_*":
|
|
208
|
+
i += 2 # 会计对齐填充(_x 留白、*x 填充符),不产生可见文本
|
|
209
|
+
elif ch == "?":
|
|
210
|
+
toks.append(("pad", ch))
|
|
211
|
+
i += 1
|
|
212
|
+
elif ch in "eE" and i + 1 < len(section) and section[i + 1] in "+-":
|
|
213
|
+
return None
|
|
214
|
+
else:
|
|
215
|
+
toks.append(("lit", ch))
|
|
216
|
+
i += 1
|
|
217
|
+
return toks
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
def _render_number(section: str, value: float) -> str | None:
|
|
221
|
+
toks = _tokens(section)
|
|
222
|
+
if toks is None:
|
|
223
|
+
return None
|
|
224
|
+
if any(k in ("y", "m", "d", "h", "s", "at") for k, _ in toks):
|
|
225
|
+
return None
|
|
226
|
+
if sum(1 for k, _ in toks if k == "%") > 1:
|
|
227
|
+
return None
|
|
228
|
+
scaled = value * 100 if any(k == "%" for k, _ in toks) else value
|
|
229
|
+
dot_at = next((i for i, (k, _) in enumerate(toks) if k == "."), None)
|
|
230
|
+
int_toks = toks if dot_at is None else toks[:dot_at]
|
|
231
|
+
frac_toks = [] if dot_at is None else toks[dot_at + 1 :]
|
|
232
|
+
if any(k == "." for k, _ in frac_toks):
|
|
233
|
+
return None
|
|
234
|
+
int_digs = [t for k, t in int_toks if k == "dig"]
|
|
235
|
+
frac_digs = [t for k, t in frac_toks if k == "dig"]
|
|
236
|
+
if not int_digs and not frac_digs:
|
|
237
|
+
return "".join(t for k, t in toks if k == "lit") # 零值段纯字面量(如 —)
|
|
238
|
+
# 整数位数字必须连续(中间夹字面量的怪格式回退)
|
|
239
|
+
kinds = [k for k, _ in int_toks if k in ("dig", "lit", ",")]
|
|
240
|
+
first = kinds.index("dig") if "dig" in kinds else len(kinds)
|
|
241
|
+
last = len(kinds) - 1 - kinds[::-1].index("dig") if "dig" in kinds else -1
|
|
242
|
+
if any(k == "lit" for k in kinds[first : last + 1]):
|
|
243
|
+
return None
|
|
244
|
+
trailing_commas = 0
|
|
245
|
+
for k, _ in reversed(int_toks):
|
|
246
|
+
if k == ",":
|
|
247
|
+
trailing_commas += 1
|
|
248
|
+
elif k == "dig":
|
|
249
|
+
break
|
|
250
|
+
thousands = any(k == "," for k, _ in int_toks[: len(int_toks) - trailing_commas]) and bool(int_digs)
|
|
251
|
+
scaled = scaled / (1000 ** trailing_commas)
|
|
252
|
+
decimals = len(frac_digs)
|
|
253
|
+
quantum = Decimal(1).scaleb(-decimals)
|
|
254
|
+
rounded = Decimal(str(scaled)).quantize(quantum, rounding=ROUND_HALF_UP)
|
|
255
|
+
sign = "-" if rounded < 0 else ""
|
|
256
|
+
rounded = abs(rounded)
|
|
257
|
+
digits = f"{rounded:.{decimals}f}"
|
|
258
|
+
int_part, _, frac_part = digits.partition(".")
|
|
259
|
+
int_zeros = sum(1 for t in int_digs if t == "0")
|
|
260
|
+
int_part = int_part.zfill(max(1, int_zeros))
|
|
261
|
+
if thousands:
|
|
262
|
+
int_part = f"{int(int_part):,}"
|
|
263
|
+
if frac_digs:
|
|
264
|
+
keep = decimals
|
|
265
|
+
while keep > 0 and frac_part[keep - 1] == "0" and frac_digs[keep - 1] == "#":
|
|
266
|
+
keep -= 1
|
|
267
|
+
frac_part = frac_part[:keep]
|
|
268
|
+
out: list[str] = []
|
|
269
|
+
int_done = frac_done = False
|
|
270
|
+
for idx, (k, t) in enumerate(toks):
|
|
271
|
+
if k == "lit":
|
|
272
|
+
out.append(t)
|
|
273
|
+
elif k == "dig":
|
|
274
|
+
if dot_at is None or idx < dot_at:
|
|
275
|
+
if not int_done:
|
|
276
|
+
out.append(sign + int_part)
|
|
277
|
+
int_done = True
|
|
278
|
+
elif not frac_done:
|
|
279
|
+
out.append(frac_part)
|
|
280
|
+
frac_done = True
|
|
281
|
+
elif k == ".":
|
|
282
|
+
if frac_part:
|
|
283
|
+
out.append(".")
|
|
284
|
+
elif k == "%":
|
|
285
|
+
out.append("%")
|
|
286
|
+
if not int_done and int_digs:
|
|
287
|
+
out.insert(0, sign + int_part)
|
|
288
|
+
return "".join(out)
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
def _render_date(fmt: str, dt: datetime) -> str | None:
|
|
292
|
+
if "AM/PM" in fmt or "am/pm" in fmt:
|
|
293
|
+
return None
|
|
294
|
+
sections = _split_sections(fmt)
|
|
295
|
+
toks = _tokens(sections[0])
|
|
296
|
+
if toks is None:
|
|
297
|
+
return None
|
|
298
|
+
if any(k in ("dig", ",", "%", "at") for k, _ in toks):
|
|
299
|
+
return None
|
|
300
|
+
out: list[str] = []
|
|
301
|
+
i = 0
|
|
302
|
+
while i < len(toks):
|
|
303
|
+
k, t = toks[i]
|
|
304
|
+
if k == "lit":
|
|
305
|
+
out.append(t)
|
|
306
|
+
i += 1
|
|
307
|
+
continue
|
|
308
|
+
run = 1
|
|
309
|
+
while i + run < len(toks) and toks[i + run][0] == k:
|
|
310
|
+
run += 1
|
|
311
|
+
prev_k = next((toks[j][0] for j in range(i - 1, -1, -1) if toks[j][0] != "lit"), "")
|
|
312
|
+
next_k = next((toks[j][0] for j in range(i + run, len(toks)) if toks[j][0] != "lit"), "")
|
|
313
|
+
if k == "y":
|
|
314
|
+
if run == 2:
|
|
315
|
+
out.append(f"{dt.year % 100:02d}")
|
|
316
|
+
elif run == 4:
|
|
317
|
+
out.append(f"{dt.year:04d}")
|
|
318
|
+
else:
|
|
319
|
+
return None
|
|
320
|
+
elif k == "m":
|
|
321
|
+
minutes = prev_k == "h" or next_k == "s"
|
|
322
|
+
unit = dt.minute if minutes else dt.month
|
|
323
|
+
if run == 1:
|
|
324
|
+
out.append(str(unit))
|
|
325
|
+
elif run == 2:
|
|
326
|
+
out.append(f"{unit:02d}")
|
|
327
|
+
else:
|
|
328
|
+
return None
|
|
329
|
+
elif k == "d":
|
|
330
|
+
if run == 1:
|
|
331
|
+
out.append(str(dt.day))
|
|
332
|
+
elif run == 2:
|
|
333
|
+
out.append(f"{dt.day:02d}")
|
|
334
|
+
else:
|
|
335
|
+
return None
|
|
336
|
+
elif k == "h":
|
|
337
|
+
if run == 1:
|
|
338
|
+
out.append(str(dt.hour))
|
|
339
|
+
elif run == 2:
|
|
340
|
+
out.append(f"{dt.hour:02d}")
|
|
341
|
+
else:
|
|
342
|
+
return None
|
|
343
|
+
elif k == "s":
|
|
344
|
+
if run == 1:
|
|
345
|
+
out.append(str(dt.second))
|
|
346
|
+
elif run == 2:
|
|
347
|
+
out.append(f"{dt.second:02d}")
|
|
348
|
+
else:
|
|
349
|
+
return None
|
|
350
|
+
else:
|
|
351
|
+
return None
|
|
352
|
+
i += run
|
|
353
|
+
return "".join(out)
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
def approx_width(text: str) -> float:
|
|
357
|
+
width = 0.0
|
|
358
|
+
for ch in text:
|
|
359
|
+
width += 2.0 if ord(ch) > 0x2E80 else 1.0
|
|
360
|
+
return width
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
def formula_map(xlsx_path: Path) -> dict[tuple[int, int], object]:
|
|
364
|
+
"""读取带缓存值的副本,用于处理用户在 Excel 中写的公式。"""
|
|
365
|
+
cached = load_workbook(str(xlsx_path), data_only=True)
|
|
366
|
+
ws = cached[SHEET_NAME] if SHEET_NAME in cached.sheetnames else cached.worksheets[0]
|
|
367
|
+
result = {}
|
|
368
|
+
for row in ws.iter_rows():
|
|
369
|
+
for cell in row:
|
|
370
|
+
if cell.value is not None:
|
|
371
|
+
result[(cell.row, cell.column)] = cell.value
|
|
372
|
+
return result
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
def formula_result(ws, row: int, col: int, cached_ws=None):
|
|
376
|
+
"""取公式计算结果:优先 Excel 缓存值,否则尝试轻量求值;无结果为 None。"""
|
|
377
|
+
if cached_ws is not None:
|
|
378
|
+
value = cached_ws.cell(row, col).value
|
|
379
|
+
if value is not None:
|
|
380
|
+
return value
|
|
381
|
+
return evaluate_formula(ws, row, col, cached_ws)
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
def _cached_value(cached, row: int, col: int):
|
|
385
|
+
if cached is None:
|
|
386
|
+
return None
|
|
387
|
+
if hasattr(cached, "cell"):
|
|
388
|
+
return cached.cell(row, col).value
|
|
389
|
+
return cached.get((row, col))
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
def _cell_numeric(ws, row: int, col: int, cached, depth: int) -> float:
|
|
393
|
+
cell = ws.cell(row, col)
|
|
394
|
+
value = _cached_value(cached, row, col)
|
|
395
|
+
if value is None:
|
|
396
|
+
value = cell.value
|
|
397
|
+
if isinstance(value, str) and value.startswith("="):
|
|
398
|
+
if depth > 5:
|
|
399
|
+
return 0.0
|
|
400
|
+
resolved = evaluate_formula(ws, row, col, cached, depth + 1)
|
|
401
|
+
return float(resolved) if isinstance(resolved, (int, float)) else 0.0
|
|
402
|
+
if isinstance(value, (int, float)) and not isinstance(value, bool):
|
|
403
|
+
return float(value)
|
|
404
|
+
return 0.0
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
def _range_total(ws, ref1: str, ref2: str, cached, depth: int):
|
|
408
|
+
start = CELL_REF_RE.match(ref1)
|
|
409
|
+
stop = CELL_REF_RE.match(ref2)
|
|
410
|
+
if not start or not stop:
|
|
411
|
+
return None
|
|
412
|
+
c1 = column_index_from_string(start.group(1).upper())
|
|
413
|
+
c2 = column_index_from_string(stop.group(1).upper())
|
|
414
|
+
r1, r2 = int(start.group(2)), int(stop.group(2))
|
|
415
|
+
total = 0.0
|
|
416
|
+
for row in range(min(r1, r2), max(r1, r2) + 1):
|
|
417
|
+
for col in range(min(c1, c2), max(c1, c2) + 1):
|
|
418
|
+
total += _cell_numeric(ws, row, col, cached, depth)
|
|
419
|
+
return total
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
def evaluate_formula(ws, row: int, col: int, cached=None, depth: int = 0):
|
|
423
|
+
"""轻量公式求值:支持 SUM(范围/单元格) 与简单四则运算,算不出时返回 None。
|
|
424
|
+
|
|
425
|
+
用于 openpyxl 保存后公式缓存值丢失的情况(常见于“合计”行)。
|
|
426
|
+
"""
|
|
427
|
+
cell = ws.cell(row, col)
|
|
428
|
+
value = cell.value
|
|
429
|
+
if not (isinstance(value, str) and value.startswith("=")):
|
|
430
|
+
return None
|
|
431
|
+
expression = value[1:].strip()
|
|
432
|
+
sum_match = SUM_RE.match(expression)
|
|
433
|
+
if sum_match:
|
|
434
|
+
total = 0.0
|
|
435
|
+
args = [item.strip() for item in sum_match.group(1).split(",") if item.strip()]
|
|
436
|
+
for arg in args:
|
|
437
|
+
rng = RANGE_REF_RE.match(arg)
|
|
438
|
+
if rng:
|
|
439
|
+
part = _range_total(ws, f"{rng.group(1)}{rng.group(2)}", f"{rng.group(3)}{rng.group(4)}", cached, depth)
|
|
440
|
+
if part is None:
|
|
441
|
+
return None
|
|
442
|
+
total += part
|
|
443
|
+
continue
|
|
444
|
+
ref = CELL_REF_RE.match(arg)
|
|
445
|
+
if not ref:
|
|
446
|
+
return None
|
|
447
|
+
total += _cell_numeric(ws, int(ref.group(2)), column_index_from_string(ref.group(1).upper()), cached, depth)
|
|
448
|
+
return total
|
|
449
|
+
|
|
450
|
+
substituted = expression
|
|
451
|
+
for ref_text in set(re.findall(r"\$?[A-Za-z]{1,3}\$?\d+", expression)):
|
|
452
|
+
ref = CELL_REF_RE.match(ref_text)
|
|
453
|
+
if not ref:
|
|
454
|
+
continue
|
|
455
|
+
number = _cell_numeric(ws, int(ref.group(2)), column_index_from_string(ref.group(1).upper()), cached, depth)
|
|
456
|
+
substituted = substituted.replace(ref_text, repr(number))
|
|
457
|
+
if not ARITH_OK_RE.match(substituted):
|
|
458
|
+
return None
|
|
459
|
+
try:
|
|
460
|
+
return float(eval(substituted, {"__builtins__": {}}, {})) # noqa: S307 - 已用白名单正则限制
|
|
461
|
+
except Exception:
|
|
462
|
+
return None
|
package/package.json
CHANGED
|
@@ -1,6 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "audit-e2w",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.11.2",
|
|
4
|
+
"description": "Audit_E2W:通过 DSH Agent 在本机完成 Word 与 Excel 审计文档处理。",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dsh_plugins/audit-e2w/lib/index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./dsh_plugins/audit-e2w/lib/index.js",
|
|
10
|
+
"./package.json": "./package.json"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dsh_plugins/audit-e2w/lib/*.js",
|
|
14
|
+
"dsh_plugins/audit-e2w/lib/*.mjs",
|
|
15
|
+
"qoder_plugins/audit-toolbox/catalog.mjs",
|
|
16
|
+
"excel_word/qoder_engine.py",
|
|
17
|
+
"excel_word/requirements.txt",
|
|
18
|
+
"excel_word/wesync/**/*.py",
|
|
19
|
+
"cordis.patch.yml",
|
|
20
|
+
"README.md",
|
|
21
|
+
"LICENSE",
|
|
22
|
+
"!excel_word/README.md",
|
|
23
|
+
"!qoder_plugins/audit-toolbox/README.md"
|
|
24
|
+
],
|
|
25
|
+
"scripts": {
|
|
26
|
+
"check": "node dsh_plugins/audit-e2w/scripts/check-package.mjs",
|
|
27
|
+
"test": "node --test dsh_plugins/audit-e2w/test/*.test.mjs",
|
|
28
|
+
"prepack": "npm run check"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=24.11.0",
|
|
35
|
+
"dsh": "0.2.2-rc.5"
|
|
36
|
+
},
|
|
37
|
+
"os": ["win32"],
|
|
38
|
+
"cpu": ["x64"],
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "https://github.com/xymlg/Audit_E2W.git",
|
|
42
|
+
"directory": "."
|
|
43
|
+
},
|
|
44
|
+
"keywords": ["dsh-plugin", "audit", "word", "excel", "windows"],
|
|
45
|
+
"dsh": {
|
|
46
|
+
"bundle": {
|
|
47
|
+
"patch": "./cordis.patch.yml",
|
|
48
|
+
"platforms": ["win32"]
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# 审计工具箱
|
|
2
|
+
|
|
3
|
+
在 Qoder Desktop 中处理 Word 与 Excel 的本地审计办公插件。当前版本:**v1.11.2**。
|
|
4
|
+
|
|
5
|
+
插件把桌面版的核心能力集中在一个界面中,包括创建 Excel、同步 Word、差异比对、链接标注、正文占位符、书签维护、空白表删除与恢复、批量同步、模板批量生成以及备份回滚。
|
|
6
|
+
|
|
7
|
+
## 运行环境
|
|
8
|
+
|
|
9
|
+
- Qoder Desktop,Windows 10/11 64 位。
|
|
10
|
+
- 内置 Windows 本地引擎,**不支持 macOS**。
|
|
11
|
+
- 不需要另外安装 Python 或 Node.js;MCP 服务使用 Qoder 自带 Node.js 运行。
|
|
12
|
+
- 打开和编辑生成的 Word/Excel 文件需要 Microsoft Office。
|
|
13
|
+
- 支持 `.docx` / `.docm`、`.xlsx` / `.xlsm`;写回 `.xlsm` 时保留 VBA。
|
|
14
|
+
|
|
15
|
+
## 安装
|
|
16
|
+
|
|
17
|
+
### 本地 ZIP 安装
|
|
18
|
+
|
|
19
|
+
1. 打开 Qoder Desktop 左侧 **扩展**。
|
|
20
|
+
2. 进入 **插件 → 添加插件 → 导入插件 / 上传 Plugin**。
|
|
21
|
+
3. 选择 `Audit_E2W_qoder_v1.11.2.zip`。
|
|
22
|
+
4. 安装后在 **已安装** 中启用“审计工具箱”。
|
|
23
|
+
|
|
24
|
+
本地安装只在当前设备生效,不会自动发布到公共市场。
|
|
25
|
+
|
|
26
|
+
### 公共市场安装
|
|
27
|
+
|
|
28
|
+
如果公共市场对你所在账号和地区可用,可在 **扩展 → 插件** 中搜索“审计工具箱”,核对发布者和权限后安装。市场是否可见、是否允许发布,以当前客户端和发布中心页面为准。
|
|
29
|
+
|
|
30
|
+
## 打开与首次检查
|
|
31
|
+
|
|
32
|
+
1. 从插件的全局或会话入口打开“审计工具箱”。
|
|
33
|
+
2. 右上角应显示 **本地引擎 1.11.2 已就绪**。
|
|
34
|
+
3. 如提示找不到引擎或版本不一致,请重新安装完整的插件 ZIP,不要只复制 `server.mjs`。
|
|
35
|
+
4. 选择左侧任务,按“选择文件 → 操作选项 → 预演 → 确认执行”的顺序操作。
|
|
36
|
+
|
|
37
|
+
## 典型流程
|
|
38
|
+
|
|
39
|
+
### 1. 从 Word 创建 Excel
|
|
40
|
+
|
|
41
|
+
1. 选择 **创建 Excel**。
|
|
42
|
+
2. 选择 Word 报告;Excel 路径默认按同名报告生成,也可以手选。
|
|
43
|
+
3. 建议先勾选 **仅预演**,确认没有警告。
|
|
44
|
+
4. 正式创建后,工作簿固定包含“表格汇总”“链接标注”“正文信息”三个工作表。
|
|
45
|
+
5. 后续用 Excel 界面修改明细、增删行列或填写标注和占位符,保存并关闭 Excel。
|
|
46
|
+
|
|
47
|
+
### 2. 同步回 Word
|
|
48
|
+
|
|
49
|
+
1. 选择 **同步到 Word**,再次选择同一份 Word 和工作簿。
|
|
50
|
+
2. 选择同步内容:表格、链接标注、正文占位符可以任意组合。
|
|
51
|
+
3. 先 **仅预演** 或使用 **差异比对** 核对。
|
|
52
|
+
4. 正式执行前确认文件列表和备份选项,再点 **确认并执行**。
|
|
53
|
+
5. 同步后检查右侧日志和结果;失败时先关闭 Word/Excel,再按日志或备份恢复。
|
|
54
|
+
|
|
55
|
+
### 3. 标注与占位符
|
|
56
|
+
|
|
57
|
+
- **导出 / 刷新标注**:把 Word 高亮位置写入“链接标注”表。
|
|
58
|
+
- **同步标注到 Word**:把新内容和可选链接写回高亮位置。
|
|
59
|
+
- **补齐占位符工作表**:扫描 `{字段名}`,生成或补齐“正文信息”表。
|
|
60
|
+
- **同步占位符到 Word**:按填写值替换占位符,保留字体和版式。
|
|
61
|
+
|
|
62
|
+
## 让 Agent 帮你干活
|
|
63
|
+
|
|
64
|
+
插件内置 `audit-word-excel` Skill。启用插件后,可以直接在对话里交代任务,不必先打开操作页面。Agent 会按“检查文件和引擎 → 读取能力目录 → 预演或差异检查 → 说明改动并确认 → 正式执行 → 核验结果”的顺序处理。
|
|
65
|
+
|
|
66
|
+
也可以聊天输入 `/`,选择以下快捷命令,再补充本次文件或需求。斜杠命令只是预设提示词,不会跳过 Skill 的安全流程,也不等于同意写盘。
|
|
67
|
+
|
|
68
|
+
| 命令 | 适用场景 | 默认流程 |
|
|
69
|
+
| --- | --- | --- |
|
|
70
|
+
| `/audit-toolbox:export` | Word 导出 Excel | 先预演,确认后生成工作簿 |
|
|
71
|
+
| `/audit-toolbox:sync` | Excel 改动同步回 Word | 先查差异,确认同步范围后写回 |
|
|
72
|
+
| `/audit-toolbox:format` | 按样板或方案调整格式 | 候选确认、格式预演、生成新副本 |
|
|
73
|
+
| `/audit-toolbox:batch` | 批量同步或模板生成 | 先确认文件清单,逐份汇报结果 |
|
|
74
|
+
| `/audit-toolbox:check` | 差异、空白表、模板字段或格式候选 | 全程只读,不修改文件 |
|
|
75
|
+
|
|
76
|
+
`audit_capabilities` 提供 31 项操作(28 项界面操作、3 项对话专用格式操作)的必填参数、默认值、预演支持和示例;任务完成后,`audit_task_status.summary` 会汇总输出文件、改动数量、备份、警告及批量成功/跳过/失败情况。批量任务即使总状态为 `succeeded`,Agent 也会检查 `outcome=partial` 和失败清单。
|
|
77
|
+
|
|
78
|
+
对话专用格式操作为 `format-scan`(格式预演)、`format-heading-candidates`(候选标题、表格前段落角色和附注边界)和 `format-apply`(应用格式)。识别疑难角色时只发送本地筛出的候选及有限上下文,不上传整篇报告或表格单元格;Agent 必须展示建议、置信度和边界变化并取得用户确认,正式应用还须传 `confirmed=true`。结果只改直接排版和本次处理范围,不写标题样式、不改大纲级别、不更新目录。生成结果的 `visual_review` 提供 Word 目视验收清单,结构核验不代表排版已通过。
|
|
79
|
+
|
|
80
|
+
可以直接这样说:
|
|
81
|
+
|
|
82
|
+
- “把 `E:\报告\审计报告.docx` 导出成 Excel,先预演。”
|
|
83
|
+
- “Excel 改好了,只把表格同步回 Word,先告诉我有什么差异。”
|
|
84
|
+
- “把表格、标注和正文占位符全部同步回 Word。”
|
|
85
|
+
- “用 `E:\报告\模板.docx` 和 `E:\报告\底稿.xlsx` 生成一份新 Word,不要覆盖原件。”
|
|
86
|
+
- “批量同步 `E:\报告\待处理` 里的 Word 和同名 Excel,失败的单列出来。”
|
|
87
|
+
|
|
88
|
+
安全约定:
|
|
89
|
+
|
|
90
|
+
- Agent 先预演或只读检查,不会把“先看看”当成写盘指令。
|
|
91
|
+
- 删除、覆盖和恢复备份前会说明范围并等待确认。
|
|
92
|
+
- 单模板生成只创建新 Word,不覆盖模板原件和源 Excel。
|
|
93
|
+
- 文件被占用时先提示关闭文件,不默认强制保存。
|
|
94
|
+
- 完成后汇报输出文件、改动数量、警告、备份位置和建议复核项。
|
|
95
|
+
|
|
96
|
+
如果 Agent 看不到 `audit_*` 工具,先确认插件已安装并启用,再重新加载插件或新建会话。插件服务显示“已连接”但工具数为 0 时,通常是工具可见性或插件版本加载问题。
|
|
97
|
+
|
|
98
|
+
## Qoder 使用边界
|
|
99
|
+
|
|
100
|
+
插件优先通过内置 MCP 工具完成操作;只有 MCP 工具不可用时,才使用包内 Skill 的本地脚本备用通道。
|
|
101
|
+
|
|
102
|
+
Qoder 插件包已包含 Windows 引擎,不需要额外安装 Python 或 Node.js,也不需要把包内文件复制到其他平台。
|
|
103
|
+
|
|
104
|
+
## 功能清单
|
|
105
|
+
|
|
106
|
+
| 分组 | 功能 |
|
|
107
|
+
| --- | --- |
|
|
108
|
+
| 常用 | 创建 Excel、同步到 Word、差异比对、导出差异清单、重新判定 ✓/✕ |
|
|
109
|
+
| 内容 | 导出/刷新标注、同步标注、补齐占位符、同步占位符、检查模板字段 |
|
|
110
|
+
| 维护 | 重建/迁移书签、清理书签、查看表块、追加新表块、查看/设置/清除排序、扫描/删除/恢复空白表 |
|
|
111
|
+
| 批量 | 多报告批量同步、批量扫描/删除空白表、多模板批量生成、生成正文信息填写表 |
|
|
112
|
+
| 备份 | 查看历史备份、恢复历史备份 |
|
|
113
|
+
|
|
114
|
+
## 数据与安全
|
|
115
|
+
|
|
116
|
+
- 报告内容只在本机处理,**插件不联网、不上传整篇报告**;格式疑难识别只向当前宿主 Agent 返回本地筛选的候选和有限上下文。
|
|
117
|
+
- 只处理用户主动选择的本地文件;查询任务只读,写盘任务显示确认。
|
|
118
|
+
- 默认在写盘前备份到原文件旁的 `_backup` 文件夹。
|
|
119
|
+
- 执行前请关闭对应 Word/Excel 文件;文件被占用时会提示,不要随意跳过占用检查。
|
|
120
|
+
- 公式单元格按 Excel 显示结果写回。如果公式没有缓存结果,请先在 Excel 中重算并保存。
|
|
121
|
+
- 不要在插件包、市场介绍或截图里放真实报告、客户名称、账号、密码或 API Key。
|
|
122
|
+
|
|
123
|
+
## 常见问题
|
|
124
|
+
|
|
125
|
+
### 右上角提示引擎不可用
|
|
126
|
+
|
|
127
|
+
确认安装的是完整 ZIP,且 `engine/审计工具箱引擎.exe` 与 `engine/_internal` 均存在。重新安装与当前插件版本一致的包。
|
|
128
|
+
|
|
129
|
+
### 文件选择器不可用
|
|
130
|
+
|
|
131
|
+
直接粘贴完整的 Windows 绝对路径,例如 `E:\报告\审计报告.docx`。相对路径不会被接受。
|
|
132
|
+
|
|
133
|
+
### 同步后公式显示为公式文本或没有结果
|
|
134
|
+
|
|
135
|
+
公式单元格应写回计算结果。若 Excel 尚未计算,先在 Excel 中打开工作簿、完成重算并保存,再重新同步。
|
|
136
|
+
|
|
137
|
+
### 表格被删除或定位错位
|
|
138
|
+
|
|
139
|
+
先查看日志和 `_backup`,不要直接重新导出覆盖已编辑工作簿。可用“查看可恢复表格”或“重建/迁移书签”处理。
|
|
140
|
+
|
|
141
|
+
### 如何更新或卸载
|
|
142
|
+
|
|
143
|
+
- **更新**:安装版本号更高的新 ZIP;审核中的市场版本不会自动替换当前线上版本。
|
|
144
|
+
- **卸载**:在 Qoder 的 **扩展 → 插件 → 已安装** 中管理“审计工具箱”。卸载前自行保存需要保留的报告和工作簿。
|
|
145
|
+
|
|
146
|
+
## 目录说明
|
|
147
|
+
|
|
148
|
+
```text
|
|
149
|
+
.qoder-plugin/plugin.json 插件清单
|
|
150
|
+
.mcp.json 本地 MCP 服务声明
|
|
151
|
+
server.mjs 插件服务与本地引擎桥接
|
|
152
|
+
commands/ 5 个 Agent 快捷斜杠指令
|
|
153
|
+
app.html / app.js 操作界面
|
|
154
|
+
engine/ 打包后的 Windows 引擎及运行库
|
|
155
|
+
assets/icon.png 市场展示图标
|
|
156
|
+
```
|