@@ -14,6 +14,7 @@ package excelize
14
14
import (
15
15
"encoding/xml"
16
16
"fmt"
17
+ "os"
17
18
"reflect"
18
19
"strconv"
19
20
"strings"
@@ -348,36 +349,57 @@ func (f *File) SetCellStr(sheet, axis, value string) error {
348
349
ws .Lock ()
349
350
defer ws .Unlock ()
350
351
cellData .S = f .prepareCellStyle (ws , col , cellData .S )
351
- cellData .T , cellData .V = f .setCellString (value )
352
+ cellData .T , cellData .V , err = f .setCellString (value )
352
353
return err
353
354
}
354
355
355
356
// setCellString provides a function to set string type to shared string
356
357
// table.
357
- func (f * File ) setCellString (value string ) (t string , v string ) {
358
+ func (f * File ) setCellString (value string ) (t , v string , err error ) {
358
359
if len (value ) > TotalCellChars {
359
360
value = value [:TotalCellChars ]
360
361
}
361
362
t = "s"
362
- v = strconv .Itoa (f .setSharedString (value ))
363
+ var si int
364
+ if si , err = f .setSharedString (value ); err != nil {
365
+ return
366
+ }
367
+ v = strconv .Itoa (si )
368
+ return
369
+ }
370
+
371
+ // sharedStringsLoader load shared string table from system temporary file to
372
+ // memory, and reset shared string table for reader.
373
+ func (f * File ) sharedStringsLoader () (err error ) {
374
+ f .Lock ()
375
+ defer f .Unlock ()
376
+ if path , ok := f .tempFiles .Load (dafaultXMLPathSharedStrings ); ok {
377
+ f .Pkg .Store (dafaultXMLPathSharedStrings , f .readBytes (dafaultXMLPathSharedStrings ))
378
+ f .tempFiles .Delete (dafaultXMLPathSharedStrings )
379
+ err = os .Remove (path .(string ))
380
+ f .SharedStrings , f .sharedStringItemMap = nil , nil
381
+ }
363
382
return
364
383
}
365
384
366
385
// setSharedString provides a function to add string to the share string table.
367
- func (f * File ) setSharedString (val string ) int {
386
+ func (f * File ) setSharedString (val string ) (int , error ) {
387
+ if err := f .sharedStringsLoader (); err != nil {
388
+ return 0 , err
389
+ }
368
390
sst := f .sharedStringsReader ()
369
391
f .Lock ()
370
392
defer f .Unlock ()
371
393
if i , ok := f .sharedStringsMap [val ]; ok {
372
- return i
394
+ return i , nil
373
395
}
374
396
sst .Count ++
375
397
sst .UniqueCount ++
376
398
t := xlsxT {Val : val }
377
399
_ , val , t .Space = setCellStr (val )
378
400
sst .SI = append (sst .SI , xlsxSI {T : & t })
379
401
f .sharedStringsMap [val ] = sst .UniqueCount - 1
380
- return sst .UniqueCount - 1
402
+ return sst .UniqueCount - 1 , nil
381
403
}
382
404
383
405
// setCellStr provides a function to set string type to cell.
@@ -762,6 +784,34 @@ func (f *File) GetCellRichText(sheet, cell string) (runs []RichTextRun, err erro
762
784
return
763
785
}
764
786
787
+ // newRpr create run properties for the rich text by given font format.
788
+ func newRpr (fnt * Font ) * xlsxRPr {
789
+ rpr := xlsxRPr {}
790
+ trueVal := ""
791
+ if fnt .Bold {
792
+ rpr .B = & trueVal
793
+ }
794
+ if fnt .Italic {
795
+ rpr .I = & trueVal
796
+ }
797
+ if fnt .Strike {
798
+ rpr .Strike = & trueVal
799
+ }
800
+ if fnt .Underline != "" {
801
+ rpr .U = & attrValString {Val : & fnt .Underline }
802
+ }
803
+ if fnt .Family != "" {
804
+ rpr .RFont = & attrValString {Val : & fnt .Family }
805
+ }
806
+ if fnt .Size > 0.0 {
807
+ rpr .Sz = & attrValFloat {Val : & fnt .Size }
808
+ }
809
+ if fnt .Color != "" {
810
+ rpr .Color = & xlsxColor {RGB : getPaletteColor (fnt .Color )}
811
+ }
812
+ return & rpr
813
+ }
814
+
765
815
// SetCellRichText provides a function to set cell with rich text by given
766
816
// worksheet. For example, set rich text on the A1 cell of the worksheet named
767
817
// Sheet1:
@@ -875,6 +925,9 @@ func (f *File) SetCellRichText(sheet, cell string, runs []RichTextRun) error {
875
925
if err != nil {
876
926
return err
877
927
}
928
+ if err := f .sharedStringsLoader (); err != nil {
929
+ return err
930
+ }
878
931
cellData .S = f .prepareCellStyle (ws , col , cellData .S )
879
932
si := xlsxSI {}
880
933
sst := f .sharedStringsReader ()
@@ -889,30 +942,7 @@ func (f *File) SetCellRichText(sheet, cell string, runs []RichTextRun) error {
889
942
_ , run .T .Val , run .T .Space = setCellStr (textRun .Text )
890
943
fnt := textRun .Font
891
944
if fnt != nil {
892
- rpr := xlsxRPr {}
893
- trueVal := ""
894
- if fnt .Bold {
895
- rpr .B = & trueVal
896
- }
897
- if fnt .Italic {
898
- rpr .I = & trueVal
899
- }
900
- if fnt .Strike {
901
- rpr .Strike = & trueVal
902
- }
903
- if fnt .Underline != "" {
904
- rpr .U = & attrValString {Val : & fnt .Underline }
905
- }
906
- if fnt .Family != "" {
907
- rpr .RFont = & attrValString {Val : & fnt .Family }
908
- }
909
- if fnt .Size > 0.0 {
910
- rpr .Sz = & attrValFloat {Val : & fnt .Size }
911
- }
912
- if fnt .Color != "" {
913
- rpr .Color = & xlsxColor {RGB : getPaletteColor (fnt .Color )}
914
- }
915
- run .RPr = & rpr
945
+ run .RPr = newRpr (fnt )
916
946
}
917
947
textRuns = append (textRuns , run )
918
948
}
0 commit comments