浏览代码

Avoid looping over unused rows and columns in SetCellStyle.

The recent improvement to SetCellStyle still loops
over all the rows before the area, moving the area checks
into the loop is more concise and faster.

Since the loop now covers the correct area by construction,
the inner loop check checkCellInArea is no longer needed.
Martin Sandve Alnæs 7 年之前
父节点
当前提交
e556c25047
共有 1 个文件被更改,包括 4 次插入21 次删除
  1. 4 21
      styles.go

+ 4 - 21
styles.go

@@ -2307,6 +2307,7 @@ func (f *File) SetCellStyle(sheet, hcell, vcell string, styleID int) {
 	vyAxis := vrow - 1
 	vyAxis := vrow - 1
 	vxAxis := TitleToNumber(vcol)
 	vxAxis := TitleToNumber(vcol)
 
 
+	// Correct the coordinate area, such correct C1:B3 to B1:C3.
 	if vxAxis < hxAxis {
 	if vxAxis < hxAxis {
 		hcell, vcell = vcell, hcell
 		hcell, vcell = vcell, hcell
 		vxAxis, hxAxis = hxAxis, vxAxis
 		vxAxis, hxAxis = hxAxis, vxAxis
@@ -2317,32 +2318,14 @@ func (f *File) SetCellStyle(sheet, hcell, vcell string, styleID int) {
 		vyAxis, hyAxis = hyAxis, vyAxis
 		vyAxis, hyAxis = hyAxis, vyAxis
 	}
 	}
 
 
-	// Correct the coordinate area, such correct C1:B3 to B1:C3.
-	hcell = ToAlphaString(hxAxis) + strconv.Itoa(hyAxis+1)
-	vcell = ToAlphaString(vxAxis) + strconv.Itoa(vyAxis+1)
-
 	xlsx := f.workSheetReader(sheet)
 	xlsx := f.workSheetReader(sheet)
 
 
 	completeRow(xlsx, vyAxis+1, vxAxis+1)
 	completeRow(xlsx, vyAxis+1, vxAxis+1)
 	completeCol(xlsx, vyAxis+1, vxAxis+1)
 	completeCol(xlsx, vyAxis+1, vxAxis+1)
 
 
-	for r, row := range xlsx.SheetData.Row {
-		if r < hyAxis {
-			continue
-		} else if r > vyAxis {
-			break
-		}
-
-		for k, c := range row.C {
-			if k < hxAxis {
-				continue
-			} else if k > vxAxis {
-				break
-			}
-
-			if checkCellInArea(c.R, hcell+":"+vcell) {
-				xlsx.SheetData.Row[r].C[k].S = styleID
-			}
+	for r := hyAxis; r <= vyAxis; r++ {
+		for k := hxAxis; k <= vxAxis; k++ {
+			xlsx.SheetData.Row[r].C[k].S = styleID
 		}
 		}
 	}
 	}
 }
 }