Skip to content

XLSX: a hidden Total Row keeps its calculations - #4383

Merged
dylanmtaylor merged 1 commit into
storytold:mainfrom
Hamza0101:fix/table-hidden-total-row-keeps-calc
Nov 17, 2026
Merged

dylanmtaylor merged 1 commit into
storytold:mainfrom
Hamza0101:fix/table-hidden-total-row-keeps-calc

Conversation

@Hamza0101

@Hamza0101 Hamza0101 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4296

What was wrong

When a table's Total Row is turned off, Excel keeps each column's calculation in the table part, and writes no totalsRowCount:

  • totalsRowLabel="Total";
  • totalsRowFunction="custom" with its <totalsRowFormula>;
  • totalsRowFunction="average".

Turning the row back on in Excel restores them. GridCraft's reader (read_table) already keeps them for a hidden row. The writer in crates/xlsx/src/sheet_write/parts.rs wrote them only if t.totals_row, so a save dropped them. Excel then brought back its default =SUBTOTAL(109,[Amount]): 15 instead of the issue's custom 25, or the Average's 7.5.

What changed

The writer writes each column's totalsRowFunction, totalsRowLabel and custom totalsRowFormula whether the Total Row is shown or not. A shown Total Row is written as before. A table that never had one has no calculations, so it writes none. The table's ref, totalsRowShown="0" and the absence of totalsRowCount are unchanged.

QA report

Main vs branch, end to end (gridcraft-cli convert)

source.xlsx has a three-column table Table1 (A1:C3, Group/Amount/Mean) made by gridcraft-cli run … insert.table. Its hidden total row was then patched to the issue's form:

  • Group: totalsRowLabel="Total";
  • Amount: totalsRowFunction="custom" with <totalsRowFormula>SUM(B2:B3)+10</totalsRowFormula>;
  • Mean: totalsRowFunction="average".

Each build ran convert source.xlsx saved.xlsx, and the saved xl/tables/table1.xml was inspected. main is a gridcraft-cli built from origin/main (abcfc191); the branch is this PR (8d8716cd).

Saved table part main this PR
<table ref totals…> ref="A1:C3" totalsRowShown="0" ref="A1:C3" totalsRowShown="0"
Group <tableColumn id="1" name="Group"/> … name="Group" totalsRowLabel="Total"/>
Amount <tableColumn id="2" name="Amount"/> … name="Amount" totalsRowFunction="custom"><totalsRowFormula>SUM(B2:B3)+10</totalsRowFormula></tableColumn>
Mean <tableColumn id="3" name="Mean"/> … name="Mean" totalsRowFunction="average"/>

Turning the row back on in GridCraft (run --in saved.xlsx --cmd 'table.totalRow={"table":"Table1","on":true}'), row 4:

main's save this PR's save
Mean (C4) 3 (=SUBTOTAL(109,Table1[Mean]), a Sum) 1.5 (=SUBTOTAL(101,Table1[Mean]), the Average)
Amount (B4) empty the text SUM(B2:B3)+10 (see "Not covered")

Test

tests::roundtrip::hidden_total_row_keeps_its_calculations covers:

  • a hidden Total Row with a label, a custom formula and an Average, written exactly as above (no totalsRowCount, ref unchanged) and read back the same;
  • a table that never had a total row, which writes no totalsRowFunction, totalsRowLabel or totalsRowFormula.

The existing table tests (a shown Total Row, a header and total row only) still pass.

Negative control

With the writer gating the function and custom formula on t.totals_row again (on top of 8d8716cd, then restored with git checkout), the test fails at the Amount column: name="Amount" totalsRowFunction="custom"><totalsRowFormula>SUM(B2:B3)+10</totalsRowFormula></tableColumn> is missing from the written XML.

Gate (macOS arm64, at 8d8716cd)

Step Result
cargo fmt --all -- --check clean
cargo clippy -p gridcraft-xlsx --all-targets -- -D warnings clean
cargo test -p gridcraft-xlsx -p gridcraft-engine 1004 (+1 ignored) + 120 + 7 + 375 passed, 0 failed

Not covered

  • GridCraft's own Total Row toggle with a custom total. table.totalRow (write_totals in crates/engine/src/cmd/insert/table.rs) writes a custom column's formula as a text label, not a formula. Turning the row on also sets the first column's label to "Total" whatever it held. Both happen on main too, with any workbook read straight from such an Excel file; this PR only stops the save from losing the calculation. That's fixed separately in Table Total Row turned on again: a custom total is its formula, and the first column keeps its own #4386.
  • Excel: I didn't reopen the output in Excel. The table part was patched by hand to the issue's form.
  • cargo xtask ci: I didn't run the full cargo xtask ci (wasm, layers, the other crates).

A table whose Total Row was turned off still holds each column's total:
Excel writes totalsRowFunction, totalsRowLabel and a custom
totalsRowFormula without totalsRowCount, and restores them when the row
is turned back on. The reader took them, but the writer wrote them only
while the row was shown, so a save lost a custom formula or an Average
and Excel brought back SUBTOTAL(109,...) instead. They are now written
whether the row is shown or not.

Fixes storytold#4296
@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The XLSX table writer now preserves totals-row functions and labels when the totals row is hidden. A round-trip test checks those attributes, custom formulas, table range, and columns without total calculations.

Changes: Hidden totals-row calculations

Layer / File(s) Summary
Serialize and verify hidden totals-row calculations
crates/xlsx/src/sheet_write/parts.rs, crates/xlsx/src/tests/roundtrip.rs
The writer emits totals-row functions and labels even when the totals row is disabled. Custom formulas remain conditional on an enabled totals row. The test checks that calculations are restored, the table range is preserved, and columns without calculations omit total-row attributes.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: aiongg, dylanmtaylor

Merge Risk: Minimal · up to 8d871

XLSX saves now keep the calculations stored for a hidden table Total Row, so they come back when the row is shown again. The change is small and covered by a round-trip test, so merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "XLSX: a hidden Total Row keeps its calculations" matches the main change. The PR makes the XLSX table writer keep each column's totals-row function, label, and custom formula when the Total…
Linked Issues check Passed Issue #4296 requires that XLSX save keep each column's hidden Total Row calculation: totalsRowFunction (including custom and average), totalsRowLabel, and the custom totalsRowFormula, without totalsRo…
Out of Scope Changes check Passed The whole-PR diff touches only the table column writer in crates/xlsx/src/sheet_write/parts.rs, plus the roundtrip test in crates/xlsx/src/tests/roundtrip.rs. Every change in parts.rs is in table_xml …
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title "XLSX: a hidden Total Row keeps its calculations" matches the main change. The PR makes the XLSX table writer keep each column's totals-row function, label, and custom formula when the Total Row is hidden, and adds a round-trip test for that case. The title names the format, the hidden-row condition, and the preserved data, so a reviewer can identify the primary change from it.

Full details: Linked Issues check

Explanation

Issue #4296 requires that XLSX save keep each column's hidden Total Row calculation: totalsRowFunction (including custom and average), totalsRowLabel, and the custom totalsRowFormula, without totalsRowCount. The whole-PR diff (merge base abcfc19 to head 8d8716c) changes crates/xlsx/src/sheet_write/parts.rs table_xml. The totalsRowFunction and totalsRowLabel emission is no longer gated on t.totals_row. The custom formula gate is now totals == TotalsFn::Custom only. The totalsRowCount and totalsRowShown="0" logic is unchanged. A table with a shown Total Row still writes the same attributes. The test tests::roundtrip::hidden_total_row_keeps_its_calculations covers the hidden-row case and the no-total-row case. The PR's QA shows that main drops these attributes and this PR writes them. The Excel-side restore from the issue was not reopened in Excel by the author. That is a verification gap, not a code gap. The raw summary states that custom formulas remain gated on t.totals_row. The diff contradicts that, so the code diff is used as the evidence. The GridCraft engine toggle issue is out of scope for #4296 as the PR states.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff touches only the table column writer in crates/xlsx/src/sheet_write/parts.rs, plus the roundtrip test in crates/xlsx/src/tests/roundtrip.rs. Every change in parts.rs is in table_xml and serves the hidden Total Row calculation requirement of #4296. The test directly supports that requirement. The PR does not change the engine toggle (write_totals in crates/engine), which the PR explicitly identifies as a separate issue and does not modify. No unrelated change was found in the inspected diff. The roundtrip test file was not fully inspected, but the raw summary describes it as limited to the same table total-row round trip.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between abcfc19 and 8d8716c.

Files selected for processing: 2

  • crates/xlsx/src/sheet_write/parts.rs
  • crates/xlsx/src/tests/roundtrip.rs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 0 remain after this review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XLSX save drops the saved calculation for a hidden Table Total Row

2 participants