feat: Add applyWidthHeightFormats option for pivot table column widths #2997
+200
−9
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 optional
applyWidthHeightFormatsparameter to pivot tables, allowing users to preserve worksheet column widths instead of using Excel's default pivot table style widths.Problem
By default, Excel pivot table styles control column widths, overriding any custom column widths set on the worksheet. This is Excel's standard behavior (
applyWidthHeightFormats='1'), but users often want to preserve their carefully crafted column widths.Current behavior:
The pivot table style resets all column widths, making custom sizing impossible.
Solution
Add optional
applyWidthHeightFormatsparameter (default:'1'for backwards compatibility):Implementation
1. API Change (
lib/doc/pivot-table.js)Added parameter to pivot table model with sensible default:
2. XML Generation (
lib/xlsx/xform/pivot-table/pivot-table-xform.js)Pass the setting to Excel via OOXML attribute:
Per OOXML spec:
'1'(default) - Apply pivot table style widths/heights'0'- Preserve worksheet widths/heights3. Test Case (
test/test-pivot-table.js)Added test demonstrating custom width preservation:
Use Cases
1. Custom Dashboard Layouts
2. Print-Optimized Reports
3. Responsive Layouts
Backwards Compatibility
✅ 100% backwards compatible
'1'maintains existing behaviorStandards Compliance
Follows Office Open XML Part 1 Section 18.10:
This is a standard Excel feature exposed through the OOXML format.
Files Changed
lib/doc/pivot-table.js- Accept parameter, set defaultlib/xlsx/xform/pivot-table/pivot-table-xform.js- Pass to XMLtest/test-pivot-table.js- Test case demonstrating featureChecklist
Fork Context: This PR originates from @protobi/exceljs, a temporary fork with pivot table enhancements. We're submitting all improvements back to upstream.