Skip to content

PivotTables: reset stale manual order and deduplicate imported item references - #2430

Draft
addibbi wants to merge 39 commits into
storytold:mainfrom
addibbi:fix/pivot-order-followup
Draft

addibbi wants to merge 39 commits into
storytold:mainfrom
addibbi:fix/pivot-order-followup

Conversation

@addibbi

@addibbi addibbi commented Oct 24, 2026 •

Copy link
Copy Markdown

Imported PivotTables can retain obsolete manual ranks after a source-order reset or a grouping change, while repeated cache references can multiply saved ranks. This draft remains a targeted follow-up to #722.

  • pivot.sort with order: "source" clears saved manual ranks; none preserves them. Source order is a GridCraft extension, distinct from Excel ascending order.
  • Changing a date grouping clears stale ranks; reapplying the same grouping preserves intentional manual order.
  • Imported references are validated and deduplicated before labels are copied; the first valid reference wins.

This draft includes #722 at a05d381c5cbffc5f159cb3dc2b438a7961a4da71, retaining its author history. The integration corrects pivot calculation and test compilation issues in that head and checks GETPIVOTDATA through a source-order reset, undo, refresh and XLSX reopen. This PR remains in draft until its dependency is integrated.

Validation — current head (2026-11-17)

  • Head 76d83a12e02e685acfbdb022ae56f02e457cd69c has the exact tree checked before commit and push. Local Linux arm64 CI passed all 10 gates, with 4,007 tests passed, 0 failed and 5 ignored. The twelve selected pivot and XLSX checks passed, including the extended GETPIVOTDATA regression.
  • GitHub workflow runs require approval on this head. The local run is independent of GitHub Actions.

Historical validation (2026-11-02)

  • Head: b56129af215c67653e3745599a62953d2e00f9d0; includes main 488f9982ebf05be9bfe581489d0edc6e3918eec7.
  • Separate local Linux arm64 CI in Docker, before this push: cargo --offline --locked xtask ci; 10/10 gates, 3020 tests passed, 0 failures, 1 pre-existing ignored test. All 12 selected regression checks were observed passing in the complete run. No tests were excluded.
  • A dedicated target was used, with all workspace packages and xtask rebuilt. Source was mounted read-only, its tree was checked against the committed tree, and CI-pinned craft-fonts b076417b5880bd4154bd22f00695b1d28c6bea83 were hash-verified and required.
  • These are local Linux results, separate from GitHub Actions and CodeRabbit. Previous-head reviews and CI results are not attributed to this head.

neilcawse and others added 20 commits October 13, 2026 13:42
…yout settings

A refreshed PivotTable read from an Excel file now lays out like Excel's:
- Sort by a value field (autoSortScope on data field n): PivotField.sort_by, applied within
  each parent; pivot.sort takes `by: "Sum of X"`.
- Captions: rowHeaderCaption, colHeaderCaption, dataCaption, grandTotalCaption and
  missingCaption (For empty cells show), field custom names and renamed items (item n).
- Manual item order of fields that aren't auto-sorted.
- Per-field subtotals (defaultSubtotal, with the table default in pivotTableDefinition16),
  Repeat Item Labels (fillDownLabels), field number formats, the Values row
  (hideValuesRow) and the classic layout (gridDropZones).
- rowGrandTotals/colGrandTotals were read and written the wrong way round.
- Date items sort after text, as in Excel.

All settings are written back to XLSX.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Sort by the values in a column (autoSortScope references naming items of the
  other axis): read, written back and used by the engine, instead of sorting by
  the grand total. A scope GridCraft can't represent loads as a sort by label,
  with a warning.
- Changing subtotals in Design replaces each field's own subtotal setting.
- A numeric "For empty cells show" caption (Excel stores missingCaption="0")
  is written as a number in the value's format.
- With field headers off there's no Values row, so firstDataRow matches.
- A field that isn't auto-sorted keeps its <items> order even when it is cache
  order (date groups only when reordered).

Items tied in a sort by value still break ties by label in the sort
direction: over 60 tie groups in the 11 Excel-saved reports no stored order
(pivotField items, sharedItems, first appearance, reversed or not) explains
more than 21, the same as the current rule.
…te order of date groups

- Sort scope entries name their field by source column and date grouping
  (PivotScopeItem), so Years and Quarters of one date column stay apart when
  reading, computing and writing.
- The engine and the writer resolve a scope the same way: a scope naming an
  item that's no longer there sorts by the grand total, as it is then saved.
- A field with custom subtotal functions (sumSubtotal...varPSubtotal) has
  subtotals on; it shows the automatic subtotal, with a warning.
- Date groups that aren't sorted show in date order, the order of the cache's
  group items, instead of source order.
…items

Excel writes a pivot cache's date items as ISO dates counting real days from
1899-12-30: a time is 1899-12-30T08:00:00 and serial 1 is 1899-12-31. GridCraft
read them with DATE() rules, which move year 1899 on to 3799, so those items
never matched the report's items. A refresh then put them after every item that
did match, such as (blank), and saving wrote that wrong order into the file.

ISO dates (pivot cache items, t="d" cells) now go through a calendar-date
conversion that counts real days from the epoch, accepts 1900-02-29 as serial
60, and in the 1904 system takes 1899-12-30 as a time's date part. The cache
writer uses the inverse, so serial 1 is written as 1899-12-31 as Excel does.
DATE() keeps its own rules.
The ascending item order put dates between text and TRUE/FALSE. Excel's item
lists put numbers, text, logicals, errors, dates and then blanks: in the one
Excel-saved cache in the test corpus whose fields mix dates with logicals or
errors (ClosedXML VariousDataTypesInTableColumns), the Mixed Types field lists
10, 14.5, ABC, TRUE, #DIV/0!, 16:30, 8/10/2020, (blank), and the DateTime with
num field puts its numbers before its dates. The sort classes follow that order.

Also adds the regression test for the time items read from an Excel cache.
The previous commit changed the shared ISO date parser, so worksheet t="d"
cells dated before March 1900 also read one day later. The evidence for the
real-day convention comes from Excel's pivot caches only, so worksheet cells
read exactly as before again. The parser takes the date conversion as a
parameter: worksheet cells keep DATE() rules, pivot cache items use the
real-day conversion that the cache writer mirrors.
The date sort class came from the number format of the column's first data
cell. Excel decides per cell (it caches date-formatted cells as <d> and General
ones as <n>), so a General first cell made every date sort as a number, and a
date-formatted first cell made General numbers sort as dates. The source now
records, per cell, whether its own format is a date, and the class follows that.
Reading every source cell's style made each refresh of a large source much
slower. The source now keeps the sheet row of each record, and the per-cell
date flags are read only for ungrouped row and column fields, one format lookup
per distinct style.

One number in a date cell and in a General cell is one item; its sort class
depended on which cell came first. Any General occurrence now makes it a
number.
… number

The General-wins rule ran in item interning, which only sees records that pass
the report filter, so filtering out the General cell made its number sort as a
date. The date flags are now decided over every record of the column before
filtering, as Excel's cache decides.
date_cells hashed every non-date number of a column into the General set even
when no cell of the column was a date, which a numeric General row field paid
for on every refresh. The set is now built only when some flag is set.
The rowGrandTotals/colGrandTotals fix moved to its own PR at review's request.
With values on rows in Outline or Tabular, the Σ Values column header showed
"Values" even when the file sets dataCaption; Excel shows the caption.
Integrate current main into the storytold#722 dependency branch. Explicit source
order and changed date grouping discard stored manual ranks; repeated
or invalid cache references do not duplicate the imported item order.
Cover both axes, Undo/Redo, dependent formulas and XLSX round trips.

Co-Authored-By: GPT-6.1 Sol <noreply@openai.com>
Co-Authored-By: GPT-6 Astra <noreply@openai.com>
@coderabbitai

coderabbitai Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • 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

addibbi and others added 6 commits October 25, 2026 09:34
Preserve the storytold#722 dependency and the source-order, regrouping and
XLSX deduplication fixes alongside upstream Show Details guards.
Add a synthetic regression covering manual/source order transitions
and Show Details records and its disabled state.

Co-Authored-By: GPT-6.1 Sol <noreply@openai.com>
Merge f6b0cec, including main f6b0cec.
Preserve the contribution's existing behavior, regression coverage and safeguards.

Validation before commit: separate cargo --offline --locked xtask ci,
10/10 gates; 2551 passed, 0 failed, 1 pre-existing ignored.
All 12 selected checks passed on tree 81faba9.
CI-pinned craft-fonts b076417b5880bd4154bd22f00695b1d28c6bea83, required mode.

Co-Authored-By: GPT-6.1 Sol <noreply@openai.com>
Merge 45f5ab1, including main 45f5ab1.
Preserve the contribution scope and safeguards while porting it to upstream module layout.

Validation before commit: separate cargo --offline --locked xtask ci,
10/10 gates; 2551 passed, 0 failed, 1 pre-existing ignored.
All 12 selected checks passed on tree 3c08422.
CI-pinned craft-fonts b076417b5880bd4154bd22f00695b1d28c6bea83, required mode.

Co-Authored-By: GPT-6.1 Sol <noreply@openai.com>
@Harrison-Booth

Copy link
Copy Markdown
Contributor

Review notes (comment only; nothing here changes the PR's status). The PR is a draft that carries #722's history, so I looked at what it adds: pivot.sort with order: "source" clearing the saved manual order, the reset on a changed date grouping, and the first-valid-occurrence dedupe of cache item references in xlsx/src/pivot/read.rs (with its malformed-cache test).

Read the diff. The reader change is bounded and drops invalid indices rather than indexing (x="99" and x="4294967295" in the test), which is the right never-crash handling. The docs paragraph matches the code.

Checked in real Excel (desktop, COM, a new temporary workbook, closed without saving). A table with Item values b, a, c, a, b, c (so first appearance is b, a, c) and a PivotTable with Item as a row field:

  • A new report lists the items a, b, c (ascending), not in first-appearance order b, a, c.
  • Moving c to position 1 gives c, a, b and the field's sort reads manual (AutoSortOrder = xlManual, -4135). AutoSort(xlManual) leaves that order as it is.
  • AutoSort(xlAscending) gives a, b, c; calling AutoSort(xlManual) afterwards keeps a, b, c: it does not return to the earlier manual order c, a, b, and there is no way back to source order. Moving b to position 1 then gives b, a, c, and a RefreshTable() keeps it.
    So Excel has no "source order" for an ordinary PivotTable field (that choice exists for OLAP data only), and its own baseline for items nobody moved is ascending order. Two consequences for this PR:
  1. order: "none"/"manual" retaining the saved order matches Excel's manual sort.
  2. order: "source" clears the saved order and the items then fall back to "source order (first appearance)" (the PivotSort::None doc comment). For the table above that is b, a, c, whereas a freshly created Excel report shows a, b, c. If the goal is to look like Excel after the user has dragged items around and wants to start again, Asc is what Excel's own list shows; "source" is a GridCraft option rather than an Excel one. Worth saying in the docs, or naming the option so it is not read as an Excel equivalent.

Not checked. I did not get the date-grouping part of Excel to run (my grouping call did not create a Months field), so the claim that regrouping discards the old ranks is not compared with Excel. I did not build or run the tests (12 required checks per the description), and the Show Details and XLSX round-trip parts were read, not run.

Respond to the reported Excel COM observations using synthetic data only.
Documentation only; retain behavior, validation and protection checks.

Separate local cargo --offline --locked xtask ci before commit/push:
10/10 gates; 2551 passed, 0 failed, 1 pre-existing ignored.
All 12 selected tests passed on tree 7b52e3f.
CI-pinned required fonts verified; no GitHub CI execution is claimed.
@addibbi

addibbi commented Oct 28, 2026

Copy link
Copy Markdown
Author

@Harrison-Booth Thank you for the sorting comparison.

Clarified the documentation and PR description in 937ca5ce: order: "source" is a GridCraft extension, not an Excel-equivalent reset for ordinary non-OLAP PivotTables. For source b, a, c, a, b, c, it returns b, a, c; order: "asc" yields a, b, c. none/manual retain the saved order.

The note attributes manual/ascending results to your Excel COM checks, and explicitly leaves date regrouping unmeasured in Excel. GridCraft's regrouping behavior has synthetic regression coverage; it is not described as verified Excel parity. This update is documentation only: sorting, cache-reference deduplication and Show Details protections are unchanged. The PR remains a draft follow-up to #722.

Separate local macOS cargo --offline --locked xtask ci before this commit/push passed 10/10 gates, 2,551 tests, 0 failures and 1 pre-existing ignored test. All 12 selected regressions/controls are present and passing in the full log. The run used an independent target and temporary directory, RUST_TEST_THREADS=1, and the upstream pinned, hash-verified required craft-fonts input. No tests or checks were disabled. These are local results, not a claim that GitHub Actions has run.

We have not independently rerun your Excel COM experiments.

@Harrison-Booth

Copy link
Copy Markdown
Contributor

I fetched this head and ran git merge-tree --write-tree upstream/main upstream/pr-2430. It reports content conflicts in crates/engine/src/cmd/pivot/tests.rs, crates/engine/src/pivot/compute.rs, and crates/xlsx/src/pivot/write.rs. The PR is described as a follow-up to still-open #722, which also currently reports a dirty merge state. I did not resolve or test a combined merge, run Rust tests, or check behavior in Excel.

neilcawse and others added 12 commits October 30, 2026 11:51
…at d5cdd4b

Keep the upstream parity descriptions and the pivot order follow-up entry.

Local Linux arm64 CI passed on the exact tree. 10/10 gates, 3633 tests passed, 0 failed and 4 ignored.
…idelity

# Conflicts:
#	crates/engine/src/pivot.rs
#	crates/engine/src/pivot/compute.rs
#	crates/engine/src/pivot/output.rs
…idelity

# Conflicts:
#	crates/xlsx/src/tests/pivot_tests.rs
Keep source-order reset, regrouping, and cache reference deduplication on the updated dependency.

Linux arm64 CI: 10/10 gates passed, 3914 tests passed, 0 failed, 4 ignored.
…idelity

# Conflicts:
#	crates/engine/src/pivot/compute.rs
#	crates/model/src/features.rs
#	crates/xlsx/src/tests/pivot_tests.rs
#	docs/target-app-parity.md

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.

3 participants