Skip to content

fix(xlsx): read and write PivotTable grand totals in Excel's direction - #1882

Merged
h-a-n-a-s-h-i merged 1 commit into
storytold:mainfrom
neilcawse:fix/pivot-grand-totals
Oct 19, 2026
Merged

h-a-n-a-s-h-i merged 1 commit into
storytold:mainfrom
neilcawse:fix/pivot-grand-totals

Conversation

@neilcawse

@neilcawse neilcawse commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

A PivotTable saved by Excel with only one grand total opened with the other one. rowGrandTotals was read into grand_totals_rows, which the model defines as the Grand Total column ("On for Rows"), and colGrandTotals into the Grand Total row. The writer used the same swapped mapping, so a GridCraft round trip hid the bug.

Split out of #722, as requested in review.

What changed

  • crates/xlsx/src/pivot.rs: rowGrandTotals now maps to grand_totals_cols (the Grand Total row under the report) and colGrandTotals to grand_totals_rows (the Grand Total column at its right), when reading and when writing. The model, the engine and the pivot.* commands are unchanged; they already used the meanings documented in crates/model/src/features.rs.
  • docs/file-format-parity.md: header and revision-history row.

Evidence

Two files from public test suites, both last saved by Excel (docProps/app.xml: "Microsoft Excel Online" 16.0300 and "Microsoft Excel" 15.0300):

File Attribute Excel's saved report Refresh on main Refresh with this PR
LibreOffice sc/qa/unit/data/xlsx/pivot/Pivot2_Row_Subtotals.xlsx (row fields only) rowGrandTotals="0" no Grand Total row adds a "Grand Total" row (600) no Grand Total row
ClosedXML TemplateWithTableSourcePivotTables.xlsx, pivotTable5 on StressedAssetsPivot (row and column fields) colGrandTotals="0" Grand Total row, no Grand Total column "Total Sum of …" columns, no Grand Total row Grand Total row, no Grand Total column

A scan of 3,075 corpus files saved by Excel found 20 PivotTables with exactly one of the two attributes off. In 14 of them the saved cells show the expected grand total: 12 have a Grand Total row and 2 a Grand Total column. All 14 match this mapping and none matches the old one. The other 6 show no grand total at all, because the axis that would carry it has no field, so they don't tell the two mappings apart.

Excel for Mac 16.114, on a synthetic workbook built in Excel: a PivotTable with Region in rows, Product in columns and Sum of Amount. Each option was chosen from PivotTable Design > Grand Totals with real clicks and then saved. Each file was then opened and refreshed (data.refreshAll) with this PR and with main (6e984bb).

Excel option Saved XML Excel shows (row at the bottom / column at the right) This PR main
On for Rows and Columns neither attribute both both both
On for Rows Only rowGrandTotals="0" column only column only row only
On for Columns Only colGrandTotals="0" row only row only column only
Off for Rows and Columns both ="0" neither neither neither

An earlier run on a PivotTable with no column field, with the setting made through Excel's scripting (RowGrand/ColumnGrand), agreed on the row side.

Tests

  • grand_totals_read_in_excel_direction (new) reads rowGrandTotals="0", colGrandTotals="0" and neither, and checks both model flags.
  • pivot_xml_details now also checks which attribute the writer emits for each of the two fixture pivots.
  • Both fail on main and pass with the fix.
  • cargo test -p gridcraft-xlsx -p gridcraft-engine passes (507 tests), and cargo xtask ci passes all 8 steps on 383bb7b.

Review

  • Round 1 (adversarial reviewer, on 383bb7b): ship. The reviewer rechecked the corpus hits, looked for other code that maps the two attributes or depends on the old mapping (other formats, the engine API, pivot charts, fixtures) and found none, and confirmed that both tests fail on main.
  • Round 2 (adversarial reviewer, on this description): the corpus sentence overclaimed (6 of the 20 hits show no grand total either way). Corrected, and the two fixtures' Excel provenance (docProps/app.xml) is now stated.
  • Excel check (above): the new description text and the reply had an adversarial review.

Summary by CodeRabbit

  • Bug Fixes
    • Corrected how PivotTable grand-total row and column settings are read from and written to Excel files. Workbooks that show only row totals or only column totals now retain the intended setting when opened or saved, instead of showing the other type of total.

rowGrandTotals is the Grand Total row under the report and colGrandTotals the
Grand Total column at its right. Both were mapped the other way round; a
GridCraft round trip hid it, but an Excel file with only one grand total opened
with the other one.
@coderabbitai

coderabbitai Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 929c9708-fe08-41a6-a742-f09878087be7
📥 Commits

Reviewing files that changed from the base of the PR and between 6e984bb and 383bb7b.

📒 Files selected for processing: 3
  • crates/xlsx/src/pivot.rs
  • crates/xlsx/src/tests/pivot_tests.rs
  • docs/file-format-parity.md

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

📝 Walkthrough

The XLSX PivotTable reader and writer now map row and column grand-total flags to the corresponding XML attributes. Tests cover XML output, input attributes, and defaults. The file-format parity history records the correction.

Changes: PivotTable grand-total mapping

Layer / File(s) Summary
Correct grand-total XML mapping
crates/xlsx/src/pivot.rs, crates/xlsx/src/tests/pivot_tests.rs, docs/file-format-parity.md
The reader and writer now map row and column grand-total flags to the corresponding XML attributes. Tests check output attributes, input mappings, and defaults. The revision history records the correction.

Priority: ➖ Normal

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

Change: Bug fix · Severity of issue fixed: Low

Suggested reviewers: echelon, vangel76

Merge Risk: ⚪ Minimal · up to 383bb

The change corrects how PivotTable grand totals are read and written, with tests covering both directions and defaults. No actionable merge-blocking risk is evident.

Security architecture risk: ⚪ Minimal · up to 383bb

The change corrects which PivotTable grand total appears when opening or saving a spreadsheet. The reviewed change introduces no new access, privileges, or execution capability.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The direct influence of the changed logic is confined to choosing the Grand Total row or column for a PivotTable in the processed workbook. The delta exchanges two Boolean attribute mappings and matching fixed output strings; it adds no new attacker-controlled target, identity selection, or privileged operation.
Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 2 files. (1 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely identifies the main change: correcting PivotTable grand-total reading and writing to match Excel's direction.
Full details: Docstring Coverage

Explanation

Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 2 files. (1 skipped: 1 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

neilcawse added a commit to neilcawse/gridcraft that referenced this pull request Oct 18, 2026
The rowGrandTotals/colGrandTotals fix moved to its own PR at review's request.
@neilcawse

Copy link
Copy Markdown
Contributor Author

An Excel check of the direction. I used Excel for Mac 16.114 on a synthetic workbook built in Excel: a PivotTable with Region in rows, Sum of Amount, and no column field. The screen was locked, so the grand totals were set through the object model (RowGrand/ColumnGrand), which the Design > Grand Totals menu sets, rather than through the ribbon itself.

Excel setting Saved XML Excel shows This PR (refresh) main 6e984bb (refresh)
On for Rows Only rowGrandTotals="0" no Grand Total row no Grand Total row Grand Total row
On for Columns Only colGrandTotals="0" Grand Total row (2100) Grand Total row no Grand Total row

With no column field, neither file has a Grand Total column, so this only tests the row side. I've added it to the Evidence section.

@neilcawse

Copy link
Copy Markdown
Contributor Author

A follow-up to my earlier comment: I repeated the check through the ribbon, this time with a column field.

I used Excel for Mac 16.114 on a synthetic PivotTable: Region in rows, Product in columns, Sum of Amount. Each option was chosen from PivotTable Design > Grand Totals with real clicks and then saved. I opened and refreshed each saved file in GridCraft:

Excel option Saved XML Excel shows This PR (383bb7b) main (6e984bb)
On for Rows and Columns neither attribute Grand Total row and column both both
On for Rows Only rowGrandTotals="0" Grand Total column only column only row only
On for Columns Only colGrandTotals="0" Grand Total row only row only column only
Off for Rows and Columns both ="0" neither neither neither

This covers the column side as well, and it replaces the scripting-only check in my earlier comment. The Evidence section is updated.

@h-a-n-a-s-h-i
h-a-n-a-s-h-i merged commit b77848e into storytold:main Oct 19, 2026
2 checks passed
@echelon

echelon commented Oct 19, 2026

Copy link
Copy Markdown
Member

Merged — thank you @neilcawse for the contribution!

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.

3 participants