Skip to content

[fix]: Replace invalid XML characters from cell values during export#399

Open
deeonwuli wants to merge 2 commits into
developmentfrom
fix/remove-invalid-xml-chars-on-export
Open

[fix]: Replace invalid XML characters from cell values during export#399
deeonwuli wants to merge 2 commits into
developmentfrom
fix/remove-invalid-xml-chars-on-export

Conversation

@deeonwuli
Copy link
Copy Markdown

@deeonwuli deeonwuli commented May 20, 2026

📌 References

📝 Implementation

Excel-generated .xlsx files fail to open in MS Excel when a DHIS2 string value contains a C0 control character (e.g. vertical tab \x0B), because the xlsx format is XML 1.0 which forbids those bytes.

  • Adds stripInvalidXmlChars in src/utils/string.ts that replaces invalid XML 1.0 control chars (\x00-\x08, \x0B, \x0C, \x0E-\x1F, \x7F) with a space, preserving the XML-safe whitespace controls (\t, \n, \r).
  • Applies the sanitizer at the export point — ExcelPopulateRepository.writeCell — so every string value reaching xlsx-populate is safe. Numeric, boolean, formula and defined-name branches are untouched.

#869dbc90g

@deeonwuli deeonwuli requested a review from MiquelAdell May 20, 2026 16:12
@deeonwuli deeonwuli changed the title replace invalid XML characters from cell values during export [fix]: Replace invalid XML characters from cell values during export May 20, 2026
@bundlemon
Copy link
Copy Markdown

bundlemon Bot commented May 20, 2026

BundleMon

No change in files bundle size

Groups updated (1)
Status Path Size Limits
Build Folder
./**/*
1.72MB (+57B 0%) +20%

Final result: ✅

View report in BundleMon website ➡️


Current branch size history | Target branch size history

@deeonwuli deeonwuli requested a review from eperedo May 21, 2026 07:23
Copy link
Copy Markdown

@MiquelAdell MiquelAdell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tested and working

Comment thread src/data/ExcelPopulateRepository.ts Outdated
destination.value(Number(value));
} else if (String(value).startsWith("=")) {
destination.formula(String(value));
const safeValue = typeof value === "string" ? replaceInvalidXmlChars(value) : value;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a isString method in string.ts that we can reuse here.

Comment thread src/data/ExcelPopulateRepository.ts Outdated
} else if (String(value).startsWith("=")) {
destination.formula(String(value));
const safeValue = typeof value === "string" ? replaceInvalidXmlChars(value) : value;
if (!!safeValue && !isNaN(Number(safeValue))) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the double !! necessary? We can use directly

safeValue && !isNaN(Number(safeValue)))

right?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right!

@deeonwuli deeonwuli requested a review from eperedo May 25, 2026 10:22
Comment thread src/utils/string.ts
@@ -5,3 +5,14 @@ export function removeCharacters(value: unknown): string {
export function isString(value: unknown): value is string {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant this function, not the lodash implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants