@@ -14,6 +14,7 @@ package excelize
1414import (
1515 "encoding/xml"
1616 "fmt"
17+ "os"
1718 "reflect"
1819 "strconv"
1920 "strings"
@@ -348,36 +349,57 @@ func (f *File) SetCellStr(sheet, axis, value string) error {
348349 ws .Lock ()
349350 defer ws .Unlock ()
350351 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 )
352353 return err
353354}
354355
355356// setCellString provides a function to set string type to shared string
356357// table.
357- func (f * File ) setCellString (value string ) (t string , v string ) {
358+ func (f * File ) setCellString (value string ) (t , v string , err error ) {
358359 if len (value ) > TotalCellChars {
359360 value = value [:TotalCellChars ]
360361 }
361362 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+ }
363382 return
364383}
365384
366385// 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+ }
368390 sst := f .sharedStringsReader ()
369391 f .Lock ()
370392 defer f .Unlock ()
371393 if i , ok := f .sharedStringsMap [val ]; ok {
372- return i
394+ return i , nil
373395 }
374396 sst .Count ++
375397 sst .UniqueCount ++
376398 t := xlsxT {Val : val }
377399 _ , val , t .Space = setCellStr (val )
378400 sst .SI = append (sst .SI , xlsxSI {T : & t })
379401 f .sharedStringsMap [val ] = sst .UniqueCount - 1
380- return sst .UniqueCount - 1
402+ return sst .UniqueCount - 1 , nil
381403}
382404
383405// 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
762784 return
763785}
764786
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+
765815// SetCellRichText provides a function to set cell with rich text by given
766816// worksheet. For example, set rich text on the A1 cell of the worksheet named
767817// Sheet1:
@@ -875,6 +925,9 @@ func (f *File) SetCellRichText(sheet, cell string, runs []RichTextRun) error {
875925 if err != nil {
876926 return err
877927 }
928+ if err := f .sharedStringsLoader (); err != nil {
929+ return err
930+ }
878931 cellData .S = f .prepareCellStyle (ws , col , cellData .S )
879932 si := xlsxSI {}
880933 sst := f .sharedStringsReader ()
@@ -889,30 +942,7 @@ func (f *File) SetCellRichText(sheet, cell string, runs []RichTextRun) error {
889942 _ , run .T .Val , run .T .Space = setCellStr (textRun .Text )
890943 fnt := textRun .Font
891944 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 )
916946 }
917947 textRuns = append (textRuns , run )
918948 }
0 commit comments