Function `GetCellValue()` performance improvement by avoid repeating deserialization, relate issue #70.

formula
Ri Xu 8 years ago
parent 86466654e2
commit e05867a033
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7

@ -1,7 +1,6 @@
package excelize package excelize
import ( import (
"encoding/xml"
"strconv" "strconv"
"strings" "strings"
) )
@ -48,10 +47,9 @@ func (f *File) GetCellValue(sheet, axis string) string {
} }
switch r.T { switch r.T {
case "s": case "s":
shardStrings := xlsxSST{} shardStrings := f.sharedStringsReader()
xlsxSI := 0 xlsxSI := 0
xlsxSI, _ = strconv.Atoi(r.V) xlsxSI, _ = strconv.Atoi(r.V)
xml.Unmarshal([]byte(f.readXML("xl/sharedStrings.xml")), &shardStrings)
return f.formattedValue(r.S, shardStrings.SI[xlsxSI].T) return f.formattedValue(r.S, shardStrings.SI[xlsxSI].T)
case "str": case "str":
return f.formattedValue(r.S, r.V) return f.formattedValue(r.S, r.V)

@ -15,15 +15,16 @@ import (
// File define a populated XLSX file struct. // File define a populated XLSX file struct.
type File struct { type File struct {
checked map[string]bool checked map[string]bool
ContentTypes *xlsxTypes ContentTypes *xlsxTypes
Path string Path string
Sheet map[string]*xlsxWorksheet SharedStrings *xlsxSST
SheetCount int Sheet map[string]*xlsxWorksheet
Styles *xlsxStyleSheet SheetCount int
WorkBook *xlsxWorkbook Styles *xlsxStyleSheet
WorkBookRels *xlsxWorkbookRels WorkBook *xlsxWorkbook
XLSX map[string]string WorkBookRels *xlsxWorkbookRels
XLSX map[string]string
} }
// OpenFile take the name of an XLSX file and returns a populated XLSX file // 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. // deserialization by given worksheet index.
func (f *File) workSheetReader(sheet string) *xlsxWorksheet { func (f *File) workSheetReader(sheet string) *xlsxWorksheet {
name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml" name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
worksheet := f.Sheet[name] if f.Sheet[name] == nil {
if worksheet == nil {
var xlsx xlsxWorksheet var xlsx xlsxWorksheet
xml.Unmarshal([]byte(f.readXML(name)), &xlsx) xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
if f.checked == nil { if f.checked == nil {
@ -159,9 +159,8 @@ func (f *File) workSheetReader(sheet string) *xlsxWorksheet {
f.checked[name] = true f.checked[name] = true
} }
f.Sheet[name] = &xlsx 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 // SetCellInt provides function to set int type value of a cell by given

@ -30,7 +30,7 @@ func (f *File) GetRows(sheet string) [][]string {
f.saveFileList(name, replaceWorkSheetsRelationshipsNameSpace(string(output))) f.saveFileList(name, replaceWorkSheetsRelationshipsNameSpace(string(output)))
} }
decoder := xml.NewDecoder(strings.NewReader(f.readXML(name))) decoder := xml.NewDecoder(strings.NewReader(f.readXML(name)))
d, _ := readXMLSST(f) d := f.sharedStringsReader()
var inElement string var inElement string
var r xlsxRow var r xlsxRow
var row []string var row []string
@ -146,11 +146,15 @@ func (f *File) GetRowHeight(sheet string, row int) float64 {
return defaultRowHeightPixels return defaultRowHeightPixels
} }
// readXMLSST read xmlSST simple function. // sharedStringsReader provides function to get the pointer to the structure
func readXMLSST(f *File) (*xlsxSST, error) { // after deserialization of xl/sharedStrings.xml.
shardStrings := xlsxSST{} func (f *File) sharedStringsReader() *xlsxSST {
err := xml.Unmarshal([]byte(f.readXML("xl/sharedStrings.xml")), &shardStrings) if f.SharedStrings == nil {
return &shardStrings, err 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 // getValueFrom return a value from a column/row cell, this function is inteded

@ -225,7 +225,7 @@ func parseTime(i int, v string) string {
} }
// stylesReader provides function to get the pointer to the structure after // stylesReader provides function to get the pointer to the structure after
// deserialization of workbook. // deserialization of xl/styles.xml.
func (f *File) stylesReader() *xlsxStyleSheet { func (f *File) stylesReader() *xlsxStyleSheet {
if f.Styles == nil { if f.Styles == nil {
var styleSheet xlsxStyleSheet var styleSheet xlsxStyleSheet

Loading…
Cancel
Save