Performance optimization, use the array index instead of the value in range.

This commit is contained in:
Ri Xu 2017-10-16 10:42:43 +08:00
parent 905be463ed
commit 9b5b74d480
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
5 changed files with 44 additions and 44 deletions

32
cell.go
View File

@ -91,11 +91,11 @@ func (f *File) GetCellValue(sheet, axis string) string {
if rows < xAxis { if rows < xAxis {
return "" return ""
} }
for _, v := range xlsx.SheetData.Row { for k := range xlsx.SheetData.Row {
if v.R == row { if xlsx.SheetData.Row[k].R == row {
for _, r := range v.C { for i := range xlsx.SheetData.Row[k].C {
if axis == r.R { if axis == xlsx.SheetData.Row[k].C[i].R {
val, _ := r.getValueFrom(f, f.sharedStringsReader()) val, _ := xlsx.SheetData.Row[k].C[i].getValueFrom(f, f.sharedStringsReader())
return val return val
} }
} }
@ -161,12 +161,12 @@ func (f *File) GetCellFormula(sheet, axis string) string {
if rows < xAxis { if rows < xAxis {
return "" return ""
} }
for _, v := range xlsx.SheetData.Row { for k := range xlsx.SheetData.Row {
if v.R == row { if xlsx.SheetData.Row[k].R == row {
for _, f := range v.C { for i := range xlsx.SheetData.Row[k].C {
if axis == f.R { if axis == xlsx.SheetData.Row[k].C[i].R {
if f.F != nil { if xlsx.SheetData.Row[k].C[i].F != nil {
return f.F.Content return xlsx.SheetData.Row[k].C[i].F.Content
} }
} }
} }
@ -256,12 +256,12 @@ func (f *File) GetCellHyperLink(sheet, axis string) (bool, string) {
if xlsx.Hyperlinks == nil || axis == "" { if xlsx.Hyperlinks == nil || axis == "" {
return link, target return link, target
} }
for _, h := range xlsx.Hyperlinks.Hyperlink { for h := range xlsx.Hyperlinks.Hyperlink {
if h.Ref == axis { if xlsx.Hyperlinks.Hyperlink[h].Ref == axis {
link = true link = true
target = h.Location target = xlsx.Hyperlinks.Hyperlink[h].Location
if h.RID != "" { if xlsx.Hyperlinks.Hyperlink[h].RID != "" {
target = f.getSheetRelationshipsTargetByID(sheet, h.RID) target = f.getSheetRelationshipsTargetByID(sheet, xlsx.Hyperlinks.Hyperlink[h].RID)
} }
} }
} }

View File

@ -550,23 +550,23 @@ func (f *File) drawScatterChart(formatSet *formatChart) *cPlotArea {
// sets. // sets.
func (f *File) drawChartSeries(formatSet *formatChart) *[]cSer { func (f *File) drawChartSeries(formatSet *formatChart) *[]cSer {
ser := []cSer{} ser := []cSer{}
for k, v := range formatSet.Series { for k := range formatSet.Series {
ser = append(ser, cSer{ ser = append(ser, cSer{
IDx: &attrValInt{Val: k}, IDx: &attrValInt{Val: k},
Order: &attrValInt{Val: k}, Order: &attrValInt{Val: k},
Tx: &cTx{ Tx: &cTx{
StrRef: &cStrRef{ StrRef: &cStrRef{
F: v.Name, F: formatSet.Series[k].Name,
}, },
}, },
SpPr: f.drawChartSeriesSpPr(k, formatSet), SpPr: f.drawChartSeriesSpPr(k, formatSet),
Marker: f.drawChartSeriesMarker(k, formatSet), Marker: f.drawChartSeriesMarker(k, formatSet),
DPt: f.drawChartSeriesDPt(k, formatSet), DPt: f.drawChartSeriesDPt(k, formatSet),
DLbls: f.drawChartSeriesDLbls(formatSet), DLbls: f.drawChartSeriesDLbls(formatSet),
Cat: f.drawChartSeriesCat(v, formatSet), Cat: f.drawChartSeriesCat(formatSet.Series[k], formatSet),
Val: f.drawChartSeriesVal(v, formatSet), Val: f.drawChartSeriesVal(formatSet.Series[k], formatSet),
XVal: f.drawChartSeriesXVal(v, formatSet), XVal: f.drawChartSeriesXVal(formatSet.Series[k], formatSet),
YVal: f.drawChartSeriesYVal(v, formatSet), YVal: f.drawChartSeriesYVal(formatSet.Series[k], formatSet),
}) })
} }
return &ser return &ser

28
col.go
View File

@ -27,9 +27,9 @@ func (f *File) GetColVisible(sheet, column string) bool {
if xlsx.Cols == nil { if xlsx.Cols == nil {
return visible return visible
} }
for _, c := range xlsx.Cols.Col { for c := range xlsx.Cols.Col {
if c.Min <= col && col <= c.Max { if xlsx.Cols.Col[c].Min <= col && col <= xlsx.Cols.Col[c].Max {
visible = !c.Hidden visible = !xlsx.Cols.Col[c].Hidden
} }
} }
return visible return visible
@ -55,9 +55,9 @@ func (f *File) SetColVisible(sheet, column string, visible bool) {
xlsx.Cols = &cols xlsx.Cols = &cols
return return
} }
for _, v := range xlsx.Cols.Col { for v := range xlsx.Cols.Col {
if v.Min <= c && c <= v.Max { if xlsx.Cols.Col[v].Min <= c && c <= xlsx.Cols.Col[v].Max {
col = v col = xlsx.Cols.Col[v]
} }
} }
col.Min = c col.Min = c
@ -262,12 +262,12 @@ func (f *File) InsertCol(sheet, column string) {
// //
func (f *File) RemoveCol(sheet, column string) { func (f *File) RemoveCol(sheet, column string) {
xlsx := f.workSheetReader(sheet) xlsx := f.workSheetReader(sheet)
for i, r := range xlsx.SheetData.Row { for r := range xlsx.SheetData.Row {
for k, v := range r.C { for k, v := range xlsx.SheetData.Row[r].C {
axis := v.R axis := v.R
col := string(strings.Map(letterOnlyMapF, axis)) col := string(strings.Map(letterOnlyMapF, axis))
if col == column { if col == column {
xlsx.SheetData.Row[i].C = append(xlsx.SheetData.Row[i].C[:k], xlsx.SheetData.Row[i].C[k+1:]...) xlsx.SheetData.Row[r].C = append(xlsx.SheetData.Row[r].C[:k], xlsx.SheetData.Row[r].C[k+1:]...)
} }
} }
} }
@ -278,13 +278,13 @@ func (f *File) RemoveCol(sheet, column string) {
// Completion column element tags of XML in a sheet. // Completion column element tags of XML in a sheet.
func completeCol(xlsx *xlsxWorksheet, row, cell int) { func completeCol(xlsx *xlsxWorksheet, row, cell int) {
buffer := bytes.Buffer{} buffer := bytes.Buffer{}
for k, v := range xlsx.SheetData.Row { for r := range xlsx.SheetData.Row {
if len(v.C) < cell { if len(xlsx.SheetData.Row[r].C) < cell {
start := len(v.C) start := len(xlsx.SheetData.Row[r].C)
for iii := start; iii < cell; iii++ { for iii := start; iii < cell; iii++ {
buffer.WriteString(ToAlphaString(iii)) buffer.WriteString(ToAlphaString(iii))
buffer.WriteString(strconv.Itoa(k + 1)) buffer.WriteString(strconv.Itoa(r + 1))
xlsx.SheetData.Row[k].C = append(xlsx.SheetData.Row[k].C, xlsxC{ xlsx.SheetData.Row[r].C = append(xlsx.SheetData.Row[r].C, xlsxC{
R: buffer.String(), R: buffer.String(),
}) })
buffer.Reset() buffer.Reset()

View File

@ -113,8 +113,8 @@ func checkSheet(xlsx *xlsxWorksheet) {
} }
sheetData := xlsxSheetData{} sheetData := xlsxSheetData{}
existsRows := map[int]int{} existsRows := map[int]int{}
for k, v := range xlsx.SheetData.Row { for k := range xlsx.SheetData.Row {
existsRows[v.R] = k existsRows[xlsx.SheetData.Row[k].R] = k
} }
for i := 0; i < row; i++ { for i := 0; i < row; i++ {
_, ok := existsRows[i+1] _, ok := existsRows[i+1]
@ -167,8 +167,8 @@ func replaceWorkSheetsRelationshipsNameSpace(workbookMarshal string) string {
func (f *File) UpdateLinkedValue() { func (f *File) UpdateLinkedValue() {
for _, name := range f.GetSheetMap() { for _, name := range f.GetSheetMap() {
xlsx := f.workSheetReader(name) xlsx := f.workSheetReader(name)
for indexR, row := range xlsx.SheetData.Row { for indexR := range xlsx.SheetData.Row {
for indexC, col := range row.C { for indexC, col := range xlsx.SheetData.Row[indexR].C {
if col.F != nil && col.V != "" { if col.F != nil && col.V != "" {
xlsx.SheetData.Row[indexR].C[indexC].V = "" xlsx.SheetData.Row[indexR].C[indexC].V = ""
xlsx.SheetData.Row[indexR].C[indexC].T = "" xlsx.SheetData.Row[indexR].C[indexC].T = ""

View File

@ -266,11 +266,11 @@ func (f *File) InsertRow(sheet string, row int) {
// 3000 rows one sheet). // 3000 rows one sheet).
func checkRow(xlsx *xlsxWorksheet) { func checkRow(xlsx *xlsxWorksheet) {
buffer := bytes.Buffer{} buffer := bytes.Buffer{}
for k, v := range xlsx.SheetData.Row { for k := range xlsx.SheetData.Row {
lenCol := len(v.C) lenCol := len(xlsx.SheetData.Row[k].C)
if lenCol > 0 { if lenCol > 0 {
endR := string(strings.Map(letterOnlyMapF, v.C[lenCol-1].R)) endR := string(strings.Map(letterOnlyMapF, xlsx.SheetData.Row[k].C[lenCol-1].R))
endRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, v.C[lenCol-1].R)) endRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, xlsx.SheetData.Row[k].C[lenCol-1].R))
endCol := TitleToNumber(endR) + 1 endCol := TitleToNumber(endR) + 1
if lenCol < endCol { if lenCol < endCol {
oldRow := xlsx.SheetData.Row[k].C oldRow := xlsx.SheetData.Row[k].C