This closes #1383, skip empty rows when saving the spreadsheet to reduce file size

This commit is contained in:
xuri 2022-11-03 00:23:48 +08:00
parent db2d084ada
commit 4998b7b929
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
2 changed files with 28 additions and 6 deletions

View File

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

View File

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