audit-e2w 0.0.0-stage → 1.11.7
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 +152 -3
- package/cordis.patch.yml +3 -0
- package/dsh_plugins/audit-e2w/lib/engine.mjs +174 -0
- package/dsh_plugins/audit-e2w/lib/index.js +122 -0
- package/dsh_plugins/audit-e2w/lib/skill.js +42 -0
- package/excel_word/README.md +388 -0
- package/excel_word/qoder_engine.py +437 -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/ai/__init__.py +52 -0
- package/excel_word/wesync/ai/actions.py +369 -0
- package/excel_word/wesync/ai/client.py +340 -0
- package/excel_word/wesync/ai/config.py +291 -0
- package/excel_word/wesync/ai/docs.py +177 -0
- package/excel_word/wesync/ai/prompts.py +288 -0
- package/excel_word/wesync/ai/tasks/__init__.py +26 -0
- package/excel_word/wesync/ai/tasks/actions_task.py +97 -0
- package/excel_word/wesync/ai/tasks/combined.py +123 -0
- package/excel_word/wesync/ai/tasks/diff_note.py +61 -0
- package/excel_word/wesync/ai/tasks/headings.py +119 -0
- package/excel_word/wesync/ai/tasks/help_task.py +69 -0
- package/excel_word/wesync/ai/tasks/roles.py +128 -0
- package/excel_word/wesync/align.py +155 -0
- package/excel_word/wesync/api.py +1292 -0
- package/excel_word/wesync/backup_history.py +100 -0
- package/excel_word/wesync/changelog.py +58 -0
- package/excel_word/wesync/cli.py +191 -0
- package/excel_word/wesync/commands/__init__.py +8 -0
- package/excel_word/wesync/commands/blank_cmd.py +279 -0
- package/excel_word/wesync/commands/bookmark_cmd.py +146 -0
- package/excel_word/wesync/commands/clear_cmd.py +50 -0
- package/excel_word/wesync/commands/export_cmd.py +270 -0
- package/excel_word/wesync/commands/incremental_cmd.py +157 -0
- package/excel_word/wesync/commands/marks_cmd.py +160 -0
- package/excel_word/wesync/commands/placeholders_cmd.py +92 -0
- package/excel_word/wesync/commands/writeback_cmd.py +469 -0
- package/excel_word/wesync/config.py +97 -0
- package/excel_word/wesync/console.py +85 -0
- package/excel_word/wesync/docx_blank.py +156 -0
- package/excel_word/wesync/docx_bookmarks.py +294 -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 +399 -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 +200 -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 +502 -0
- package/excel_word/wesync/excel_export.py +143 -0
- package/excel_word/wesync/excel_marks.py +265 -0
- package/excel_word/wesync/excel_totals.py +53 -0
- package/excel_word/wesync/fsops.py +219 -0
- package/excel_word/wesync/gui/__init__.py +53 -0
- package/excel_word/wesync/gui/__main__.py +6 -0
- package/excel_word/wesync/gui/app.py +613 -0
- package/excel_word/wesync/gui/manual.py +70 -0
- package/excel_word/wesync/gui/pages/__init__.py +80 -0
- package/excel_word/wesync/gui/pages/_page.py +211 -0
- package/excel_word/wesync/gui/pages/ai_page.py +854 -0
- package/excel_word/wesync/gui/pages/batch_page.py +456 -0
- package/excel_word/wesync/gui/pages/blank_page.py +530 -0
- package/excel_word/wesync/gui/pages/bookmark_page.py +97 -0
- package/excel_word/wesync/gui/pages/export_page.py +141 -0
- package/excel_word/wesync/gui/pages/feedback_page.py +196 -0
- package/excel_word/wesync/gui/pages/format_page.py +551 -0
- package/excel_word/wesync/gui/pages/help_page.py +158 -0
- package/excel_word/wesync/gui/pages/history_page.py +168 -0
- package/excel_word/wesync/gui/pages/marks_page.py +211 -0
- package/excel_word/wesync/gui/pages/settings_page.py +450 -0
- package/excel_word/wesync/gui/pages/writeback_page.py +473 -0
- package/excel_word/wesync/gui/resources.py +22 -0
- package/excel_word/wesync/gui/runner.py +90 -0
- package/excel_word/wesync/gui/store.py +50 -0
- package/excel_word/wesync/gui/theme.py +271 -0
- package/excel_word/wesync/gui/widgets/__init__.py +8 -0
- package/excel_word/wesync/gui/widgets/ai_config.py +362 -0
- package/excel_word/wesync/gui/widgets/cards.py +139 -0
- package/excel_word/wesync/gui/widgets/chips.py +565 -0
- package/excel_word/wesync/gui/widgets/filebar.py +58 -0
- package/excel_word/wesync/gui/widgets/logpane.py +126 -0
- package/excel_word/wesync/gui/widgets/nav.py +294 -0
- package/excel_word/wesync/gui/widgets/theme_choice.py +93 -0
- package/excel_word/wesync/report_format.py +1585 -0
- package/excel_word/wesync/settings.py +35 -0
- package/excel_word/wesync/sort_rules.py +293 -0
- package/excel_word/wesync/valuefmt.py +485 -0
- package/package.json +53 -6
- package/packaging//344/275/277/347/224/250/350/257/264/346/230/216.txt +247 -0
- package/qoder_plugins/audit-toolbox/README.md +159 -0
- package/qoder_plugins/audit-toolbox/catalog.mjs +265 -0
|
@@ -0,0 +1,485 @@
|
|
|
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, RANGE_REF_RE, 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
|
+
_NUMBER_TEXT_RE = re.compile(r"^-?(?:(?:0|[1-9]\d*)|[1-9]\d{0,2}(?:,\d{3})+)(?:\.\d+)?$")
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def to_excel_value(text: str) -> tuple[object, str | None]:
|
|
40
|
+
"""Word 单元格文本 → (Excel 值, 数字格式)。数字文本转真数值,其余保持文本。"""
|
|
41
|
+
stripped = text.strip()
|
|
42
|
+
if stripped == "":
|
|
43
|
+
return None, None
|
|
44
|
+
if _NUMBER_TEXT_RE.match(stripped):
|
|
45
|
+
raw = stripped.replace(",", "")
|
|
46
|
+
decimals = len(raw.split(".")[1]) if "." in raw else 0
|
|
47
|
+
integer_digits = raw.lstrip("-").split(".", 1)[0]
|
|
48
|
+
if decimals == 0 and len(integer_digits) > 15: # Excel 只保证 15 位整数精度
|
|
49
|
+
return text, None
|
|
50
|
+
try:
|
|
51
|
+
value = int(raw) if decimals == 0 else float(raw)
|
|
52
|
+
except ValueError:
|
|
53
|
+
return text, None
|
|
54
|
+
number_format = "#,##0" if decimals == 0 else "#,##0." + "0" * decimals
|
|
55
|
+
return value, number_format
|
|
56
|
+
return text, None
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@lru_cache(maxsize=1)
|
|
60
|
+
def _windows_short_date_format() -> str | None:
|
|
61
|
+
if sys.platform != "win32":
|
|
62
|
+
return None
|
|
63
|
+
try:
|
|
64
|
+
buffer = ctypes.create_unicode_buffer(128)
|
|
65
|
+
if ctypes.windll.kernel32.GetLocaleInfoEx(None, 0x1F, buffer, len(buffer)):
|
|
66
|
+
return buffer.value.replace("M", "m")
|
|
67
|
+
except Exception:
|
|
68
|
+
pass
|
|
69
|
+
return None
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def display_text(
|
|
73
|
+
value: object, number_format: str | None, num_fmt_id: int | None = None, zero_mode: str = "excel"
|
|
74
|
+
) -> str:
|
|
75
|
+
"""Excel 单元格 → 回写 Word 的显示文本(模拟 Excel 的显示效果)。
|
|
76
|
+
|
|
77
|
+
覆盖审计常用格式子集:日期/时间(含中文年月日字面量)、千分位有无、小数位、
|
|
78
|
+
百分比、负数括号段、零值段;子集外的格式回退旧口径并按格式警告一次。
|
|
79
|
+
"""
|
|
80
|
+
if value is None:
|
|
81
|
+
return ""
|
|
82
|
+
if num_fmt_id == 14:
|
|
83
|
+
number_format = _windows_short_date_format() or number_format
|
|
84
|
+
if isinstance(value, bool):
|
|
85
|
+
return "TRUE" if value else "FALSE"
|
|
86
|
+
if zero_mode not in ("excel", "fixed", "dash", "blank"):
|
|
87
|
+
zero_mode = "excel"
|
|
88
|
+
if isinstance(value, (int, float)) and value == 0:
|
|
89
|
+
if zero_mode == "fixed":
|
|
90
|
+
return "0.00"
|
|
91
|
+
if zero_mode == "dash":
|
|
92
|
+
return "-"
|
|
93
|
+
if zero_mode == "blank":
|
|
94
|
+
return ""
|
|
95
|
+
if isinstance(value, timedelta):
|
|
96
|
+
# openpyxl 把 [h]:mm 等耗时格式读成 timedelta;还原为序列值走下方数字/回退口径
|
|
97
|
+
value = value.days + value.seconds / 86400 + value.microseconds / 86400e6
|
|
98
|
+
fmt = str(number_format) if number_format else "General"
|
|
99
|
+
if isinstance(value, (datetime, date, time)):
|
|
100
|
+
if isinstance(value, datetime):
|
|
101
|
+
dt = value
|
|
102
|
+
elif isinstance(value, date):
|
|
103
|
+
dt = datetime.combine(value, datetime.min.time())
|
|
104
|
+
else:
|
|
105
|
+
dt = datetime.combine(date.min, value)
|
|
106
|
+
rendered = _render_date(fmt, dt)
|
|
107
|
+
if rendered is not None:
|
|
108
|
+
return rendered
|
|
109
|
+
_warn_format(fmt)
|
|
110
|
+
return str(value)
|
|
111
|
+
if isinstance(value, (int, float)):
|
|
112
|
+
if is_date_format(fmt):
|
|
113
|
+
try:
|
|
114
|
+
dt = from_excel(value)
|
|
115
|
+
except (ValueError, OverflowError, TypeError):
|
|
116
|
+
dt = None
|
|
117
|
+
if dt is not None:
|
|
118
|
+
rendered = _render_date(fmt, dt)
|
|
119
|
+
if rendered is not None:
|
|
120
|
+
return rendered
|
|
121
|
+
_warn_format(fmt)
|
|
122
|
+
return _legacy_number(value, number_format)
|
|
123
|
+
if fmt == "General":
|
|
124
|
+
if isinstance(value, float) and value.is_integer():
|
|
125
|
+
return str(int(value))
|
|
126
|
+
return str(value)
|
|
127
|
+
sections = _split_sections(fmt)
|
|
128
|
+
if value < 0 and len(sections) > 1:
|
|
129
|
+
rendered = _render_number(sections[1], abs(value))
|
|
130
|
+
elif value == 0 and len(sections) > 2:
|
|
131
|
+
rendered = _render_number(sections[2], 0.0)
|
|
132
|
+
else:
|
|
133
|
+
rendered = _render_number(sections[0], value)
|
|
134
|
+
if rendered is not None:
|
|
135
|
+
return rendered
|
|
136
|
+
_warn_format(fmt)
|
|
137
|
+
return _legacy_number(value, number_format)
|
|
138
|
+
return str(value)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
_CELL_VALUE = object()
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def display_cell(cell, value=_CELL_VALUE, zero_mode: str = "excel") -> str:
|
|
145
|
+
if value is _CELL_VALUE:
|
|
146
|
+
value = cell.value
|
|
147
|
+
style = getattr(cell, "_style", None)
|
|
148
|
+
return display_text(value, cell.number_format, getattr(style, "numFmtId", None), zero_mode=zero_mode)
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
_WARNED_FORMATS: set[str] = set()
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _warn_format(fmt: str) -> None:
|
|
155
|
+
if fmt in _WARNED_FORMATS:
|
|
156
|
+
return
|
|
157
|
+
_WARNED_FORMATS.add(fmt)
|
|
158
|
+
warn(f"单元格格式 {fmt} 超出内置显示渲染子集,按千分位+小数位旧口径回写,请核对 Word 显示效果。")
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _legacy_number(value: float, number_format: str | None) -> str:
|
|
162
|
+
decimals = decimals_of_format(number_format)
|
|
163
|
+
if decimals:
|
|
164
|
+
return f"{value:,.{decimals}f}"
|
|
165
|
+
return f"{value:,.0f}"
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _split_sections(fmt: str) -> list[str]:
|
|
169
|
+
sections: list[str] = []
|
|
170
|
+
buf: list[str] = []
|
|
171
|
+
quoted = escaped = False
|
|
172
|
+
for ch in fmt:
|
|
173
|
+
if escaped:
|
|
174
|
+
escaped = False
|
|
175
|
+
elif ch == "\\":
|
|
176
|
+
escaped = True
|
|
177
|
+
elif ch == '"':
|
|
178
|
+
quoted = not quoted
|
|
179
|
+
elif ch == ";" and not quoted:
|
|
180
|
+
sections.append("".join(buf))
|
|
181
|
+
buf = []
|
|
182
|
+
continue
|
|
183
|
+
buf.append(ch)
|
|
184
|
+
sections.append("".join(buf))
|
|
185
|
+
return sections
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def _tokens(section: str) -> list[tuple[str, str]] | None:
|
|
189
|
+
"""格式段 → 令牌流;遇到子集外记号返回 None(调用方回退+警告)。"""
|
|
190
|
+
toks: list[tuple[str, str]] = []
|
|
191
|
+
i = 0
|
|
192
|
+
while i < len(section):
|
|
193
|
+
ch = section[i]
|
|
194
|
+
if ch == '"':
|
|
195
|
+
j = section.find('"', i + 1)
|
|
196
|
+
if j < 0:
|
|
197
|
+
return None
|
|
198
|
+
toks.append(("lit", section[i + 1 : j]))
|
|
199
|
+
i = j + 1
|
|
200
|
+
elif ch == "\\":
|
|
201
|
+
if i + 1 >= len(section):
|
|
202
|
+
return None
|
|
203
|
+
toks.append(("lit", section[i + 1]))
|
|
204
|
+
i += 2
|
|
205
|
+
elif ch in "0#":
|
|
206
|
+
toks.append(("dig", ch))
|
|
207
|
+
i += 1
|
|
208
|
+
elif ch in ",.%":
|
|
209
|
+
toks.append((ch, ch))
|
|
210
|
+
i += 1
|
|
211
|
+
elif ch in "ymdhs":
|
|
212
|
+
toks.append((ch, ch))
|
|
213
|
+
i += 1
|
|
214
|
+
elif ch == "@":
|
|
215
|
+
toks.append(("at", ch))
|
|
216
|
+
i += 1
|
|
217
|
+
elif ch == "[":
|
|
218
|
+
j = section.find("]", i + 1)
|
|
219
|
+
if j < 0:
|
|
220
|
+
return None
|
|
221
|
+
if not re.fullmatch(r"\$-[0-9A-Fa-f]+", section[i + 1 : j]):
|
|
222
|
+
return None
|
|
223
|
+
i = j + 1 # 区域标记(如 [$-804]),无可见字符
|
|
224
|
+
elif ch in "_*":
|
|
225
|
+
i += 2 # 会计对齐填充(_x 留白、*x 填充符),不产生可见文本
|
|
226
|
+
elif ch == "?":
|
|
227
|
+
toks.append(("pad", ch))
|
|
228
|
+
i += 1
|
|
229
|
+
elif ch in "eE" and i + 1 < len(section) and section[i + 1] in "+-":
|
|
230
|
+
return None
|
|
231
|
+
else:
|
|
232
|
+
toks.append(("lit", ch))
|
|
233
|
+
i += 1
|
|
234
|
+
return toks
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def _render_number(section: str, value: float) -> str | None:
|
|
238
|
+
toks = _tokens(section)
|
|
239
|
+
if toks is None:
|
|
240
|
+
return None
|
|
241
|
+
if any(k in ("y", "m", "d", "h", "s", "at") for k, _ in toks):
|
|
242
|
+
return None
|
|
243
|
+
if sum(1 for k, _ in toks if k == "%") > 1:
|
|
244
|
+
return None
|
|
245
|
+
scaled = value * 100 if any(k == "%" for k, _ in toks) else value
|
|
246
|
+
dot_at = next((i for i, (k, _) in enumerate(toks) if k == "."), None)
|
|
247
|
+
int_toks = toks if dot_at is None else toks[:dot_at]
|
|
248
|
+
frac_toks = [] if dot_at is None else toks[dot_at + 1 :]
|
|
249
|
+
if any(k == "." for k, _ in frac_toks):
|
|
250
|
+
return None
|
|
251
|
+
int_digs = [t for k, t in int_toks if k == "dig"]
|
|
252
|
+
frac_digs = [t for k, t in frac_toks if k == "dig"]
|
|
253
|
+
if not int_digs and not frac_digs:
|
|
254
|
+
return "".join(t for k, t in toks if k == "lit") # 零值段纯字面量(如 —)
|
|
255
|
+
# 整数位数字必须连续(中间夹字面量的怪格式回退)
|
|
256
|
+
kinds = [k for k, _ in int_toks if k in ("dig", "lit", ",")]
|
|
257
|
+
first = kinds.index("dig") if "dig" in kinds else len(kinds)
|
|
258
|
+
last = len(kinds) - 1 - kinds[::-1].index("dig") if "dig" in kinds else -1
|
|
259
|
+
if any(k == "lit" for k in kinds[first : last + 1]):
|
|
260
|
+
return None
|
|
261
|
+
trailing_commas = 0
|
|
262
|
+
for k, _ in reversed(int_toks):
|
|
263
|
+
if k == ",":
|
|
264
|
+
trailing_commas += 1
|
|
265
|
+
elif k == "dig":
|
|
266
|
+
break
|
|
267
|
+
thousands = any(k == "," for k, _ in int_toks[: len(int_toks) - trailing_commas]) and bool(int_digs)
|
|
268
|
+
scaled = scaled / (1000 ** trailing_commas)
|
|
269
|
+
decimals = len(frac_digs)
|
|
270
|
+
quantum = Decimal(1).scaleb(-decimals)
|
|
271
|
+
rounded = Decimal(str(scaled)).quantize(quantum, rounding=ROUND_HALF_UP)
|
|
272
|
+
sign = "-" if rounded < 0 else ""
|
|
273
|
+
rounded = abs(rounded)
|
|
274
|
+
digits = f"{rounded:.{decimals}f}"
|
|
275
|
+
int_part, _, frac_part = digits.partition(".")
|
|
276
|
+
int_zeros = sum(1 for t in int_digs if t == "0")
|
|
277
|
+
int_part = int_part.zfill(max(1, int_zeros))
|
|
278
|
+
if thousands:
|
|
279
|
+
int_part = f"{int(int_part):,}"
|
|
280
|
+
if frac_digs:
|
|
281
|
+
keep = decimals
|
|
282
|
+
while keep > 0 and frac_part[keep - 1] == "0" and frac_digs[keep - 1] == "#":
|
|
283
|
+
keep -= 1
|
|
284
|
+
frac_part = frac_part[:keep]
|
|
285
|
+
out: list[str] = []
|
|
286
|
+
int_done = frac_done = False
|
|
287
|
+
for idx, (k, t) in enumerate(toks):
|
|
288
|
+
if k == "lit":
|
|
289
|
+
out.append(t)
|
|
290
|
+
elif k == "dig":
|
|
291
|
+
if dot_at is None or idx < dot_at:
|
|
292
|
+
if not int_done:
|
|
293
|
+
out.append(sign + int_part)
|
|
294
|
+
int_done = True
|
|
295
|
+
elif not frac_done:
|
|
296
|
+
out.append(frac_part)
|
|
297
|
+
frac_done = True
|
|
298
|
+
elif k == ".":
|
|
299
|
+
if frac_part:
|
|
300
|
+
out.append(".")
|
|
301
|
+
elif k == "%":
|
|
302
|
+
out.append("%")
|
|
303
|
+
if not int_done and int_digs:
|
|
304
|
+
out.insert(0, sign + int_part)
|
|
305
|
+
return "".join(out)
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def _render_date(fmt: str, dt: datetime) -> str | None:
|
|
309
|
+
if "AM/PM" in fmt or "am/pm" in fmt:
|
|
310
|
+
return None
|
|
311
|
+
sections = _split_sections(fmt)
|
|
312
|
+
toks = _tokens(sections[0])
|
|
313
|
+
if toks is None:
|
|
314
|
+
return None
|
|
315
|
+
if any(k in ("dig", ",", "%", "at") for k, _ in toks):
|
|
316
|
+
return None
|
|
317
|
+
out: list[str] = []
|
|
318
|
+
i = 0
|
|
319
|
+
while i < len(toks):
|
|
320
|
+
k, t = toks[i]
|
|
321
|
+
if k == "lit":
|
|
322
|
+
out.append(t)
|
|
323
|
+
i += 1
|
|
324
|
+
continue
|
|
325
|
+
run = 1
|
|
326
|
+
while i + run < len(toks) and toks[i + run][0] == k:
|
|
327
|
+
run += 1
|
|
328
|
+
prev_k = next((toks[j][0] for j in range(i - 1, -1, -1) if toks[j][0] != "lit"), "")
|
|
329
|
+
next_k = next((toks[j][0] for j in range(i + run, len(toks)) if toks[j][0] != "lit"), "")
|
|
330
|
+
if k == "y":
|
|
331
|
+
if run == 2:
|
|
332
|
+
out.append(f"{dt.year % 100:02d}")
|
|
333
|
+
elif run == 4:
|
|
334
|
+
out.append(f"{dt.year:04d}")
|
|
335
|
+
else:
|
|
336
|
+
return None
|
|
337
|
+
elif k == "m":
|
|
338
|
+
minutes = prev_k == "h" or next_k == "s"
|
|
339
|
+
unit = dt.minute if minutes else dt.month
|
|
340
|
+
if run == 1:
|
|
341
|
+
out.append(str(unit))
|
|
342
|
+
elif run == 2:
|
|
343
|
+
out.append(f"{unit:02d}")
|
|
344
|
+
else:
|
|
345
|
+
return None
|
|
346
|
+
elif k == "d":
|
|
347
|
+
if run == 1:
|
|
348
|
+
out.append(str(dt.day))
|
|
349
|
+
elif run == 2:
|
|
350
|
+
out.append(f"{dt.day:02d}")
|
|
351
|
+
else:
|
|
352
|
+
return None
|
|
353
|
+
elif k == "h":
|
|
354
|
+
if run == 1:
|
|
355
|
+
out.append(str(dt.hour))
|
|
356
|
+
elif run == 2:
|
|
357
|
+
out.append(f"{dt.hour:02d}")
|
|
358
|
+
else:
|
|
359
|
+
return None
|
|
360
|
+
elif k == "s":
|
|
361
|
+
if run == 1:
|
|
362
|
+
out.append(str(dt.second))
|
|
363
|
+
elif run == 2:
|
|
364
|
+
out.append(f"{dt.second:02d}")
|
|
365
|
+
else:
|
|
366
|
+
return None
|
|
367
|
+
else:
|
|
368
|
+
return None
|
|
369
|
+
i += run
|
|
370
|
+
return "".join(out)
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
def approx_width(text: str) -> float:
|
|
374
|
+
width = 0.0
|
|
375
|
+
for ch in text:
|
|
376
|
+
width += 2.0 if ord(ch) > 0x2E80 else 1.0
|
|
377
|
+
return width
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
def formula_map(xlsx_path: Path, *, cached_wb=None) -> dict[tuple[str, int, int], object]:
|
|
381
|
+
"""读取所有工作表带缓存值的副本,用于处理用户在 Excel 中写的公式。"""
|
|
382
|
+
own_wb = cached_wb is None
|
|
383
|
+
if own_wb:
|
|
384
|
+
cached_wb = load_workbook(str(xlsx_path), data_only=True)
|
|
385
|
+
result = {}
|
|
386
|
+
try:
|
|
387
|
+
for ws in cached_wb.worksheets:
|
|
388
|
+
for row in ws.iter_rows():
|
|
389
|
+
for cell in row:
|
|
390
|
+
if cell.value is not None:
|
|
391
|
+
result[(ws.title, cell.row, cell.column)] = cell.value
|
|
392
|
+
finally:
|
|
393
|
+
if own_wb:
|
|
394
|
+
cached_wb.close()
|
|
395
|
+
return result
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
def formula_result(ws, row: int, col: int, cached_ws=None):
|
|
399
|
+
"""取公式计算结果:优先 Excel 缓存值,否则尝试轻量求值;无结果为 None。"""
|
|
400
|
+
if cached_ws is not None:
|
|
401
|
+
value = cached_ws.cell(row, col).value
|
|
402
|
+
if value is not None:
|
|
403
|
+
return value
|
|
404
|
+
return evaluate_formula(ws, row, col, cached_ws)
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
def _cached_value(ws, row: int, col: int, cached):
|
|
408
|
+
if cached is None:
|
|
409
|
+
return None
|
|
410
|
+
if hasattr(cached, "cell"):
|
|
411
|
+
return cached.cell(row, col).value
|
|
412
|
+
return cached.get((ws.title, row, col))
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
def _cell_numeric(ws, row: int, col: int, cached, depth: int) -> float:
|
|
416
|
+
cell = ws.cell(row, col)
|
|
417
|
+
value = _cached_value(ws, row, col, cached)
|
|
418
|
+
if value is None:
|
|
419
|
+
value = cell.value
|
|
420
|
+
if isinstance(value, str) and value.startswith("="):
|
|
421
|
+
if depth > 5:
|
|
422
|
+
return 0.0
|
|
423
|
+
resolved = evaluate_formula(ws, row, col, cached, depth + 1)
|
|
424
|
+
return float(resolved) if isinstance(resolved, (int, float)) else 0.0
|
|
425
|
+
if isinstance(value, (int, float)) and not isinstance(value, bool):
|
|
426
|
+
return float(value)
|
|
427
|
+
return 0.0
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
def _range_total(ws, ref1: str, ref2: str, cached, depth: int):
|
|
431
|
+
start = CELL_REF_RE.match(ref1)
|
|
432
|
+
stop = CELL_REF_RE.match(ref2)
|
|
433
|
+
if not start or not stop:
|
|
434
|
+
return None
|
|
435
|
+
c1 = column_index_from_string(start.group(1).upper())
|
|
436
|
+
c2 = column_index_from_string(stop.group(1).upper())
|
|
437
|
+
r1, r2 = int(start.group(2)), int(stop.group(2))
|
|
438
|
+
total = 0.0
|
|
439
|
+
for row in range(min(r1, r2), max(r1, r2) + 1):
|
|
440
|
+
for col in range(min(c1, c2), max(c1, c2) + 1):
|
|
441
|
+
total += _cell_numeric(ws, row, col, cached, depth)
|
|
442
|
+
return total
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
def evaluate_formula(ws, row: int, col: int, cached=None, depth: int = 0):
|
|
446
|
+
"""轻量公式求值:支持 SUM(范围/单元格) 与简单四则运算,算不出时返回 None。
|
|
447
|
+
|
|
448
|
+
用于 openpyxl 保存后公式缓存值丢失的情况(常见于“合计”行)。
|
|
449
|
+
"""
|
|
450
|
+
cell = ws.cell(row, col)
|
|
451
|
+
value = cell.value
|
|
452
|
+
if not (isinstance(value, str) and value.startswith("=")):
|
|
453
|
+
return None
|
|
454
|
+
expression = value[1:].strip()
|
|
455
|
+
sum_match = SUM_RE.match(expression)
|
|
456
|
+
if sum_match:
|
|
457
|
+
total = 0.0
|
|
458
|
+
args = [item.strip() for item in sum_match.group(1).split(",") if item.strip()]
|
|
459
|
+
for arg in args:
|
|
460
|
+
rng = RANGE_REF_RE.match(arg)
|
|
461
|
+
if rng:
|
|
462
|
+
part = _range_total(ws, f"{rng.group(1)}{rng.group(2)}", f"{rng.group(3)}{rng.group(4)}", cached, depth)
|
|
463
|
+
if part is None:
|
|
464
|
+
return None
|
|
465
|
+
total += part
|
|
466
|
+
continue
|
|
467
|
+
ref = CELL_REF_RE.match(arg)
|
|
468
|
+
if not ref:
|
|
469
|
+
return None
|
|
470
|
+
total += _cell_numeric(ws, int(ref.group(2)), column_index_from_string(ref.group(1).upper()), cached, depth)
|
|
471
|
+
return total
|
|
472
|
+
|
|
473
|
+
substituted = expression
|
|
474
|
+
for ref_text in set(re.findall(r"\$?[A-Za-z]{1,3}\$?\d+", expression)):
|
|
475
|
+
ref = CELL_REF_RE.match(ref_text)
|
|
476
|
+
if not ref:
|
|
477
|
+
continue
|
|
478
|
+
number = _cell_numeric(ws, int(ref.group(2)), column_index_from_string(ref.group(1).upper()), cached, depth)
|
|
479
|
+
substituted = substituted.replace(ref_text, repr(number))
|
|
480
|
+
if not ARITH_OK_RE.match(substituted):
|
|
481
|
+
return None
|
|
482
|
+
try:
|
|
483
|
+
return float(eval(substituted, {"__builtins__": {}}, {})) # noqa: S307 - 已用白名单正则限制
|
|
484
|
+
except Exception:
|
|
485
|
+
return None
|
package/package.json
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "audit-e2w",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "audit-e2w",
|
|
3
|
+
"version": "1.11.7",
|
|
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
|
+
"packaging/使用说明.txt",
|
|
20
|
+
"cordis.patch.yml",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE",
|
|
23
|
+
"!excel_word/README.md",
|
|
24
|
+
"!qoder_plugins/audit-toolbox/README.md"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"check": "node dsh_plugins/audit-e2w/scripts/check-package.mjs",
|
|
28
|
+
"test": "node --test dsh_plugins/audit-e2w/test/*.test.mjs",
|
|
29
|
+
"prepack": "npm run check",
|
|
30
|
+
"pack:dist": "node scripts/pack-dsh.mjs"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"engines": {
|
|
36
|
+
"node": ">=24.11.0",
|
|
37
|
+
"dsh": "0.2.2-rc.5"
|
|
38
|
+
},
|
|
39
|
+
"os": ["win32"],
|
|
40
|
+
"cpu": ["x64"],
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "https://github.com/xymlg/Audit_E2W.git",
|
|
44
|
+
"directory": "."
|
|
45
|
+
},
|
|
46
|
+
"keywords": ["dsh-plugin", "audit", "word", "excel", "windows"],
|
|
47
|
+
"dsh": {
|
|
48
|
+
"bundle": {
|
|
49
|
+
"patch": "./cordis.patch.yml",
|
|
50
|
+
"platforms": ["win32"]
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|