Skip to content

Commit f47b3df

Browse files
committed
This closes qax-os#2167, count characters as single runes in characters length limitation checking
1 parent 38f11c4 commit f47b3df

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

cell.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ func setRichText(runs []RichTextRun) ([]xlsxR, error) {
12111211
totalCellChars int
12121212
)
12131213
for _, textRun := range runs {
1214-
totalCellChars += len(textRun.Text)
1214+
totalCellChars += utf8.RuneCountInString(textRun.Text)
12151215
if totalCellChars > TotalCellChars {
12161216
return textRuns, ErrCellCharsLength
12171217
}

cell_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,7 @@ func TestSetCellRichText(t *testing.T) {
10111011
}
10121012
assert.NoError(t, f.SetCellRichText("Sheet1", "A1", richTextRun))
10131013
assert.NoError(t, f.SetCellRichText("Sheet1", "A2", richTextRun))
1014+
assert.NoError(t, f.SetCellRichText("Sheet1", "A3", []RichTextRun{{Text: strings.Repeat("\u4e00", TotalCellChars)}}))
10141015
style, err := f.NewStyle(&Style{
10151016
Alignment: &Alignment{
10161017
WrapText: true,

vml.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"path/filepath"
2020
"strconv"
2121
"strings"
22+
"unicode/utf8"
2223
)
2324

2425
// FormControlType is the type of supported form controls.
@@ -285,20 +286,20 @@ func (f *File) addComment(commentsXML string, opts vmlOptions) error {
285286
Text: xlsxText{R: []xlsxR{}},
286287
}
287288
if opts.Comment.Text != "" {
288-
if len(opts.Comment.Text) > TotalCellChars {
289-
opts.Comment.Text = opts.Comment.Text[:TotalCellChars]
289+
if utf8.RuneCountInString(opts.Comment.Text) > TotalCellChars {
290+
opts.Comment.Text = string([]rune(opts.Comment.Text)[:TotalCellChars])
290291
}
291292
cmt.Text.T = stringPtr(opts.Comment.Text)
292-
chars += len(opts.Comment.Text)
293+
chars += utf8.RuneCountInString(opts.Comment.Text)
293294
}
294295
for _, run := range opts.Comment.Paragraph {
295296
if chars == TotalCellChars {
296297
break
297298
}
298-
if chars+len(run.Text) > TotalCellChars {
299-
run.Text = run.Text[:TotalCellChars-chars]
299+
if chars+utf8.RuneCountInString(run.Text) > TotalCellChars {
300+
run.Text = string([]rune(run.Text)[:TotalCellChars-chars])
300301
}
301-
chars += len(run.Text)
302+
chars += utf8.RuneCountInString(run.Text)
302303
r := xlsxR{
303304
RPr: &xlsxRPr{
304305
Sz: &attrValFloat{Val: float64Ptr(9)},

0 commit comments

Comments
 (0)