Repository navigation
Conversation
The total row of a table lived only in the table part: totalsRowLabel, totalsRowFunction and totalsRowFormula, with nothing in the sheet. Excel writes the row twice, in the table part and as ordinary cells, and reads the cells. Excel for Mac showed an empty total row for a built-in function, and asked to repair any table with a custom total, then dropped the total row. The worksheet writer now fills the total row the way Excel does: the label as a string, a built-in function as SUBTOTAL(10x,Name[Column]) on the displayName with [ ] # ' escaped, a custom total as its formula. A cell the caller already filled is left alone, so a workbook read back from Excel keeps its own cells and cached results. Those cells read back as ordinary cells; docs/PARITY.md says so. Four of the six new tests fail against main. Closes productdevbook#604
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
|
@coderabbitai review |
✅ Action performedReview finished.
|
Closes #604
What was wrong
A table with
showTotalRow: truegot its total row only in the table part (totalsRowLabel,totalsRowFunction,totalsRowFormulainxl/tables/table1.xml). The sheet's total row had no cells at all.In Excel for Mac 16:
totalFunction: "custom": Excel asks to repair the file, whether or not the formula uses newer functions. The repair deletes the total row, and the table becomesA1:B4.totalFunction: "sum": the file opens, but row 5 is empty. Neither the label nor the sum shows.Excel writes a total row twice. The table part describes it, and the sheet holds it as ordinary cells, which is what Excel reads. Here is what Excel for Mac wrote for the same table:
With a newer function, the
_xlfn.prefix is in both<totalsRowFormula>and the cell. #597 couldn't confirm in Excel that its total-row prefix worked, because any custom total row already needed repair.Why the tests did not catch it
They checked the table part's XML and round-tripped it through hucre's own reader. That reader reads
totalsRowFunctionandtotalsRowLabelback from the table part, so the round trip looked right. Nothing looked at the sheet's total row, and nothing opened the file in Excel. The one existing total-row test intables.test.tshid the problem: itsrowsalready include a hand-written["Total", 30]row.What landed
table-writer.tshas a newtotalRowCells(table, range)that returns the cells Excel writes:SUBTOTAL(n,Name[Column]), with Excel's function numbers (average 101, countNums 102, count 103, max 104, min 105, stdDev 107, sum 109, var 110);The reference uses the table's
displayName, as Excel does: a table named "Odd Tbl" becomesOdd_Tbl[...].[ ] # 'in a column name are escaped with', soa[b]becomesOdd_Tbl[a'[b']]andit'sbecomes[it''s]. Every one of these forms was checked against what Excel wrote.worksheet-writer.ts:resolveRowsmerges these cells into the grid. It places them on the last row of the table's range, or of the computed range when there is none. BothwriteXlsxandsaveXlsxgo throughresolveRows.A total-row cell the caller already filled is kept. A workbook read back from Excel already has these cells, with cached results.
openXlsx→saveXlsxon Excel's own files still writes row 5 once, with its<v>.No cached value is written. hucre always writes
fullCalcOnLoad, so Excel computes the total when it opens the file.docs/PARITY.mdhas a new section, "A table's total row comes back as cells". After a write and a read, the total row shows up inrowsandcellseven when the caller never wrote it.Tests: six new ones in
test/tables.test.ts. Four fail againstmain:SUBTOTAL(109,Table1[Qty])displayNameand the escaped column names, for a table placed at C3The two that pass on
maincheck what must not change: a total-row cell the caller filled is kept, and nothing is written for a column with no total or for a table withoutshowTotalRow.bun run testpasses, with 240 files and 10,704 tests.Checked in Excel
All in Excel for Mac 16. "Opens cleanly" means no repair prompt.
main, custom totalmain,sumtotalsumtotaltotals calculationas sumtotals calculationis customXLOOKUPtotalThe last row works because #597 adds the prefix when it writes a cell, so the total-row cell gets
_xlfn.with no code here. The two branches merge without conflicts.Not verified
writeXlsxStream. It does not write tables at all, so it is unchanged.TableDefinitionhas no option for one, so I didn't try it.totalsRowFunction.Summary by CodeRabbit
New Features
SUBTOTALformulas, while custom totals retain their specified formulas.Documentation