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 {
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)
}
}
}

View File

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

28
col.go
View File

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

View File

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

View File

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