feat: Support multiple pivot tables from same source data#2995
Open
protobi-pieter wants to merge 2 commits intoexceljs:masterfrom
Open
feat: Support multiple pivot tables from same source data#2995protobi-pieter wants to merge 2 commits intoexceljs:masterfrom
protobi-pieter wants to merge 2 commits intoexceljs:masterfrom
Conversation
Changed splice() to slice() in makeCacheFields() to prevent mutating source worksheet column data. Previously, splice() modified the original array, causing second and subsequent pivot tables to fail with corrupted field data. This one-character fix ensures each pivot table independently reads fresh data from the source worksheet. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
…tion Fixed four distinct bugs that prevented multiple pivot tables from displaying their own field configurations: 1. State mutation: Changed splice() to slice() in makeCacheFields() to prevent corrupting source worksheet column data 2. Duplicate UIDs: All pivot tables had hardcoded identical UUID, causing Excel to treat them as the same table. Now generates unique UUID for each pivot table 3. Incomplete cache data: Each cache only had sharedItems for fields used by that specific pivot table. Now generates sharedItems for ALL fields in source worksheet so any field can be used 4. Wrong worksheet relationships: All worksheets pointed to pivotTable1.xml because code used local worksheet index (always 0). Now assigns global tableNumber property to track correct file Added comprehensive test case with 3 pivot tables from same source with completely different field configurations. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
protobi-pieter
added a commit
to protobi/exceljs
that referenced
this pull request
Nov 7, 2025
All original features now submitted to upstream: - PR exceljs#2995: Multiple pivot tables support - PR exceljs#2996: XML special character escaping - PR exceljs#2997: Pivot table column width control Total: 9 PRs submitted to upstream (6 adopted + 3 original) All features are now pending upstream review. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds support for multiple pivot tables per workbook. Previously limited to one pivot table per file, users can now create multiple pivot tables from the same or different source worksheets.
This resolves a fundamental limitation mentioned in the original pivot table PR (#2551) and addresses user requests for multi-table support.
Motivation
The current implementation throws an error when attempting to create more than one pivot table:
However, Excel natively supports multiple pivot tables, and users frequently need to create different views of the same data source.
Changes
1. Unique Cache IDs (
lib/doc/pivot-table.js)cacheId: '10'to dynamiccacheId: String(10 + worksheet.workbook.pivotTables.length)2. Unique Pivot Table UIDs (
lib/xlsx/xform/pivot-table/pivot-table-xform.js)uuidv4()for each pivot table3. Complete Cache Field Data (
lib/doc/pivot-table.js)4. Correct Worksheet Relationships (
lib/doc/worksheet.js,lib/xlsx/xform/sheet/worksheet-xform.js)tableNumberproperty to track correct pivot table file referencespivotTable1.xml(hardcoded) topivotTable${tableNumber}.xml5. Fixed State Mutation Bug (
lib/doc/pivot-table.js)splice()toslice()inmakeCacheFields()6. Remove One-Table Limit (
lib/doc/pivot-table.js)Test Plan
Added comprehensive test case
test/test-pivot-multiple-from-same-source.js:Example Usage
Backwards Compatibility
✅ Fully backwards compatible - Existing code with single pivot tables works identically
✅ No breaking changes - All existing tests pass
✅ Opt-in feature - Multiple tables only if user creates them
Files Changed
lib/doc/pivot-table.js- Core pivot table generation logiclib/doc/worksheet.js- Track table numberslib/xlsx/xform/pivot-table/pivot-table-xform.js- Unique UIDslib/xlsx/xform/sheet/worksheet-xform.js- Correct file referencestest/test-pivot-multiple-from-same-source.js- Test caseRelated Issues
Checklist
Fork Context: This PR originates from @protobi/exceljs, a temporary fork with pivot table enhancements. We're submitting all improvements back to upstream.