From 08ba2723fe6978a3e62e55f5a8cd0f856b1ecb6f Mon Sep 17 00:00:00 2001 From: xuri Date: Thu, 18 May 2023 20:33:16 +0800 Subject: [PATCH] This closes #1536, support fallback to default column width in sheet format property --- col.go | 6 ++++++ col_test.go | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/col.go b/col.go index e396005b..4d19a2aa 100644 --- a/col.go +++ b/col.go @@ -657,6 +657,9 @@ func (f *File) getColWidth(sheet string, col int) int { return int(convertColWidthToPixels(width)) } } + if ws.SheetFormatPr != nil && ws.SheetFormatPr.DefaultColWidth > 0 { + return int(convertColWidthToPixels(ws.SheetFormatPr.DefaultColWidth)) + } // Optimization for when the column widths haven't changed. return int(defaultColWidthPixels) } @@ -715,6 +718,9 @@ func (f *File) GetColWidth(sheet, col string) (float64, error) { return width, err } } + if ws.SheetFormatPr != nil && ws.SheetFormatPr.DefaultColWidth > 0 { + return ws.SheetFormatPr.DefaultColWidth, err + } // Optimization for when the column widths haven't changed. return defaultColWidth, err } diff --git a/col_test.go b/col_test.go index 335bee06..ce7c3808 100644 --- a/col_test.go +++ b/col_test.go @@ -366,6 +366,15 @@ func TestColWidth(t *testing.T) { assert.Equal(t, defaultColWidth, width) assert.NoError(t, err) + ws, ok := f.Sheet.Load("xl/worksheets/sheet1.xml") + assert.True(t, ok) + ws.(*xlsxWorksheet).SheetFormatPr = &xlsxSheetFormatPr{DefaultColWidth: 10} + ws.(*xlsxWorksheet).Cols = nil + width, err = f.GetColWidth("Sheet1", "A") + assert.NoError(t, err) + assert.Equal(t, 10.0, width) + assert.Equal(t, 76, f.getColWidth("Sheet1", 1)) + // Test set and get column width with illegal cell reference width, err = f.GetColWidth("Sheet1", "*") assert.Equal(t, defaultColWidth, width)