From 9b5b74d4801f60daa580fd282ff9fa058bb03385 Mon Sep 17 00:00:00 2001 From: Ri Xu Date: Mon, 16 Oct 2017 10:42:43 +0800 Subject: [PATCH] Performance optimization, use the array index instead of the value in range. --- cell.go | 32 ++++++++++++++++---------------- chart.go | 12 ++++++------ col.go | 28 ++++++++++++++-------------- excelize.go | 8 ++++---- rows.go | 8 ++++---- 5 files changed, 44 insertions(+), 44 deletions(-) diff --git a/cell.go b/cell.go index c47b1f1..36b8ef9 100644 --- a/cell.go +++ b/cell.go @@ -91,11 +91,11 @@ func (f *File) GetCellValue(sheet, axis string) string { if rows < xAxis { return "" } - for _, v := range xlsx.SheetData.Row { - if v.R == row { - for _, r := range v.C { - if axis == r.R { - val, _ := r.getValueFrom(f, f.sharedStringsReader()) + for k := range xlsx.SheetData.Row { + if xlsx.SheetData.Row[k].R == row { + for i := range xlsx.SheetData.Row[k].C { + if axis == xlsx.SheetData.Row[k].C[i].R { + val, _ := xlsx.SheetData.Row[k].C[i].getValueFrom(f, f.sharedStringsReader()) return val } } @@ -161,12 +161,12 @@ func (f *File) GetCellFormula(sheet, axis string) string { if rows < xAxis { return "" } - for _, v := range xlsx.SheetData.Row { - if v.R == row { - for _, f := range v.C { - if axis == f.R { - if f.F != nil { - return f.F.Content + for k := range xlsx.SheetData.Row { + if xlsx.SheetData.Row[k].R == row { + for i := range xlsx.SheetData.Row[k].C { + if axis == xlsx.SheetData.Row[k].C[i].R { + if xlsx.SheetData.Row[k].C[i].F != nil { + 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 == "" { return link, target } - for _, h := range xlsx.Hyperlinks.Hyperlink { - if h.Ref == axis { + for h := range xlsx.Hyperlinks.Hyperlink { + if xlsx.Hyperlinks.Hyperlink[h].Ref == axis { link = true - target = h.Location - if h.RID != "" { - target = f.getSheetRelationshipsTargetByID(sheet, h.RID) + target = xlsx.Hyperlinks.Hyperlink[h].Location + if xlsx.Hyperlinks.Hyperlink[h].RID != "" { + target = f.getSheetRelationshipsTargetByID(sheet, xlsx.Hyperlinks.Hyperlink[h].RID) } } } diff --git a/chart.go b/chart.go index f36c83a..e6cb94f 100644 --- a/chart.go +++ b/chart.go @@ -550,23 +550,23 @@ func (f *File) drawScatterChart(formatSet *formatChart) *cPlotArea { // sets. func (f *File) drawChartSeries(formatSet *formatChart) *[]cSer { ser := []cSer{} - for k, v := range formatSet.Series { + for k := range formatSet.Series { ser = append(ser, cSer{ IDx: &attrValInt{Val: k}, Order: &attrValInt{Val: k}, Tx: &cTx{ StrRef: &cStrRef{ - F: v.Name, + F: formatSet.Series[k].Name, }, }, SpPr: f.drawChartSeriesSpPr(k, formatSet), Marker: f.drawChartSeriesMarker(k, formatSet), DPt: f.drawChartSeriesDPt(k, formatSet), DLbls: f.drawChartSeriesDLbls(formatSet), - Cat: f.drawChartSeriesCat(v, formatSet), - Val: f.drawChartSeriesVal(v, formatSet), - XVal: f.drawChartSeriesXVal(v, formatSet), - YVal: f.drawChartSeriesYVal(v, formatSet), + Cat: f.drawChartSeriesCat(formatSet.Series[k], formatSet), + Val: f.drawChartSeriesVal(formatSet.Series[k], formatSet), + XVal: f.drawChartSeriesXVal(formatSet.Series[k], formatSet), + YVal: f.drawChartSeriesYVal(formatSet.Series[k], formatSet), }) } return &ser diff --git a/col.go b/col.go index d9c4390..cc064c4 100644 --- a/col.go +++ b/col.go @@ -27,9 +27,9 @@ func (f *File) GetColVisible(sheet, column string) bool { if xlsx.Cols == nil { return visible } - for _, c := range xlsx.Cols.Col { - if c.Min <= col && col <= c.Max { - visible = !c.Hidden + for c := range xlsx.Cols.Col { + if xlsx.Cols.Col[c].Min <= col && col <= xlsx.Cols.Col[c].Max { + visible = !xlsx.Cols.Col[c].Hidden } } return visible @@ -55,9 +55,9 @@ func (f *File) SetColVisible(sheet, column string, visible bool) { xlsx.Cols = &cols return } - for _, v := range xlsx.Cols.Col { - if v.Min <= c && c <= v.Max { - col = v + for v := range xlsx.Cols.Col { + if xlsx.Cols.Col[v].Min <= c && c <= xlsx.Cols.Col[v].Max { + col = xlsx.Cols.Col[v] } } col.Min = c @@ -262,12 +262,12 @@ func (f *File) InsertCol(sheet, column string) { // func (f *File) RemoveCol(sheet, column string) { xlsx := f.workSheetReader(sheet) - for i, r := range xlsx.SheetData.Row { - for k, v := range r.C { + for r := range xlsx.SheetData.Row { + for k, v := range xlsx.SheetData.Row[r].C { axis := v.R col := string(strings.Map(letterOnlyMapF, axis)) 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. func completeCol(xlsx *xlsxWorksheet, row, cell int) { buffer := bytes.Buffer{} - for k, v := range xlsx.SheetData.Row { - if len(v.C) < cell { - start := len(v.C) + for r := range xlsx.SheetData.Row { + if len(xlsx.SheetData.Row[r].C) < cell { + start := len(xlsx.SheetData.Row[r].C) for iii := start; iii < cell; iii++ { buffer.WriteString(ToAlphaString(iii)) - buffer.WriteString(strconv.Itoa(k + 1)) - xlsx.SheetData.Row[k].C = append(xlsx.SheetData.Row[k].C, xlsxC{ + buffer.WriteString(strconv.Itoa(r + 1)) + xlsx.SheetData.Row[r].C = append(xlsx.SheetData.Row[r].C, xlsxC{ R: buffer.String(), }) buffer.Reset() diff --git a/excelize.go b/excelize.go index e3b5dac..95bce34 100644 --- a/excelize.go +++ b/excelize.go @@ -113,8 +113,8 @@ func checkSheet(xlsx *xlsxWorksheet) { } sheetData := xlsxSheetData{} existsRows := map[int]int{} - for k, v := range xlsx.SheetData.Row { - existsRows[v.R] = k + for k := range xlsx.SheetData.Row { + existsRows[xlsx.SheetData.Row[k].R] = k } for i := 0; i < row; i++ { _, ok := existsRows[i+1] @@ -167,8 +167,8 @@ func replaceWorkSheetsRelationshipsNameSpace(workbookMarshal string) string { func (f *File) UpdateLinkedValue() { for _, name := range f.GetSheetMap() { xlsx := f.workSheetReader(name) - for indexR, row := range xlsx.SheetData.Row { - for indexC, col := range row.C { + for indexR := range xlsx.SheetData.Row { + for indexC, col := range xlsx.SheetData.Row[indexR].C { if col.F != nil && col.V != "" { xlsx.SheetData.Row[indexR].C[indexC].V = "" xlsx.SheetData.Row[indexR].C[indexC].T = "" diff --git a/rows.go b/rows.go index 9bbbec2..78ed664 100644 --- a/rows.go +++ b/rows.go @@ -266,11 +266,11 @@ func (f *File) InsertRow(sheet string, row int) { // 3000 rows one sheet). func checkRow(xlsx *xlsxWorksheet) { buffer := bytes.Buffer{} - for k, v := range xlsx.SheetData.Row { - lenCol := len(v.C) + for k := range xlsx.SheetData.Row { + lenCol := len(xlsx.SheetData.Row[k].C) if lenCol > 0 { - endR := string(strings.Map(letterOnlyMapF, v.C[lenCol-1].R)) - endRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, v.C[lenCol-1].R)) + endR := string(strings.Map(letterOnlyMapF, xlsx.SheetData.Row[k].C[lenCol-1].R)) + endRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, xlsx.SheetData.Row[k].C[lenCol-1].R)) endCol := TitleToNumber(endR) + 1 if lenCol < endCol { oldRow := xlsx.SheetData.Row[k].C