Skip to content

Commit 08ba272

Browse files
authored
This closes #1536, support fallback to default column width in sheet format property
1 parent ef3e81d commit 08ba272

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

col.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,9 @@ func (f *File) getColWidth(sheet string, col int) int {
657657
return int(convertColWidthToPixels(width))
658658
}
659659
}
660+
if ws.SheetFormatPr != nil && ws.SheetFormatPr.DefaultColWidth > 0 {
661+
return int(convertColWidthToPixels(ws.SheetFormatPr.DefaultColWidth))
662+
}
660663
// Optimization for when the column widths haven't changed.
661664
return int(defaultColWidthPixels)
662665
}
@@ -715,6 +718,9 @@ func (f *File) GetColWidth(sheet, col string) (float64, error) {
715718
return width, err
716719
}
717720
}
721+
if ws.SheetFormatPr != nil && ws.SheetFormatPr.DefaultColWidth > 0 {
722+
return ws.SheetFormatPr.DefaultColWidth, err
723+
}
718724
// Optimization for when the column widths haven't changed.
719725
return defaultColWidth, err
720726
}

col_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,15 @@ func TestColWidth(t *testing.T) {
366366
assert.Equal(t, defaultColWidth, width)
367367
assert.NoError(t, err)
368368

369+
ws, ok := f.Sheet.Load("xl/worksheets/sheet1.xml")
370+
assert.True(t, ok)
371+
ws.(*xlsxWorksheet).SheetFormatPr = &xlsxSheetFormatPr{DefaultColWidth: 10}
372+
ws.(*xlsxWorksheet).Cols = nil
373+
width, err = f.GetColWidth("Sheet1", "A")
374+
assert.NoError(t, err)
375+
assert.Equal(t, 10.0, width)
376+
assert.Equal(t, 76, f.getColWidth("Sheet1", 1))
377+
369378
// Test set and get column width with illegal cell reference
370379
width, err = f.GetColWidth("Sheet1", "*")
371380
assert.Equal(t, defaultColWidth, width)

0 commit comments

Comments
 (0)