Skip to content

Commit eaf9781

Browse files
Arnie97xuri
andauthored
Improve compatibility for SetRichText (#976)
- support escaped string literal - maximum character limit added - fix missing preserve character in some case Co-authored-by: xuri <[email protected]>
1 parent 7ac37ed commit eaf9781

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

cell.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -307,16 +307,8 @@ func (f *File) setSharedString(val string) int {
307307
}
308308
sst.Count++
309309
sst.UniqueCount++
310-
val = bstrMarshal(val)
311310
t := xlsxT{Val: val}
312-
// Leading and ending space(s) character detection.
313-
if len(val) > 0 && (val[0] == 32 || val[len(val)-1] == 32) {
314-
ns := xml.Attr{
315-
Name: xml.Name{Space: NameSpaceXML, Local: "space"},
316-
Value: "preserve",
317-
}
318-
t.Space = ns
319-
}
311+
_, val, t.Space = setCellStr(val)
320312
sst.SI = append(sst.SI, xlsxSI{T: &t})
321313
f.sharedStringsMap[val] = sst.UniqueCount - 1
322314
return sst.UniqueCount - 1
@@ -327,11 +319,16 @@ func setCellStr(value string) (t string, v string, ns xml.Attr) {
327319
if len(value) > TotalCellChars {
328320
value = value[0:TotalCellChars]
329321
}
330-
// Leading and ending space(s) character detection.
331-
if len(value) > 0 && (value[0] == 32 || value[len(value)-1] == 32) {
332-
ns = xml.Attr{
333-
Name: xml.Name{Space: NameSpaceXML, Local: "space"},
334-
Value: "preserve",
322+
if len(value) > 0 {
323+
prefix, suffix := value[0], value[len(value)-1]
324+
for _, ascii := range []byte{10, 13, 32} {
325+
if prefix == ascii || suffix == ascii {
326+
ns = xml.Attr{
327+
Name: xml.Name{Space: NameSpaceXML, Local: "space"},
328+
Value: "preserve",
329+
}
330+
break
331+
}
335332
}
336333
}
337334
t = "str"
@@ -702,11 +699,14 @@ func (f *File) SetCellRichText(sheet, cell string, runs []RichTextRun) error {
702699
si := xlsxSI{}
703700
sst := f.sharedStringsReader()
704701
textRuns := []xlsxR{}
702+
totalCellChars := 0
705703
for _, textRun := range runs {
706-
run := xlsxR{T: &xlsxT{Val: textRun.Text}}
707-
if strings.ContainsAny(textRun.Text, "\r\n ") {
708-
run.T.Space = xml.Attr{Name: xml.Name{Space: NameSpaceXML, Local: "space"}, Value: "preserve"}
704+
totalCellChars += len(textRun.Text)
705+
if totalCellChars > TotalCellChars {
706+
return ErrCellCharsLength
709707
}
708+
run := xlsxR{T: &xlsxT{}}
709+
_, run.T.Val, run.T.Space = setCellStr(textRun.Text)
710710
fnt := textRun.Font
711711
if fnt != nil {
712712
rpr := xlsxRPr{}

cell_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,9 @@ func TestSetCellRichText(t *testing.T) {
401401
assert.EqualError(t, f.SetCellRichText("SheetN", "A1", richTextRun), "sheet SheetN is not exist")
402402
// Test set cell rich text with illegal cell coordinates
403403
assert.EqualError(t, f.SetCellRichText("Sheet1", "A", richTextRun), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
404+
richTextRun = []RichTextRun{{Text: strings.Repeat("s", TotalCellChars+1)}}
405+
// Test set cell rich text with characters over the maximum limit
406+
assert.EqualError(t, f.SetCellRichText("Sheet1", "A1", richTextRun), ErrCellCharsLength.Error())
404407
}
405408

406409
func TestFormattedValue2(t *testing.T) {

errors.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ var (
4545
ErrColumnNumber = errors.New("column number exceeds maximum limit")
4646
// ErrColumnWidth defined the error message on receive an invalid column
4747
// width.
48-
ErrColumnWidth = errors.New("the width of the column must be smaller than or equal to 255 characters")
48+
ErrColumnWidth = fmt.Errorf("the width of the column must be smaller than or equal to %d characters", MaxColumnWidth)
4949
// ErrOutlineLevel defined the error message on receive an invalid outline
5050
// level number.
5151
ErrOutlineLevel = errors.New("invalid outline level")
@@ -111,4 +111,7 @@ var (
111111
// ErrDataValidationRange defined the error message on set decimal range
112112
// exceeds limit.
113113
ErrDataValidationRange = errors.New("data validation range exceeds limit")
114+
// ErrCellCharsLength defined the error message for receiving a cell
115+
// characters length that exceeds the limit.
116+
ErrCellCharsLength = fmt.Errorf("cell value must be 0-%d characters", TotalCellChars)
114117
)

0 commit comments

Comments
 (0)