Skip to content

Commit

Permalink
Function GetCellValue() performance improvement by avoid repeating …
Browse files Browse the repository at this point in the history
…deserialization, relate issue qax-os#70.
  • Loading branch information
xuri committed Jun 29, 2017
1 parent 8646665 commit e05867a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
4 changes: 1 addition & 3 deletions cell.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package excelize

import (
"encoding/xml"
"strconv"
"strings"
)
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 12 additions & 13 deletions excelize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
16 changes: 10 additions & 6 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e05867a

Please sign in to comment.