summaryrefslogtreecommitdiff
path: root/sheet.go
diff options
context:
space:
mode:
authorMichael <osiris2918@gmail.com>2019-04-16 01:50:16 -0500
committerxuri <xuri.me@gmail.com>2019-04-16 14:50:16 +0800
commit0f9170a03b9fe19c1c22687fba8bcbdfd69a6347 (patch)
treebc7e73c690d274548530199de14c4d668d45c118 /sheet.go
parenta88459d5f1e83006ba421f334a1513d1c231eb6b (diff)
Resolve #382, rewrite prepareSheetXML to scale linearly (#383)
* Rewrite prepareSheetXML to scale linearly We don't need to backfill columns into every row for most purposes Provided makeContiguousColumns for setting styles where we do need it for a specific region. Added a benchmark to monitor progress. For 50,000 rows this went from about 11 seconds to 1 second. The improvements are more dramatic as the row/column count increases. * Assigning that row value was redundant
Diffstat (limited to 'sheet.go')
-rw-r--r--sheet.go29
1 files changed, 19 insertions, 10 deletions
diff --git a/sheet.go b/sheet.go
index 72d8921..5c681d2 100644
--- a/sheet.go
+++ b/sheet.go
@@ -1072,8 +1072,8 @@ func (f *File) workSheetRelsWriter() {
}
}
-// fillSheetData fill missing row and cell XML data to made it continuous from
-// first cell [1, 1] to last cell [col, row]
+// fillSheetData ensures there are enough rows, and columns in the chosen
+// row to accept data. Missing rows are backfilled and given their row number
func prepareSheetXML(xlsx *xlsxWorksheet, col int, row int) {
rowCount := len(xlsx.SheetData.Row)
if rowCount < row {
@@ -1082,14 +1082,23 @@ func prepareSheetXML(xlsx *xlsxWorksheet, col int, row int) {
xlsx.SheetData.Row = append(xlsx.SheetData.Row, xlsxRow{R: rowIdx + 1})
}
}
- for rowIdx := range xlsx.SheetData.Row {
- rowData := &xlsx.SheetData.Row[rowIdx] // take reference
- cellCount := len(rowData.C)
- if cellCount < col {
- for colIdx := cellCount; colIdx < col; colIdx++ {
- cellName, _ := CoordinatesToCellName(colIdx+1, rowIdx+1)
- rowData.C = append(rowData.C, xlsxC{R: cellName})
- }
+ rowData := &xlsx.SheetData.Row[row-1]
+ fillColumns(rowData, col, row)
+}
+
+func fillColumns(rowData *xlsxRow, col, row int) {
+ cellCount := len(rowData.C)
+ if cellCount < col {
+ for colIdx := cellCount; colIdx < col; colIdx++ {
+ cellName, _ := CoordinatesToCellName(colIdx+1, row)
+ rowData.C = append(rowData.C, xlsxC{R: cellName})
}
}
}
+
+func makeContiguousColumns(xlsx *xlsxWorksheet, fromRow, toRow, colCount int) {
+ for ; fromRow < toRow; fromRow++ {
+ rowData := &xlsx.SheetData.Row[fromRow-1]
+ fillColumns(rowData, colCount, fromRow)
+ }
+}