feat: Support multiple pivot tables from same source data #2995
+96
−7
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.