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.
Files changed (72) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +147 -2
  3. package/cordis.patch.yml +3 -0
  4. package/dsh_plugins/audit-e2w/lib/engine.mjs +174 -0
  5. package/dsh_plugins/audit-e2w/lib/index.js +114 -0
  6. package/dsh_plugins/audit-e2w/lib/skill.js +41 -0
  7. package/excel_word/README.md +380 -0
  8. package/excel_word/qoder_engine.py +438 -0
  9. package/excel_word/requirements.txt +4 -0
  10. package/excel_word/wesync/__init__.py +19 -0
  11. package/excel_word/wesync/__main__.py +6 -0
  12. package/excel_word/wesync/align.py +154 -0
  13. package/excel_word/wesync/api.py +840 -0
  14. package/excel_word/wesync/backup_history.py +65 -0
  15. package/excel_word/wesync/changelog.py +58 -0
  16. package/excel_word/wesync/cli.py +173 -0
  17. package/excel_word/wesync/commands/__init__.py +8 -0
  18. package/excel_word/wesync/commands/blank_cmd.py +250 -0
  19. package/excel_word/wesync/commands/bookmark_cmd.py +115 -0
  20. package/excel_word/wesync/commands/clear_cmd.py +48 -0
  21. package/excel_word/wesync/commands/export_cmd.py +220 -0
  22. package/excel_word/wesync/commands/marks_cmd.py +152 -0
  23. package/excel_word/wesync/commands/placeholders_cmd.py +87 -0
  24. package/excel_word/wesync/commands/writeback_cmd.py +513 -0
  25. package/excel_word/wesync/config.py +96 -0
  26. package/excel_word/wesync/console.py +78 -0
  27. package/excel_word/wesync/docx_blank.py +156 -0
  28. package/excel_word/wesync/docx_bookmarks.py +287 -0
  29. package/excel_word/wesync/docx_columns.py +257 -0
  30. package/excel_word/wesync/docx_edit.py +77 -0
  31. package/excel_word/wesync/docx_grid.py +212 -0
  32. package/excel_word/wesync/docx_marks.py +291 -0
  33. package/excel_word/wesync/docx_meta.py +157 -0
  34. package/excel_word/wesync/docx_newtable.py +220 -0
  35. package/excel_word/wesync/docx_placeholders.py +161 -0
  36. package/excel_word/wesync/docx_rows.py +67 -0
  37. package/excel_word/wesync/errors.py +5 -0
  38. package/excel_word/wesync/excel_blocks.py +543 -0
  39. package/excel_word/wesync/excel_export.py +136 -0
  40. package/excel_word/wesync/excel_marks.py +193 -0
  41. package/excel_word/wesync/excel_totals.py +53 -0
  42. package/excel_word/wesync/fsops.py +214 -0
  43. package/excel_word/wesync/gui/__init__.py +34 -0
  44. package/excel_word/wesync/gui/__main__.py +6 -0
  45. package/excel_word/wesync/gui/app.py +399 -0
  46. package/excel_word/wesync/gui/manual.py +68 -0
  47. package/excel_word/wesync/gui/pages/__init__.py +31 -0
  48. package/excel_word/wesync/gui/pages/_page.py +89 -0
  49. package/excel_word/wesync/gui/pages/batch_page.py +423 -0
  50. package/excel_word/wesync/gui/pages/blank_page.py +465 -0
  51. package/excel_word/wesync/gui/pages/bookmark_page.py +37 -0
  52. package/excel_word/wesync/gui/pages/export_page.py +88 -0
  53. package/excel_word/wesync/gui/pages/format_page.py +429 -0
  54. package/excel_word/wesync/gui/pages/help_page.py +92 -0
  55. package/excel_word/wesync/gui/pages/history_page.py +145 -0
  56. package/excel_word/wesync/gui/pages/marks_page.py +178 -0
  57. package/excel_word/wesync/gui/pages/writeback_page.py +452 -0
  58. package/excel_word/wesync/gui/resources.py +22 -0
  59. package/excel_word/wesync/gui/runner.py +74 -0
  60. package/excel_word/wesync/gui/store.py +34 -0
  61. package/excel_word/wesync/gui/theme.py +100 -0
  62. package/excel_word/wesync/gui/widgets/__init__.py +8 -0
  63. package/excel_word/wesync/gui/widgets/cards.py +20 -0
  64. package/excel_word/wesync/gui/widgets/filebar.py +58 -0
  65. package/excel_word/wesync/gui/widgets/logpane.py +110 -0
  66. package/excel_word/wesync/gui/widgets/nav.py +39 -0
  67. package/excel_word/wesync/report_format.py +1577 -0
  68. package/excel_word/wesync/sort_rules.py +244 -0
  69. package/excel_word/wesync/valuefmt.py +462 -0
  70. package/package.json +49 -4
  71. package/qoder_plugins/audit-toolbox/README.md +156 -0
  72. package/qoder_plugins/audit-toolbox/catalog.mjs +265 -0
@@ -0,0 +1,244 @@
1
+ """按金额列排序:锚点行「排序:」设置的读写、数据行重排、导出重建时按书签沿用。
2
+
3
+ 规则:表头行与合计行不动;首列(或第二列)项目名为「其他」的行不参与排序,
4
+ 统一放在数据区末尾(合计行之前);解析不了的金额按最小值排在排序段末尾。
5
+ 数据区含合并单元格的表保守跳过,避免错位。
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import re
11
+ from copy import copy
12
+
13
+ from openpyxl.styles import Alignment, Font
14
+
15
+ from .config import FILL_ANCHOR, FONT_NAME, FONT_SIZE, META_SCAN_COLUMNS, NUMBER_RE, SHEET_NAME
16
+ from .console import log, warn
17
+ from .errors import SyncError
18
+ from .valuefmt import display_cell, evaluate_formula
19
+
20
+ SORT_PREFIX = "排序:"
21
+ DESC = "降序"
22
+ ASC = "升序"
23
+ OTHER_ITEM = "其他"
24
+
25
+ _RANGE_OR_REF_RE = re.compile(r"(?<![A-Za-z0-9_$])(\$?[A-Za-z]{1,3}\$?)(\d+)(?::(\$?[A-Za-z]{1,3}\$?)(\d+))?")
26
+
27
+
28
+ def sort_setting_text(column: str, descending: bool = True) -> str:
29
+ return f"{SORT_PREFIX}{column.strip()} {DESC if descending else ASC}"
30
+
31
+
32
+ def parse_sort_setting(text: str) -> tuple[str, bool] | None:
33
+ text = (text or "").strip()
34
+ if not text.startswith(SORT_PREFIX):
35
+ return None
36
+ body = text[len(SORT_PREFIX):].strip()
37
+ descending = True
38
+ if body.endswith(DESC):
39
+ body = body[: -len(DESC)].strip()
40
+ elif body.endswith(ASC):
41
+ descending = False
42
+ body = body[: -len(ASC)].strip()
43
+ return (body, descending) if body else None
44
+
45
+
46
+ def _anchor_style(cell) -> None:
47
+ cell.font = Font(name=FONT_NAME, size=FONT_SIZE, bold=True, color="1F4E79")
48
+ cell.fill = FILL_ANCHOR
49
+ cell.alignment = Alignment(horizontal="left", vertical="center")
50
+
51
+
52
+ def write_block_sort(ws, block: dict, column: str, descending: bool = True) -> None:
53
+ """把排序设置写进锚点行第一个空闲元数据列(列数格之后),已有设置则原位更新。"""
54
+ row = block["anchor_row"]
55
+ col0 = block.get("col0", 2)
56
+ target = None
57
+ for col in range(col0 + 4, META_SCAN_COLUMNS + 1):
58
+ text = "" if ws.cell(row, col).value is None else str(ws.cell(row, col).value).strip()
59
+ if text.startswith(SORT_PREFIX):
60
+ target = col
61
+ break
62
+ if target is None and text == "":
63
+ target = col
64
+ if target is None:
65
+ raise SyncError(f"{block['code']}:锚点行元数据列已满,无法写入排序设置")
66
+ cell = ws.cell(row, target, sort_setting_text(column, descending))
67
+ _anchor_style(cell)
68
+
69
+
70
+ def clear_block_sort(ws, block: dict) -> bool:
71
+ row = block["anchor_row"]
72
+ col0 = block.get("col0", 2)
73
+ cleared = False
74
+ for col in range(col0 + 4, META_SCAN_COLUMNS + 1):
75
+ cell = ws.cell(row, col)
76
+ text = "" if cell.value is None else str(cell.value).strip()
77
+ if text.startswith(SORT_PREFIX):
78
+ cell.value = None
79
+ cleared = True
80
+ return cleared
81
+
82
+
83
+ def block_headers(ws, block: dict) -> list[str]:
84
+ """表块首行(表头)各列显示文本,供界面选择金额列。"""
85
+ if not block["grid_rows"]:
86
+ return []
87
+ head = block["grid_rows"][0]
88
+ col0 = block.get("col0", 2)
89
+ return [
90
+ display_cell(ws.cell(head, col0 + c)).strip()
91
+ for c in range(block["ncols"])
92
+ ]
93
+
94
+
95
+ def _row_text(ws, row: int, col: int) -> str:
96
+ return display_cell(ws.cell(row, col)).strip()
97
+
98
+
99
+ def _amount(ws, row: int, col: int, cached) -> float | None:
100
+ cell = ws.cell(row, col)
101
+ raw = cell.value
102
+ if isinstance(raw, str) and raw.startswith("="):
103
+ raw = cached.get((row, col))
104
+ if raw is None:
105
+ raw = evaluate_formula(ws, row, col, cached)
106
+ if isinstance(raw, (int, float)) and not isinstance(raw, bool):
107
+ return float(raw)
108
+ if isinstance(raw, str) and NUMBER_RE.match(raw.strip()):
109
+ try:
110
+ return float(raw.strip().replace(",", ""))
111
+ except ValueError:
112
+ return None
113
+ return None
114
+
115
+
116
+ def _is_other(ws, row: int, ncols: int, col0: int = 2) -> bool:
117
+ if _row_text(ws, row, col0) == OTHER_ITEM:
118
+ return True
119
+ return ncols > 1 and _row_text(ws, row, col0 + 1) == OTHER_ITEM
120
+
121
+
122
+ def _remap_formula(text: str, rowmap: dict[int, int]) -> str:
123
+ """按旧行号→新行号映射改写公式引用;区间取映射后行号的最小/最大(保持求和范围不变)。"""
124
+
125
+ def fix_part(part: str) -> str:
126
+ if part.startswith('"'):
127
+ return part
128
+
129
+ def repl(m: re.Match) -> str:
130
+ if part[m.end():m.end() + 1] == "(": # LOG10( 这类函数名,不是单元格引用
131
+ return m.group(0)
132
+ if m.group(3) is not None:
133
+ r1, r2 = int(m.group(2)), int(m.group(4))
134
+ rows = [rowmap.get(r, r) for r in range(min(r1, r2), max(r1, r2) + 1)]
135
+ return f"{m.group(1)}{min(rows)}:{m.group(3)}{max(rows)}"
136
+ row = int(m.group(2))
137
+ return f"{m.group(1)}{rowmap.get(row, row)}"
138
+
139
+ return _RANGE_OR_REF_RE.sub(repl, part)
140
+
141
+ return "".join(fix_part(p) for p in re.split(r'("[^"]*")', text))
142
+
143
+
144
+ def apply_block_sort(ws, block: dict, cached: dict, dry_run: bool = False) -> dict | None:
145
+ """按表块保存的排序设置重排数据行(原地修改 ws)。无设置/无需改动时返回 None。"""
146
+ from .excel_blocks import block_merges
147
+
148
+ setting = parse_sort_setting(block.get("meta", {}).get("sort", ""))
149
+ if setting is None:
150
+ return None
151
+ column, descending = setting
152
+ code = block["code"]
153
+ rows = block["grid_rows"]
154
+ ncols = block["ncols"]
155
+ if len(rows) < 3:
156
+ return None
157
+ headers = block_headers(ws, block)
158
+ matches = [c for c, h in enumerate(headers) if h == column]
159
+ if not matches:
160
+ warn(f"[排序] {code}:找不到金额列「{column}」(表头可能已改),本次跳过排序")
161
+ return None
162
+ sort_col = block.get("col0", 2) + matches[0]
163
+ col0 = block.get("col0", 2)
164
+
165
+ data = rows[1:]
166
+ tail_start = next((i for i, r in enumerate(data) if _row_text(ws, r, col0).startswith("合计")), len(data))
167
+ sortable, tail = data[:tail_start], data[tail_start:]
168
+ if len(sortable) < 2:
169
+ return None
170
+ sort_idx = {r: i + 1 for i, r in enumerate(sortable)} # 相对块内网格的行号(表头为 0)
171
+ for (r, _c, rs, cs) in block_merges(ws, block):
172
+ if (rs > 1 or cs > 1) and any(r <= idx < r + rs for idx in sort_idx.values()):
173
+ warn(f"[排序] {code}:数据区含合并单元格,为避免错位本次跳过排序")
174
+ return None
175
+
176
+ normal = [r for r in sortable if not _is_other(ws, r, ncols, col0)]
177
+ others = [r for r in sortable if _is_other(ws, r, ncols, col0)]
178
+ parsed = [r for r in normal if _amount(ws, r, sort_col, cached) is not None]
179
+ unparsed_rows = [r for r in normal if r not in parsed]
180
+ unparsed = [_row_text(ws, r, col0) or f"第{sort_idx[r] + 1}行" for r in unparsed_rows]
181
+ ordered = sorted(parsed, key=lambda r: _amount(ws, r, sort_col, cached), reverse=descending)
182
+ new_data = ordered + unparsed_rows + others + tail
183
+ if new_data == data:
184
+ log(f"[排序] {code}:已是「{column} {DESC if descending else ASC}」顺序,无需改动")
185
+ return None
186
+ moved = sum(1 for old, new in zip(data, new_data) if old != new)
187
+ note = f"(其他 {len(others)} 行置底" + (f",{len(unparsed)} 行金额无法解析按最小值处理:{'、'.join(unparsed[:5])}" if unparsed else "") + ")"
188
+ if dry_run:
189
+ log(f"[排序·预演] {code}:按「{column}」{DESC if descending else ASC}重排,{moved} 行将移动{note},合计行不动")
190
+ return {"moved": moved, "others": len(others), "unparsed": unparsed}
191
+
192
+ snapshot = {}
193
+ for r in data:
194
+ for c in range(2, col0 + ncols):
195
+ cell = ws.cell(r, c)
196
+ snapshot[(r, c)] = (cell.value, copy(cell._style) if cell.has_style else None,
197
+ copy(cell.hyperlink) if cell.hyperlink is not None else None)
198
+ heights = {r: ws.row_dimensions[r].height for r in data if r in ws.row_dimensions and ws.row_dimensions[r].height is not None}
199
+ rowmap = {old: new for old, new in zip(data, new_data)}
200
+ for target, source in zip(data, new_data):
201
+ for c in range(2, col0 + ncols):
202
+ value, style, link = snapshot[(source, c)]
203
+ cell = ws.cell(target, c)
204
+ cell.value = value
205
+ if style is not None:
206
+ cell._style = style
207
+ if link is not None:
208
+ link.ref = cell.coordinate
209
+ cell.hyperlink = link
210
+ else:
211
+ cell.hyperlink = None
212
+ if source in heights:
213
+ ws.row_dimensions[target].height = heights[source]
214
+ ws.row_dimensions[target].customHeight = True
215
+ for r in data:
216
+ for c in range(2, col0 + ncols):
217
+ cell = ws.cell(r, c)
218
+ if isinstance(cell.value, str) and cell.value.startswith("="):
219
+ cell.value = _remap_formula(cell.value, rowmap)
220
+ cached_snapshot = {key: value for key, value in cached.items() if key[0] in rowmap}
221
+ for key in list(cached_snapshot):
222
+ del cached[key]
223
+ for (r, c), value in cached_snapshot.items():
224
+ cached[(rowmap[r], c)] = value
225
+ log(f"[排序] {code}:已按「{column}」{DESC if descending else ASC}重排 {moved} 行{note},合计行保持末行")
226
+ return {"moved": moved, "others": len(others), "unparsed": unparsed}
227
+
228
+
229
+ def carry_over_sort_settings(wb_new, ws_old, blocks_old: dict) -> list[str]:
230
+ """导出重建工作簿后,把旧表块的排序设置按书签名沿用到新表块。"""
231
+ from .excel_blocks import parse_blocks
232
+
233
+ ws_new = wb_new[SHEET_NAME]
234
+ _, blocks_new = parse_blocks(ws_new)
235
+ by_bookmark = {b["bookmark"]: b for b in blocks_new.values() if b.get("bookmark")}
236
+ carried = []
237
+ for block in blocks_old.values():
238
+ setting = parse_sort_setting(block.get("meta", {}).get("sort", ""))
239
+ bookmark = block.get("bookmark", "")
240
+ if setting is None or not bookmark or bookmark not in by_bookmark:
241
+ continue
242
+ write_block_sort(ws_new, by_bookmark[bookmark], setting[0], setting[1])
243
+ carried.append(by_bookmark[bookmark]["code"])
244
+ return carried