From 57275db22e3a7f9dce51556f7579b704b8033dcb Mon Sep 17 00:00:00 2001 From: xuri Date: Sun, 14 Nov 2021 00:17:31 +0800 Subject: This closes #1057, merge column styles to reduce spreadsheet size --- sheet.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'sheet.go') diff --git a/sheet.go b/sheet.go index dc93513..43e277a 100644 --- a/sheet.go +++ b/sheet.go @@ -24,6 +24,7 @@ import ( "path/filepath" "reflect" "regexp" + "sort" "strconv" "strings" "unicode/utf8" @@ -149,6 +150,37 @@ func (f *File) workBookWriter() { } } +// mergeExpandedCols merge expanded columns. +func (f *File) mergeExpandedCols(ws *xlsxWorksheet) { + sort.Slice(ws.Cols.Col, func(i, j int) bool { + return ws.Cols.Col[i].Min < ws.Cols.Col[j].Min + }) + columns := []xlsxCol{} + for i, n := 0, len(ws.Cols.Col); i < n; { + left := i + for i++; i < n && reflect.DeepEqual( + xlsxCol{ + BestFit: ws.Cols.Col[i-1].BestFit, + Collapsed: ws.Cols.Col[i-1].Collapsed, + CustomWidth: ws.Cols.Col[i-1].CustomWidth, + Hidden: ws.Cols.Col[i-1].Hidden, + Max: ws.Cols.Col[i-1].Max + 1, + Min: ws.Cols.Col[i-1].Min + 1, + OutlineLevel: ws.Cols.Col[i-1].OutlineLevel, + Phonetic: ws.Cols.Col[i-1].Phonetic, + Style: ws.Cols.Col[i-1].Style, + Width: ws.Cols.Col[i-1].Width, + }, ws.Cols.Col[i]); i++ { + } + column := deepcopy.Copy(ws.Cols.Col[left]).(xlsxCol) + if left < i-1 { + column.Max = ws.Cols.Col[i-1].Min + } + columns = append(columns, column) + } + ws.Cols.Col = columns +} + // workSheetWriter provides a function to save xl/worksheets/sheet%d.xml after // serialize structure. func (f *File) workSheetWriter() { @@ -161,6 +193,9 @@ func (f *File) workSheetWriter() { if sheet.MergeCells != nil && len(sheet.MergeCells.Cells) > 0 { _ = f.mergeOverlapCells(sheet) } + 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) } -- cgit v1.2.1