Skip to content

Treat tabs and line breaks as blanks when exporting strings - #1033

Merged
michelebastione merged 1 commit into
mini-software:masterfrom
ybai08:fix/export-preserve-whitespace
Oct 22, 2026
Merged

michelebastione merged 1 commit into
mini-software:masterfrom
ybai08:fix/export-preserve-whitespace

Conversation

@ybai08

@ybai08 ybai08 commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Refs #294. Follow-up to #1030, from its review.

Excel drops leading and trailing blanks from a <t> that has no xml:space="preserve". Export added the attribute only for a plain space (StartsWith(" ") || EndsWith(" ")). #1030's template pass uses char.IsWhiteSpace, so a value starting with a tab or ending with a line break kept the attribute in FillTemplate but not in Export. Tabs and line breaks are written raw (EncodeString passes them through as valid XML characters), so without the attribute they are at the same risk as spaces.

Change

  • XmlHelper.HasLeadingOrTrailingWhiteSpace: char.IsWhiteSpace on the first and last character. No allocation; it works on netstandard2.0.
  • Used for inline string cells (OpenXmlWriter.WriteCellAsync, the Prefix and suffix blank space will lost after SaveAs #294 check) and for shared strings (ExcelXml.SharedStrings).

The formula branch of WorksheetXml.Cell also reads preserveSpace, so a formula that starts or ends with a tab or line break now gets the attribute too, as one with a space already did.

Tests

TestIssue294WhiteSpace exports "\tJack", "Jack\n" and "Ja\tck" with StringStorageMode.Inline and Shared. It checks the <x:t> in sheet1.xml or sharedStrings.xml: the attribute is present for the first two and absent for the third. Without the change to src/, the four preserve cases fail and the two "Ja\tck" cases pass.

Commands and results

  • dotnet test tests/MiniExcel.OpenXml.Tests -p:TargetFrameworks=net10.0: 517 passed, 20 failed. The 20 failures are the SQLite.Interop.dll load errors on osx-arm64 described in Keep leading and trailing blanks of template values visible in Excel #1030. With src/ from master the same 20 fail, plus the 4 new cases.
  • dotnet test tests/MiniExcel.Csv.Tests -p:TargetFrameworks=net10.0: 82 passed, 4 failed, all SQLite native-library errors.
  • dotnet build src/MiniExcel.OpenXml for netstandard2.0, net8.0 and net10.0: no warnings in the changed files.

Not run: tests on net8.0, net9.0 and net11.0 (only the .NET 10 runtime is installed here), the SQLite-backed tests, and opening the output in Excel. The tests check the XML Excel reads, not what Excel displays.

Summary by CodeRabbit

  • Bug Fixes
    • Excel exports now preserve leading or trailing whitespace—including tabs and line breaks—in cell text. Interior whitespace remains unchanged.

Excel drops leading and trailing blanks from a <t> without
xml:space="preserve". The export only checked for a plain space, while
the template pass (mini-software#295) checks char.IsWhiteSpace, so a value that
starts with a tab or ends with a line break kept it in FillTemplate but
lost it in Export. Inline cells and shared strings now use the same
check.
@coderabbitai

coderabbitai Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: da8cd4cf-89f2-4628-ab8f-18d4f6fe3334
📥 Commits

Reviewing files that changed from the base of the PR and between d1c15b0 and 07822e0.

📒 Files selected for processing: 4
  • src/MiniExcel.OpenXml/Constants/ExcelXml.cs
  • src/MiniExcel.OpenXml/Helpers/XmlHelper.cs
  • src/MiniExcel.OpenXml/Writer/OpenXmlWriter.cs
  • tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

📝 Walkthrough

Whitespace detection for shared strings and cells now uses a common helper that checks for leading or trailing whitespace. Tests cover tabs and line breaks in inline and shared-string storage.

Changes: Whitespace preservation

Layer / File(s) Summary
Boundary whitespace detection
src/MiniExcel.OpenXml/Helpers/XmlHelper.cs, src/MiniExcel.OpenXml/Constants/ExcelXml.cs, src/MiniExcel.OpenXml/Writer/OpenXmlWriter.cs, tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs
XmlHelper.HasLeadingOrTrailingWhiteSpace checks the first and last characters of nonempty input. Shared-string and cell writing use the helper to detect whitespace that requires preservation. Tests cover leading tabs, trailing line breaks, and interior tabs for inline and shared-string storage.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: michelebastione

Merge Risk: ⚪ Minimal · up to 07822

The change is mergeable; remaining Excel-rendering and target-framework validation are routine follow-up checks rather than established merge-blocking risks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 57.14% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 4 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: preserving tabs and line breaks when exporting strings.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@michelebastione
michelebastione merged commit 5a6a8b1 into mini-software:master Oct 22, 2026
4 checks passed
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.

2 participants