Skip to content

Fix #42: extend pivot cache preservation to eliminate Excel repair warning#53

Open
senoff wants to merge 1 commit intoprotobi:masterfrom
senoff:fix-42-pivot-cache-repair
Open

Fix #42: extend pivot cache preservation to eliminate Excel repair warning#53
senoff wants to merge 1 commit intoprotobi:masterfrom
senoff:fix-42-pivot-cache-repair

Conversation

@senoff
Copy link
Copy Markdown

@senoff senoff commented May 1, 2026

Closes #42.

Summary

Programmatically-generated pivot tables previously emitted fully-enumerated <sharedItems> for every source field, including non-axial columns (value fields and unused columns like long-text "Description"). On open, Excel flagged the file with:

Repaired Records: PivotTable report from /xl/pivotCache/pivotCacheDefinition1.xml part (PivotTable cache)

This refines the existing pivot-cache hybrid-preservation work shipped in 4.4.0-protobi.9 (#41) by scoping sharedItems enumeration to fields actually used as pivot axes (rows / columns / pages). Non-axial fields now emit a lightweight <sharedItems .../> declaration with type hints from column inspection, and pivotCacheRecords inlines their values (<n>/<s>/<m>) rather than referencing them via <x v="N"/>.

Also corrects a related bug in pivotCacheDefinition: recordCount was cacheFields.length + 1 when it should match the data row count declared on <pivotCacheRecords count="...">. Excel's strict cache validator flags the mismatch.

Root cause

lib/doc/pivot-table.js called makeCacheFields(sourceSheet, allHeaderNames) — every source column got sharedItems aggregated. For a 1500-row workbook with a long-text "Description" column not used in the pivot, this produced ~915 KB of irrelevant <sharedItems> content in the cache definition (and triggered the repair warning). Only fields actually projected onto pivot axes need indexable shared items; everything else can be inlined into the records part.

Change shape

  • lib/doc/pivot-table.js: pass only axial field names (rows + columns + pages) to makeCacheFields; inspect non-axial columns for type hints (containsBlank, containsString, containsNumber, containsInteger, longText).
  • lib/xlsx/xform/pivot-table/cache-field.js: when sharedItems === null, render <sharedItems .../> with hint-derived attributes only — no enumeration.
  • lib/xlsx/xform/pivot-table/pivot-cache-definition-xform.js: derive recordCount from sourceSheet.getSheetValues().length - 2 to match the records count.
  • spec/integration/issues/issue-42-pivot-cache-repair.spec.js: 8 new regression assertions.

pivotCacheRecords's existing renderCell already handles sharedItems === null by inlining <n> / <s> / <m> values, so no change was needed there.

Result on the issue's reference fixture

Metric Before After
pivotCacheDefinition1.xml size ~915 KB ~4 KB
recordCount 6 1500
Excel repair warning on open yes (expected: no)

Test plan

  • npm run test:unit — 884 passing
  • npm run test:integration — 208 passing (12 pivot tests, including 8 new for [BUG] Pivot cache triggers Excel repair #42)
  • npm run test:end-to-end — 1 passing
  • Existing pivot integration tests (pivot-tables.spec.js, pivot-tables-with-count.spec.js) still green — no regressions
  • Open the resulting .xlsx from the new spec in Excel and confirm no repair prompt (soffice was not available on the dev machine — byte-shape assertions used as a proxy; happy to add a soffice --headless --convert-to xlsx smoke step in a follow-up if useful)

Notes for review

  • The fix is limited to the programmatic pivot path. Round-trip preservation (loading an existing pivot via _processPivotCache* and re-emitting raw) is untouched; that path already passes through verbatim XML.
  • cache-field.js is conservative about declaring type attributes on lightweight sharedItems: it only emits containsNumber="1" etc. when the column is unambiguously numeric. Mixed-type columns get a bare <sharedItems/> (or with containsBlank/longText hints) and Excel infers from the inline records — this avoids attribute/record disagreement which is itself a repair trigger.
  • A small amount of pre-existing-but-flagged lint in the touched files was cleaned up so the pre-commit hook passes (no --no-verify used): a redundant comma after a thrown-error message and an unused destructured defaultValue. Both are cosmetic.

🤖 Generated with Claude Code

…d Excel repair

Programmatically-generated pivot tables previously emitted fully-enumerated
<sharedItems> for every source field, including non-axial columns (value
fields and unused columns like long-text "Description"). On open, Excel
flagged the file with:

  "Repaired Records: PivotTable report from /xl/pivotCache/
   pivotCacheDefinition1.xml part (PivotTable cache)"

This refines the fork's existing pivot-cache hybrid-preservation work
(4.4.0-protobi.9, protobi#41) by scoping sharedItems enumeration to fields
actually used as pivot axes (rows/columns/pages). Non-axial fields now
emit a lightweight <sharedItems .../> declaration with type hints from
column inspection; pivotCacheRecords inlines their values (<n>/<s>/<m>)
rather than referencing them via <x v="N"/>.

Also corrects a related bug in pivotCacheDefinition: recordCount was
cacheFields.length+1 when it should match the data row count declared
on <pivotCacheRecords count="...">.

Files:
- lib/doc/pivot-table.js: pass only axial field names to makeCacheFields;
  inspect non-axial columns for type hints.
- lib/xlsx/xform/pivot-table/cache-field.js: render lightweight
  sharedItems with hint-derived attributes when sharedItems is null.
- lib/xlsx/xform/pivot-table/pivot-cache-definition-xform.js: derive
  recordCount from source row count.
- spec/integration/issues/issue-42-pivot-cache-repair.spec.js: 8 new
  regression assertions covering definition shape, recordCount, records
  inlining, file size, and round-trip reload.

Test results: 884 unit + 208 integration + 1 end-to-end passing.
On the issue's 1500-row reference fixture, pivotCacheDefinition shrinks
from ~915 KB to ~4 KB; recordCount corrected from 6 to 1500.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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.

[BUG] Pivot cache triggers Excel repair

1 participant