@@ -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 {}
0 commit comments