summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxuri <xuri.me@gmail.com>2022-11-03 00:23:48 +0800
committerxuri <xuri.me@gmail.com>2022-11-03 00:23:48 +0800
commit4998b7b92980e1139b3f38d3c2b8cbc11b1a629d (patch)
tree4d1fe4ef4ff88b5fe5968d31f77e08aa24f37f86
parentdb2d084ada1a08a48967506b2f1852062168deec (diff)
This closes #1383, skip empty rows when saving the spreadsheet to reduce file size
-rw-r--r--rows.go7
-rw-r--r--sheet.go27
2 files changed, 28 insertions, 6 deletions
diff --git a/rows.go b/rows.go
index 5b21f29..bfea398 100644
--- a/rows.go
+++ b/rows.go
@@ -772,6 +772,13 @@ func checkRow(ws *xlsxWorksheet) error {
return nil
}
+// hasAttr determine if row non-default attributes.
+func (r *xlsxRow) hasAttr() bool {
+ return r.Spans != "" || r.S != 0 || r.CustomFormat || r.Ht != 0 ||
+ r.Hidden || r.CustomHeight || r.OutlineLevel != 0 || r.Collapsed ||
+ r.ThickTop || r.ThickBot || r.Ph
+}
+
// SetRowStyle provides a function to set the style of rows by given worksheet
// name, row range, and style ID. Note that this will overwrite the existing
// styles for the rows, it won't append or merge style with existing styles.
diff --git a/sheet.go b/sheet.go
index 070b47d..1346801 100644
--- a/sheet.go
+++ b/sheet.go
@@ -139,9 +139,11 @@ func (f *File) mergeExpandedCols(ws *xlsxWorksheet) {
// workSheetWriter provides a function to save xl/worksheets/sheet%d.xml after
// serialize structure.
func (f *File) workSheetWriter() {
- var arr []byte
- buffer := bytes.NewBuffer(arr)
- encoder := xml.NewEncoder(buffer)
+ var (
+ arr []byte
+ buffer = bytes.NewBuffer(arr)
+ encoder = xml.NewEncoder(buffer)
+ )
f.Sheet.Range(func(p, ws interface{}) bool {
if ws != nil {
sheet := ws.(*xlsxWorksheet)
@@ -151,9 +153,7 @@ func (f *File) workSheetWriter() {
if sheet.Cols != nil && len(sheet.Cols.Col) > 0 {
f.mergeExpandedCols(sheet)
}
- for k, v := range sheet.SheetData.Row {
- sheet.SheetData.Row[k].C = trimCell(v.C)
- }
+ sheet.SheetData.Row = trimRow(&sheet.SheetData)
if sheet.SheetPr != nil || sheet.Drawing != nil || sheet.Hyperlinks != nil || sheet.Picture != nil || sheet.TableParts != nil {
f.addNameSpaces(p.(string), SourceRelationship)
}
@@ -178,6 +178,21 @@ func (f *File) workSheetWriter() {
})
}
+// trimRow provides a function to trim empty rows.
+func trimRow(sheetData *xlsxSheetData) []xlsxRow {
+ var (
+ row xlsxRow
+ rows []xlsxRow
+ )
+ for k, v := range sheetData.Row {
+ row = sheetData.Row[k]
+ if row.C = trimCell(v.C); len(row.C) != 0 || row.hasAttr() {
+ rows = append(rows, row)
+ }
+ }
+ return rows
+}
+
// trimCell provides a function to trim blank cells which created by fillColumns.
func trimCell(column []xlsxC) []xlsxC {
rowFull := true