Repository navigation
XLSX: a hidden Total Row keeps its calculations - #4383
Conversation
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
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
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 incrates/xlsx/src/sheet_write/parts.rswrote them onlyif 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,totalsRowLabeland customtotalsRowFormulawhether 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'sref,totalsRowShown="0"and the absence oftotalsRowCountare unchanged.QA report
Main vs branch, end to end (
gridcraft-cli convert)source.xlsxhas a three-column tableTable1(A1:C3, Group/Amount/Mean) made bygridcraft-cli run … insert.table. Its hidden total row was then patched to the issue's form:totalsRowLabel="Total";totalsRowFunction="custom"with<totalsRowFormula>SUM(B2:B3)+10</totalsRowFormula>;totalsRowFunction="average".Each build ran
convert source.xlsx saved.xlsx, and the savedxl/tables/table1.xmlwas inspected.mainis agridcraft-clibuilt fromorigin/main(abcfc191); the branch is this PR (8d8716cd).<table ref totals…>ref="A1:C3" totalsRowShown="0"ref="A1:C3" totalsRowShown="0"<tableColumn id="1" name="Group"/>… name="Group" totalsRowLabel="Total"/><tableColumn id="2" name="Amount"/>… name="Amount" totalsRowFunction="custom"><totalsRowFormula>SUM(B2:B3)+10</totalsRowFormula></tableColumn><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:=SUBTOTAL(109,Table1[Mean]), a Sum)=SUBTOTAL(101,Table1[Mean]), the Average)SUM(B2:B3)+10(see "Not covered")Test
tests::roundtrip::hidden_total_row_keeps_its_calculationscovers:totalsRowCount,refunchanged) and read back the same;totalsRowFunction,totalsRowLabelortotalsRowFormula.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_rowagain (on top of8d8716cd, then restored withgit 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)cargo fmt --all -- --checkcargo clippy -p gridcraft-xlsx --all-targets -- -D warningscargo test -p gridcraft-xlsx -p gridcraft-engineNot covered
table.totalRow(write_totalsincrates/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 onmaintoo, 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.cargo xtask ci: I didn't run the fullcargo xtask ci(wasm, layers, the other crates).