forked from p30928647/excelize
This closes #1536, support fallback to default column width in sheet format property
This commit is contained in:
parent
ef3e81de8e
commit
08ba2723fe
6
col.go
6
col.go
|
@ -657,6 +657,9 @@ func (f *File) getColWidth(sheet string, col int) int {
|
||||||
return int(convertColWidthToPixels(width))
|
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.
|
// Optimization for when the column widths haven't changed.
|
||||||
return int(defaultColWidthPixels)
|
return int(defaultColWidthPixels)
|
||||||
}
|
}
|
||||||
|
@ -715,6 +718,9 @@ func (f *File) GetColWidth(sheet, col string) (float64, error) {
|
||||||
return width, err
|
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.
|
// Optimization for when the column widths haven't changed.
|
||||||
return defaultColWidth, err
|
return defaultColWidth, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -366,6 +366,15 @@ func TestColWidth(t *testing.T) {
|
||||||
assert.Equal(t, defaultColWidth, width)
|
assert.Equal(t, defaultColWidth, width)
|
||||||
assert.NoError(t, err)
|
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
|
// Test set and get column width with illegal cell reference
|
||||||
width, err = f.GetColWidth("Sheet1", "*")
|
width, err = f.GetColWidth("Sheet1", "*")
|
||||||
assert.Equal(t, defaultColWidth, width)
|
assert.Equal(t, defaultColWidth, width)
|
||||||
|
|
Loading…
Reference in New Issue