Skip to content

Commit

Permalink
This closes #1536, support fallback to default column width in sheet …
Browse files Browse the repository at this point in the history
…format property
  • Loading branch information
xuri authored May 18, 2023
1 parent ef3e81d commit 08ba272
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions col.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 9 additions & 0 deletions col_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 08ba272

Please sign in to comment.