diff --git a/cell.go b/cell.go index a8f6861..dc2263a 100644 --- a/cell.go +++ b/cell.go @@ -1,7 +1,6 @@ package excelize import ( - "encoding/xml" "strconv" "strings" ) @@ -48,10 +47,9 @@ func (f *File) GetCellValue(sheet, axis string) string { } switch r.T { case "s": - shardStrings := xlsxSST{} + shardStrings := f.sharedStringsReader() xlsxSI := 0 xlsxSI, _ = strconv.Atoi(r.V) - xml.Unmarshal([]byte(f.readXML("xl/sharedStrings.xml")), &shardStrings) return f.formattedValue(r.S, shardStrings.SI[xlsxSI].T) case "str": return f.formattedValue(r.S, r.V) diff --git a/excelize.go b/excelize.go index f8b938c..8e91fea 100644 --- a/excelize.go +++ b/excelize.go @@ -15,15 +15,16 @@ import ( // File define a populated XLSX file struct. type File struct { - checked map[string]bool - ContentTypes *xlsxTypes - Path string - Sheet map[string]*xlsxWorksheet - SheetCount int - Styles *xlsxStyleSheet - WorkBook *xlsxWorkbook - WorkBookRels *xlsxWorkbookRels - XLSX map[string]string + checked map[string]bool + ContentTypes *xlsxTypes + Path string + SharedStrings *xlsxSST + Sheet map[string]*xlsxWorksheet + SheetCount int + Styles *xlsxStyleSheet + WorkBook *xlsxWorkbook + WorkBookRels *xlsxWorkbookRels + XLSX map[string]string } // OpenFile take the name of an XLSX file and returns a populated XLSX file @@ -145,8 +146,7 @@ func (f *File) setDefaultTimeStyle(sheet, axis string) { // deserialization by given worksheet index. func (f *File) workSheetReader(sheet string) *xlsxWorksheet { name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml" - worksheet := f.Sheet[name] - if worksheet == nil { + if f.Sheet[name] == nil { var xlsx xlsxWorksheet xml.Unmarshal([]byte(f.readXML(name)), &xlsx) if f.checked == nil { @@ -159,9 +159,8 @@ func (f *File) workSheetReader(sheet string) *xlsxWorksheet { f.checked[name] = true } f.Sheet[name] = &xlsx - worksheet = f.Sheet[name] } - return worksheet + return f.Sheet[name] } // SetCellInt provides function to set int type value of a cell by given diff --git a/rows.go b/rows.go index 0dac853..227a7d5 100644 --- a/rows.go +++ b/rows.go @@ -30,7 +30,7 @@ func (f *File) GetRows(sheet string) [][]string { f.saveFileList(name, replaceWorkSheetsRelationshipsNameSpace(string(output))) } decoder := xml.NewDecoder(strings.NewReader(f.readXML(name))) - d, _ := readXMLSST(f) + d := f.sharedStringsReader() var inElement string var r xlsxRow var row []string @@ -146,11 +146,15 @@ func (f *File) GetRowHeight(sheet string, row int) float64 { return defaultRowHeightPixels } -// readXMLSST read xmlSST simple function. -func readXMLSST(f *File) (*xlsxSST, error) { - shardStrings := xlsxSST{} - err := xml.Unmarshal([]byte(f.readXML("xl/sharedStrings.xml")), &shardStrings) - return &shardStrings, err +// sharedStringsReader provides function to get the pointer to the structure +// after deserialization of xl/sharedStrings.xml. +func (f *File) sharedStringsReader() *xlsxSST { + if f.SharedStrings == nil { + var sharedStrings xlsxSST + xml.Unmarshal([]byte(f.readXML("xl/sharedStrings.xml")), &sharedStrings) + f.SharedStrings = &sharedStrings + } + return f.SharedStrings } // getValueFrom return a value from a column/row cell, this function is inteded diff --git a/styles.go b/styles.go index 6db97c1..eacbe9f 100644 --- a/styles.go +++ b/styles.go @@ -225,7 +225,7 @@ func parseTime(i int, v string) string { } // stylesReader provides function to get the pointer to the structure after -// deserialization of workbook. +// deserialization of xl/styles.xml. func (f *File) stylesReader() *xlsxStyleSheet { if f.Styles == nil { var styleSheet xlsxStyleSheet