mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-21 11:11:59 +00:00
Excel 期 5:列表按期累积、穿插的小计、表格对象起草、CSV 走配方(E)
为什么:每月一份的明细导出是最常见的场景,期 4 之前列表只能每期替换;夹在中间的小计会让规则起草失败;
表格对象这个现成的边界信号没用上;CSV 不能走配方。四项都复用现有的执行器、核对、累积、溯源,不另起一套。
- 列表按期累积:ListBlock.period 二选一——{column, check: within_context} 按日期列分期,试运行多一条结构类核对
D(每行的日期都在本期统计期内,空着也不行);{synthesize: 统计期} 每行加「统计期起」「统计期止」两列,列表在
统计期定下来之前就抽完了,这两格先放占位、写库时换成真值。accumulate_blockers 放行绑定了统计期的列表;合计行、
小计行另存的表各期主键分不开,照旧不能累积;同一订单两期都出现由并集的主键核对拒收。U3 按列表的分期方式核对。
说明里「按哪一列筛选」用分期列,加的两列有列说明;空行之后文字行的说法补了并集版本(列表能累积以后要用到)。
- 穿插的小计:ListRows.subtotals {label_column, pick, scope: since_previous | group_column, group_column, keep_as}。
小计行不进数据,每个数字格一条合计格(分段键是块 id + ·小计),核对模块按它所管那组明细的 rowid 区间用 SQL
求和比对(新 T 编号,标题「小计行按各组明细求和」),整行记进排除的行(reason=subtotal),可以另存成
reported_total 表(小计项、分组取值、各数字列);数字格全空报 subtotal_blank(结构类);合计行的公式引用了小计格
时按它管的明细换算。静态校验:标签列、分组列要在列里,按组小计要给分组列,小计和合计的开头词不能互为前缀。
规则起草把第一列同一个合计字样、下面还有明细的行提议成小计(以前记 stray_totals 失败),框选同一条规则。
- 表格对象起草:工作表有表格对象时按它出列表块:表头行、列范围、数据行、汇总行(映射成 total_row,标签取汇总行
第一列的文字)都由表格对象给,锚点仍按表头文字、不记坐标;表名优先用表格对象的名字(含数字的不用)。不调用 AI。
- CSV 走配方:新增 csv_grid,按内容认出 CSV(不是 zip、不是 OLE、开头没有 NUL),编码和分隔符复用 tabular 读 CSV
的那一段,适配成一张名为「CSV」的工作表(SheetScan.path=csv:,WorkbookScan.format=csv);只有写法毫无歧义的
整数、小数转成数,千分位、前导零、日期写法一律留作文字交给配方的规则。坐标沿用「工作表!A1」(偏离细案的
R{行}C{列},理由见 csv_grid 的模块说明)。CSV 不做稀疏判定。导入向导和简单上传的结构提示都放行 CSV。
- 每个列表一道题「每期怎么更新」(q_list_period):每期替换 / 按期累积并按某个日期列核对 / 按期累积并加统计期
起止两列,后两种同时把导入模式改成按期累积、缺统计期的工作表一并配上(问题集合按草稿生成、回答时不变,草稿里
的列表还不符合累积资格,q_mode 不会出现,所以并成一道题)。确认项新增 subtotals:<块>、list_period:<块>。
- 前端配方面板:列表块多「小计行」(标签列、开头词、管哪些明细、分组列、另存)和「按期累积时每一行属于哪一期」;
回执「排除的行」认 subtotal。
验证:新增 tests/test_csv_grid.py、test_recipe_p5.py(执行器、核对、校验、资格、并集物化、起草)、
test_recipe_p5_e2e.py(经接口:三份月度 CSV 累积、部分重叠拒收、同一期替换要确认;带穿插小计的 Excel 起草、
核对、提交)。去掉小计识别、统计期回填、D 核对分别回跑,新测试 10 条失败。旧测试里「夹在中间的小计一律失败」
「列表只能每期替换」的断言按新行为改写。
This commit is contained in:
@@ -27,6 +27,9 @@ from app.data.names import canon
|
||||
from app.data.recipe_checks import date_columns, plan_checks, split_coord
|
||||
from app.data.recipe_parsers import UNITS
|
||||
from app.data.recipe_types import (
|
||||
LIST_PERIOD_END,
|
||||
LIST_PERIOD_START,
|
||||
SUBTOTAL_KEY,
|
||||
Acceptance,
|
||||
CheckResult,
|
||||
ColumnOut,
|
||||
@@ -46,6 +49,7 @@ from app.data.recipe_types import (
|
||||
TableNote,
|
||||
UnionNotePart,
|
||||
derive_tables,
|
||||
list_period_columns,
|
||||
prose_problems,
|
||||
render_prose,
|
||||
)
|
||||
@@ -134,6 +138,13 @@ NOTE_TEXT = {
|
||||
"ignored_rows_union": "部分期的原表另有按配方忽略的行,内容见证据面板",
|
||||
"ignored_cells_union": "部分期的原表另有按配方忽略的单元格,内容见证据面板",
|
||||
"dim_values_vary": "各期的 {dim} 取值不完全相同:跨期比较前请先确认两期都有的取值",
|
||||
# ---- 期 5:列表按期累积、穿插的小计。列表可以累积以后,空行之后的文字行也要有并集说法
|
||||
"rows_after_stop_union": "部分期的原表在空行之后另有未导入的文字行,内容见证据面板",
|
||||
"subtotal_kept": "原表穿插的小计在 {table},不要与本表相加",
|
||||
"subtotal_not_kept": "原表穿插的小计行未导入本表",
|
||||
"list_subtotal_passed": "原表穿插的小计行的值,已按各组明细求和核对一致:不要与 {base} 相加,也不要彼此相加",
|
||||
"list_subtotal_unverified": "原表穿插的小计行的值,按原表保存的值存储。本期部分小计未能核对:不要与 {base} 相加,"
|
||||
"也不要彼此相加",
|
||||
}
|
||||
|
||||
#: 列说明的片段模板
|
||||
@@ -148,6 +159,9 @@ COLUMN_TEXT = {
|
||||
"col_partial_periods": "部分期没有这一列的数据,为空值",
|
||||
"col_absent": "当前版本的数据中没有这一列,为空值",
|
||||
"col_retired": "这一列自某一期起不再导入,此后各期为空值",
|
||||
# ---- 期 5:列表按期累积时由系统加的统计期起止两列(ListPeriod.synthesize)。不含数字(说明不许报数)
|
||||
"period_start": "本期统计期的第一天,取自表内的统计期或人工录入的统计期,格式 YYYY-MM-DD",
|
||||
"period_end": "本期统计期的最后一天,取自表内的统计期或人工录入的统计期,格式 YYYY-MM-DD",
|
||||
}
|
||||
|
||||
#: 「逐日 / 逐行」「个别日期 / 个别行」:粒度只有一列日期时按日说,否则按行说
|
||||
@@ -161,7 +175,7 @@ _K_REASON = {r: f"total_k_{r}" for r in ("uncached", "blank", "null_detail", "ti
|
||||
_T_REASON = {r: f"list_total_{r}" for r in ("uncached", "blank", "null_detail", "tiling", "hidden")}
|
||||
#: 允许出现「一致」的模板:只在对应的 K / T 通过时才会选用
|
||||
CONSISTENT_KEYS = frozenset({"total_k_passed", "total_k_passed_g_unverifiable", "list_total_passed",
|
||||
"list_total_passed_partial"})
|
||||
"list_total_passed_partial", "list_subtotal_passed"})
|
||||
|
||||
|
||||
def _tok(kind: str, name: str) -> str:
|
||||
@@ -202,6 +216,8 @@ class _Writers:
|
||||
lists: list[ListBlock] = field(default_factory=list)
|
||||
#: 把合计行另存进本表的列表块
|
||||
list_totals: list[ListBlock] = field(default_factory=list)
|
||||
#: 期 5:把穿插的小计另存进本表的列表块
|
||||
list_subtotals: list[ListBlock] = field(default_factory=list)
|
||||
|
||||
def add_sheet(self, sheet: SheetRecipe) -> None:
|
||||
if all(s is not sheet for s in self.sheets):
|
||||
@@ -242,6 +258,9 @@ def _writers(recipe: Recipe) -> dict[str, _Writers]:
|
||||
total = block.rows.total_row
|
||||
if total is not None and total.keep_as:
|
||||
w(total.keep_as, sheet).list_totals.append(block)
|
||||
sub = block.rows.subtotals
|
||||
if sub is not None and sub.keep_as:
|
||||
w(sub.keep_as, sheet).list_subtotals.append(block)
|
||||
return out
|
||||
|
||||
|
||||
@@ -395,6 +414,15 @@ def _base_total_fragments(w: _Writers) -> list[NoteFragment]:
|
||||
b.rows.total_row is not None and not b.rows.total_row.keep_as for b in w.lists)
|
||||
if dropped:
|
||||
out.append(NoteFragment("total_not_kept", None, NOTE_TEXT["total_not_kept"]))
|
||||
# 期 5:穿插的小计行不在本表里:另存在哪,或者没导入
|
||||
for b in w.lists:
|
||||
sub = b.rows.subtotals
|
||||
if sub is None:
|
||||
continue
|
||||
if sub.keep_as:
|
||||
_add_unique(out, _frag("subtotal_kept", sub.keep_as, NOTE_TEXT["subtotal_kept"].format(table=_tab(sub.keep_as))))
|
||||
elif all(f.key != "subtotal_not_kept" for f in out):
|
||||
out.append(NoteFragment("subtotal_not_kept", None, NOTE_TEXT["subtotal_not_kept"]))
|
||||
return out
|
||||
|
||||
|
||||
@@ -450,6 +478,13 @@ def _kept_total_fragments(ctx: _Ctx, table: str, w: _Writers) -> list[NoteFragme
|
||||
else:
|
||||
frag = _frag(key, table, NOTE_TEXT[key].format(base=_tab(block.table)))
|
||||
_add_unique(out, frag)
|
||||
for block in w.list_subtotals:
|
||||
# 期 5:另存的小计:T 通过才说「核对一致」,没核对上的(无法核对)照实说部分未能核对;不一致的整份拒收
|
||||
t = ctx.check(table, ctx.plan.t.get(f"{block.id}{SUBTOTAL_KEY}"), f"块「{block.id}」的小计行")
|
||||
if t is None or _blocking(t):
|
||||
continue
|
||||
key = "list_subtotal_passed" if t.status == "passed" else "list_subtotal_unverified"
|
||||
_add_unique(out, _frag(key, table, NOTE_TEXT[key].format(base=_tab(block.table))))
|
||||
return out
|
||||
|
||||
|
||||
@@ -653,10 +688,17 @@ def _date_key(ctx: _Ctx, w: _Writers, table: str, col: ColumnOut) -> str | None:
|
||||
return None
|
||||
|
||||
|
||||
def _period_column_key(col: ColumnOut) -> str | None:
|
||||
"""期 5:系统加的统计期起止两列(role=period)用哪条列说明。"""
|
||||
if col.role != "period":
|
||||
return None
|
||||
return "period_start" if col.name == LIST_PERIOD_START else "period_end" if col.name == LIST_PERIOD_END else None
|
||||
|
||||
|
||||
def _null_eligible(ctx: _Ctx, w: _Writers, table: str, col: ColumnOut) -> bool:
|
||||
"""这一列要不要考虑空值说明:值列;主键不会是空值;表内合计表的空值已由表说明(未能核对)交代。"""
|
||||
value_col = col.role in ("measure", "value") and col.type in ("INTEGER", "REAL")
|
||||
is_total = bool(w.kept_from or w.list_totals)
|
||||
is_total = bool(w.kept_from or w.list_totals or w.list_subtotals)
|
||||
return value_col and not is_total and col.name not in set(ctx.grains.get(table, []))
|
||||
|
||||
|
||||
@@ -668,6 +710,8 @@ def _column_templates(ctx: _Ctx, table: str, null_counts: Mapping[str, Mapping[s
|
||||
frags: list[str] = []
|
||||
if (date := _date_key(ctx, w, table, col)) is not None:
|
||||
frags.append(COLUMN_TEXT[date])
|
||||
if (period := _period_column_key(col)) is not None:
|
||||
frags.append(COLUMN_TEXT[period])
|
||||
if col.unit:
|
||||
frags.append(COLUMN_TEXT["unit"].format(unit=_tok("单位", col.unit)))
|
||||
has_nulls = counts is None or counts.get(col.name, 0) > 0
|
||||
@@ -793,22 +837,27 @@ _FAMILY: dict[str, str] = {
|
||||
"hidden_excluded": "hidden_excluded", "hidden_included": "hidden_included",
|
||||
"ignored_columns": "ignored_columns", "ignored_rows": "ignored_rows", "ignored_cells": "ignored_cells",
|
||||
"rows_after_stop": "rows_after_stop",
|
||||
"subtotal_kept": "subtotal_kept", "subtotal_not_kept": "subtotal_not_kept",
|
||||
"list_subtotal_passed": "list_total", "list_subtotal_unverified": "list_total",
|
||||
"period_conflict_accepted": "period_conflict", "period_conflict_pending": "period_conflict",
|
||||
"period_human": "period_human", "outside_digits": "outside_digits",
|
||||
"note": "note",
|
||||
}
|
||||
#: 合并后的拼接顺序(与 _table_fragments 一致;累积特有的几句排在用户写的表说明之前)
|
||||
_FAMILY_ORDER = ("grain", "sum_eq", "not_comparable", "hour_range", "const", "total_kept", "total_not_kept", "total_k",
|
||||
"list_total", "total_overlap", "hidden_excluded", "hidden_included", "ignored_columns",
|
||||
_FAMILY_ORDER = ("grain", "sum_eq", "not_comparable", "hour_range", "const", "total_kept", "total_not_kept",
|
||||
"subtotal_kept", "subtotal_not_kept", "total_k", "list_total", "total_overlap", "hidden_excluded", "hidden_included", "ignored_columns",
|
||||
"ignored_rows", "ignored_cells", "rows_after_stop", "period_conflict", "period_human",
|
||||
"outside_digits", "union", "note")
|
||||
#: 与核对结论无关、只描述表结构的片段族:用目标配方那一期的;只在早期出现的 subject(退役的表、另存表)用
|
||||
#: 最后一个含它的那一期的
|
||||
_STRUCTURAL = frozenset({"grain", "hour_range", "const", "total_kept", "total_not_kept", "total_overlap", "note"})
|
||||
_STRUCTURAL = frozenset({"grain", "hour_range", "const", "total_kept", "total_not_kept", "total_overlap", "note",
|
||||
"subtotal_kept", "subtotal_not_kept"})
|
||||
#: 「任一期出现就写」的片段族 → 并集里的说法
|
||||
_ANY_UNION = {"hidden_excluded": "hidden_excluded_union", "hidden_included": "hidden_included_union",
|
||||
"ignored_columns": "ignored_columns_union", "ignored_rows": "ignored_rows_union",
|
||||
"ignored_cells": "ignored_cells_union", "outside_digits": "outside_digits_union"}
|
||||
"ignored_cells": "ignored_cells_union", "outside_digits": "outside_digits_union",
|
||||
# 期 5:列表可以按期累积了,空行之后的文字行任一期有就写
|
||||
"rows_after_stop": "rows_after_stop_union"}
|
||||
_K_PASSED = frozenset({"total_k_passed", "total_k_passed_g_unverifiable"})
|
||||
|
||||
|
||||
@@ -943,20 +992,26 @@ def _merge_group(table: str, family: str, subject: str | None, group: dict[int,
|
||||
if family == "period_human":
|
||||
key = "period_human_all" if len(present) == len(having) else "period_human_union"
|
||||
return NoteFragment(key, subject, NOTE_TEXT[key])
|
||||
# list_total、rows_after_stop 只有列表才有,而列表不能按期累积(P3-SPEC 2.2);不认识的片段族同样没有并集说法。
|
||||
# list_total 只在列表的合计行、小计行另存的表上,这种表不能按期累积(accumulate_blockers);不认识的片段族同样
|
||||
# 没有并集说法。
|
||||
# 记 problems(不能保存),绝不沿用某一期的单期说法。消息给人看,不露片段族的键名
|
||||
problems.append(f"表「{table}」:{_UNMERGEABLE.get(family, '有一段说明')}不能按期合并,无法生成说明")
|
||||
return None
|
||||
|
||||
|
||||
#: 没有并集说法的片段族 → 给人看的叫法(problems 里不露键名)
|
||||
_UNMERGEABLE = {"list_total": "列表合计行的核对结论", "rows_after_stop": "列表空行之后的文字行"}
|
||||
_UNMERGEABLE = {"list_total": "列表合计行、小计行的核对结论"}
|
||||
|
||||
|
||||
def _axis_column(recipe: Recipe, table: str) -> str | None:
|
||||
"""各期按哪一列区分(说明里「请按 {col} 筛选」):交叉表的日期轴;期 5 起列表按 period 绑定的日期列,或者
|
||||
系统加的统计期起那一列。"""
|
||||
tables, _ = derive_tables(recipe)
|
||||
axes = [c.name for c in tables.get(table, []) if c.role == "axis"]
|
||||
return axes[0] if len(axes) == 1 else None
|
||||
if len(axes) == 1:
|
||||
return axes[0]
|
||||
spec = list_period_columns(recipe).get(table)
|
||||
return spec[1] if spec else None
|
||||
|
||||
|
||||
def _union_table_fragments(table: str, having: list[int], parts: list[_UPart], ref: int, target: Recipe,
|
||||
@@ -1037,6 +1092,8 @@ def _union_column_templates(table: str, columns: list[ColumnOut], having: list[i
|
||||
frags.append(COLUMN_TEXT["date_year_from_period"])
|
||||
elif "date" in dates or (not with_col and col.role == "axis"):
|
||||
frags.append(COLUMN_TEXT["date"])
|
||||
if (period := _period_column_key(col)) is not None:
|
||||
frags.append(COLUMN_TEXT[period])
|
||||
if target_cols is not None and col.name in target_cols:
|
||||
unit = target_cols[col.name].unit
|
||||
elif with_col:
|
||||
|
||||
Reference in New Issue
Block a user