summaryrefslogtreecommitdiff
path: root/sheet.go
diff options
context:
space:
mode:
authorxuri <xuri.me@gmail.com>2019-11-10 16:51:15 +0800
committerGitHub <noreply@github.com>2019-11-10 16:51:15 +0800
commitc8c8397751e994dca05467e7615f6ee77704775b (patch)
tree023a4a3d98b0efcd38860a062f4fe26de81cbb81 /sheet.go
parent6abf8bf9723512086f009ca574bde1d6682fc83d (diff)
parentbf9a8355494eac18812f3caf6d469962824f627f (diff)
Fix #494 Merge pull request #514 from mlh758/fix-494-write-allocations
Reduce allocations when writing
Diffstat (limited to 'sheet.go')
-rw-r--r--sheet.go18
1 files changed, 15 insertions, 3 deletions
diff --git a/sheet.go b/sheet.go
index 335c4fc..43c7cc0 100644
--- a/sheet.go
+++ b/sheet.go
@@ -117,12 +117,19 @@ func (f *File) workSheetWriter() {
}
}
-// trimCell provides a function to trim blank cells which created by completeCol.
+// trimCell provides a function to trim blank cells which created by fillColumns.
func trimCell(column []xlsxC) []xlsxC {
+ rowFull := true
+ for i := range column {
+ rowFull = column[i].hasValue() && rowFull
+ }
+ if rowFull {
+ return column
+ }
col := make([]xlsxC, len(column))
i := 0
for _, c := range column {
- if c.S != 0 || c.V != "" || c.F != nil || c.T != "" {
+ if c.hasValue() {
col[i] = c
i++
}
@@ -1404,12 +1411,17 @@ func (f *File) relsReader(path string) *xlsxRelationships {
// fillSheetData ensures there are enough rows, and columns in the chosen
// row to accept data. Missing rows are backfilled and given their row number
+// Uses the last populated row as a hint for the size of the next row to add
func prepareSheetXML(xlsx *xlsxWorksheet, col int, row int) {
rowCount := len(xlsx.SheetData.Row)
+ sizeHint := 0
+ if rowCount > 0 {
+ sizeHint = len(xlsx.SheetData.Row[rowCount-1].C)
+ }
if rowCount < row {
// append missing rows
for rowIdx := rowCount; rowIdx < row; rowIdx++ {
- xlsx.SheetData.Row = append(xlsx.SheetData.Row, xlsxRow{R: rowIdx + 1})
+ xlsx.SheetData.Row = append(xlsx.SheetData.Row, xlsxRow{R: rowIdx + 1, C: make([]xlsxC, 0, sizeHint)})
}
}
rowData := &xlsx.SheetData.Row[row-1]