Skip to content

Table Total Row turned on again: a custom total is its formula, and the first column keeps its own - #4386

Open
Hamza0101 wants to merge 1 commit into
storytold:mainfrom
Hamza0101:fix/table-total-row-custom-formula
Open

Hamza0101 wants to merge 1 commit into
storytold:mainfrom
Hamza0101:fix/table-total-row-custom-formula

Conversation

@Hamza0101

@Hamza0101 Hamza0101 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Refs #4296 (found while testing #4383)

What was wrong

A table whose Total Row is turned off keeps each column's calculation. Excel writes it to the file, and GridCraft reads it into TableColumn::totals / totals_label. Turning the row back on with table.totalRow (table_flag and write_totals in crates/engine/src/cmd/insert/table.rs) didn't bring it back as Excel does:

  • A custom total became text. A totalsRowFormula such as SUM(B2:B3)+10 is kept in totals_label, and write_totals wrote it as a text label. B4 showed SUM(B2:B3)+10 instead of 25.
  • The first column always became "Total". Turning the row on overwrote its label (All groups), and wrote "Total" over a function it had (a Count).

This happens on main with any workbook read from such an Excel file. #4383 is separate: it keeps the calculation when GridCraft saves the file.

What changed

  • write_totals writes a custom column's total as its formula (= + totals_label). Built-in functions are SUBTOTAL(…) as before, and other labels stay text.
  • Turning the row on sets the first column's label to "Total" only when that column has neither a label nor a function. A table that never had a Total Row still gets "Total" and a Sum of its last column, as before.

QA report

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

source.xlsx has a table Table1 (A1:C3; Group/Amount/Mean; Amount 7, 8 and Mean 1, 2). Its Total Row is off, and its hidden totals are in the issue's Excel form:

  • Group: totalsRowLabel="Total";
  • Amount: custom SUM(B2:B3)+10;
  • Mean: average.

The table was made by gridcraft-cli, and its XML was patched by hand. Each build ran run --in source.xlsx --cmd 'table.totalRow={"table":"Table1","on":true}' --out, and the saved file was evaluated. main is a gridcraft-cli built from origin/main (abcfc191); the branch is this PR (8ee422bc).

Row 4 after turning the Total Row on main this PR Excel (#4296)
Amount (B4) the text SUM(B2:B3)+10, no formula 25, =SUM(B2:B3)+10 25
Mean (C4) 1.5 1.5 7.5 for its Average fixture
Group (A4) Total Total
Group, with totalsRowLabel="All groups" in the file Total All groups
a fresh table (Item/Qty, x 2, y 5; control) Total, 7 Total, 7

Test

tests::total_row_turned_on_again_restores_custom_totals_and_labels puts the hidden-row table into the open workbook, turns the row on and covers:

  • B4 is =SUM(B2:B3)+10 and 25, C4 is the Average 1.5, and A4 is "Total";
  • a first column labelled "All groups" keeps it;
  • a first column with Count gives 2;
  • a fresh table still gets "Total" and the Sum, 7.

The existing Total Row tests (tables_and_structured_refs, structured_refs_to_columns_with_edge_spaces, the last-row tests) still pass.

Negative controls

Each was an edit on top of 8ee422bc, run, then restored with git checkout:

Control Result
C1: a custom total written as text again the test fails at B4's formula: left Null, right "=SUM(B2:B3)+10"
C2: the first column always set to "Total" the test fails: left Text("Total"), right Text("All groups")

Gate (macOS arm64, at 8ee422bc)

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

Not covered

Summary by CodeRabbit

  • Bug Fixes
    • Re-enabling a table’s totals row now preserves its existing labels, formulas, and totals functions. Tables without prior totals still receive the default “Total” label and a sum in the last column.

table.totalRow wrote a custom column total (totalsRowFormula, kept in
totals_label) as a text label, so SUM(B2:B3)+10 showed as that text.
It is now written as the formula. Turning the row on also set the first
column's label to Total whatever it held; now only a first column with
no label or function of its own gets Total, so a label or function
kept from when the row was turned off comes back, as in Excel.
@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

When a table’s Total Row is enabled, the first column receives the default “Total” label only if it has no totals function or existing label. Custom totals formulas are written from nonblank labels, and a regression test covers restoring totals and default values.

Changes: Total Row restoration

Layer / File(s) Summary
Preserve and write totals-row values
crates/engine/src/cmd/insert/table.rs, crates/engine/src/tests.rs
The logic preserves an existing first-column label and writes nonblank custom totals formulas. The regression test covers restored custom formulas, labels, and functions, plus default totals for a table without prior totals.

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

Change: Bug fix

Possibly related issues

  • storytold/gridcraft#4296 — The issue describes custom formulas and averages not being restored when a Total Row is re-enabled. This change covers that turn-on path, but the issue also identifies a separate XLSX writer path that this change does not address.

Suggested reviewers: aiongg, prxnvt

Merge Risk: Minimal · up to 8ee42

Re-enabling a table's Total Row now restores custom totals as formulas and keeps the first column's existing label or function. The change is small and covered by a regression test, and no merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main behavior change: turning a table's Total Row back on now restores a custom total as a formula and keeps the first column's own label or function. It matches the raw summary an…
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.
Full details: Title check

Explanation

The title names the main behavior change: turning a table's Total Row back on now restores a custom total as a formula and keeps the first column's own label or function. It matches the raw summary and the PR objectives, which describe writing custom totals as formulas and preserving first-column content. The title is long (98 characters, 19 words) and uses a colon-separated phrasing, but it is specific and accurate.

Finishing Touches

Generate unit tests (beta)

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

Warning

Some tools did not complete. Review the errors below.

Clippy (1.99.1)

Clippy execution timed out

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 8ee422b.

Files selected for processing: 2

  • crates/engine/src/cmd/insert/table.rs
  • crates/engine/src/tests.rs

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

emrahoksak pushed a commit to emrahoksak/girdcraft- that referenced this pull request Nov 18, 2026

This branch has not been deployed

No deployments
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.

1 participant