Fix string counting and truncation for cell length limitation #2205 #2206
+68
−25
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.
PR Details
Add UTF-16 code unit aware helpers (utf16UnitCountInString, truncateUTF16Units) and update sheet name validation to use code unit length.
Description
This change introduces two new internal helpers:
Sheet name validation (checkSheetName) now enforces the 31 unit limit using UTF-16 code units instead of plain rune count, aligning behavior with Excel’s internal UTF-16 length semantics (emoji / supplementary characters consume two units). This prevents accepting names that Excel would later reject or truncating inside a surrogate pair in downstream consumers.
Related Issue
#2205
Motivation and Context
The Excel limit is effectively a UTF-16 unit, not a Unicode scalar number.
The previous length check did not properly handle strings containing surrogate pairs, resulting in problematic Excel files.
This will be fixed so that correct Excel files are output.
How Has This Been Tested
Testing Environment:
Go version: go version go1.24.2 darwin/arm64
OS: macOS 15.5
Types of changes
Stricter rejection of sheet names that previously slipped through when they exceeded the true UTF-16 unit limit via supplementary characters. This aligns behavior with Excel and is considered a correctness fix.
Checklist