fix: Handle XML special characters and null values in pivot tables #2996
+136
−3
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
Fixes two critical issues with pivot table data handling:
&,<,>,",'Both issues cause Excel to display "We found a problem with some content" or crash when opening files generated by ExcelJS.
Problem
Issue 1: XML Special Characters Not Escaped
When pivot table source data contains XML special characters (e.g., company names like "Smith & Co"), ExcelJS generates invalid XML:
Excel rejects the file with: "We found a problem with some content in 'file.xlsx'"
Issue 2: Null/Undefined Values Cause Crashes
When source data contains
nullorundefined:ExcelJS throws:
Solution
1. XML Escaping (
lib/xlsx/xform/pivot-table/cache-field.js)Added
escapeXml()method to properly escape special characters in cache field values:Now generates valid XML:
2. Null/Undefined Handling (
lib/xlsx/xform/pivot-table/pivot-cache-records-xform.js)Added check before sharedItems lookup:
Follows OOXML standard for representing missing data in PivotCacheRecords.
Test Plan
XML Escaping Test (
test/test-pivot-xml-escape.js)Tests data containing all XML special characters:
Verifies:
Null Values Test (
test/test-pivot-null-values.js)Tests data with missing values:
Verifies:
Backwards Compatibility
✅ Fully backwards compatible
✅ No breaking changes
✅ All existing tests pass
✅ Handles edge cases that previously crashed
Real-World Impact
These fixes address common production scenarios:
Before: ❌ Crashes with data like:
After: ✅ Works perfectly with all real-world data
Files Changed
lib/xlsx/xform/pivot-table/cache-field.js- XML escapinglib/xlsx/xform/pivot-table/pivot-cache-records-xform.js- Null handlingtest/test-pivot-xml-escape.js- XML escaping teststest/test-pivot-null-values.js- Null handling testsStandards Compliance
<m />tag for missing values (per OOXML spec)Checklist
Fork Context: This PR originates from @protobi/exceljs, a temporary fork with pivot table enhancements. We're submitting all improvements back to upstream.