This closes #1536, support fallback to default column width in sheet format property

This commit is contained in:
xuri 2023-05-18 20:33:16 +08:00 committed by GitHub
parent ef3e81de8e
commit 08ba2723fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 0 deletions

6
col.go
View File

@ -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
}

View File

@ -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)