From c168233e70db8f220bd07d9d6d277ae9e2a4a73f Mon Sep 17 00:00:00 2001 From: xuri Date: Wed, 27 May 2020 00:02:29 +0800 Subject: [PATCH] speedup get cell value from shared string table --- cell.go | 7 +++---- excelize.go | 2 ++ rows.go | 5 +++++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/cell.go b/cell.go index e64ef26..6981cce 100644 --- a/cell.go +++ b/cell.go @@ -299,14 +299,13 @@ func (f *File) setCellString(value string) (t string, v string, ns xml.Attr) { // setSharedString provides a function to add string to the share string table. func (f *File) setSharedString(val string) int { sst := f.sharedStringsReader() - for i, si := range sst.SI { - if si.T == val { - return i - } + if i, ok := f.sharedStringsMap[val]; ok { + return i } sst.Count++ sst.UniqueCount++ sst.SI = append(sst.SI, xlsxSI{T: val}) + f.sharedStringsMap[val] = sst.UniqueCount - 1 return sst.UniqueCount - 1 } diff --git a/excelize.go b/excelize.go index 3fd25aa..3e0255a 100644 --- a/excelize.go +++ b/excelize.go @@ -38,6 +38,7 @@ type File struct { Drawings map[string]*xlsxWsDr Path string SharedStrings *xlsxSST + sharedStringsMap map[string]int Sheet map[string]*xlsxWorksheet SheetCount int Styles *xlsxStyleSheet @@ -75,6 +76,7 @@ func newFile() *File { sheetMap: make(map[string]string), Comments: make(map[string]*xlsxComments), Drawings: make(map[string]*xlsxWsDr), + sharedStringsMap: make(map[string]int), Sheet: make(map[string]*xlsxWorksheet), DecodeVMLDrawing: make(map[string]*decodeVmlDrawing), VMLDrawing: make(map[string]*vmlDrawing), diff --git a/rows.go b/rows.go index 5be3182..352f1eb 100644 --- a/rows.go +++ b/rows.go @@ -292,6 +292,11 @@ func (f *File) sharedStringsReader() *xlsxSST { log.Printf("xml decode error: %s", err) } f.SharedStrings = &sharedStrings + for i := range sharedStrings.SI { + if sharedStrings.SI[i].T != "" { + f.sharedStringsMap[sharedStrings.SI[i].T] = i + } + } f.addContentTypePart(0, "sharedStrings") rels := f.relsReader("xl/_rels/workbook.xml.rels") for _, rel := range rels.Relationships {