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

This commit is contained in:
Ri Xu 2017-06-29 13:28:44 +08:00
parent 86466654e2
commit e05867a033
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
4 changed files with 24 additions and 23 deletions

View File

@ -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)

View File

@ -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

16
rows.go
View File

@ -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

View File

@ -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