fix: Handle XML special characters and null values in pivot tables#2996
Open
protobi-pieter wants to merge 2 commits intoexceljs:masterfrom
Open
fix: Handle XML special characters and null values in pivot tables#2996protobi-pieter wants to merge 2 commits intoexceljs:masterfrom
protobi-pieter wants to merge 2 commits intoexceljs:masterfrom
Conversation
Fixed crash when pivot table source data contains null or undefined values. Previously threw "undefined not in sharedItems" error. Changes: - Added check for null/undefined in renderCell() before sharedItems lookup - Returns <m /> tag (Excel's standard for missing values) - Added test case with null/undefined values in data The fix follows Excel's OOXML standard for representing missing data in PivotCacheRecords. Closes #3 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Added escapeXml() method to properly escape XML special characters (&, <, >, ", ') in pivot table cache fields. This prevents Excel from showing "We found a problem with some content" error when source data contains these characters. Includes test case with common special characters in company names, comparisons, and quoted text. 🤖 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
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.