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