This support set column style with default width in sheet props settings (#1728)
This commit is contained in:
parent
41259b474f
commit
bce2789c11
26
col.go
26
col.go
|
@ -290,7 +290,7 @@ func (f *File) GetColVisible(sheet, col string) (bool, error) {
|
||||||
//
|
//
|
||||||
// err := f.SetColVisible("Sheet1", "D:F", false)
|
// err := f.SetColVisible("Sheet1", "D:F", false)
|
||||||
func (f *File) SetColVisible(sheet, columns string, visible bool) error {
|
func (f *File) SetColVisible(sheet, columns string, visible bool) error {
|
||||||
min, max, err := f.parseColRange(columns)
|
minVal, maxVal, err := f.parseColRange(columns)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -301,8 +301,8 @@ func (f *File) SetColVisible(sheet, columns string, visible bool) error {
|
||||||
ws.mu.Lock()
|
ws.mu.Lock()
|
||||||
defer ws.mu.Unlock()
|
defer ws.mu.Unlock()
|
||||||
colData := xlsxCol{
|
colData := xlsxCol{
|
||||||
Min: min,
|
Min: minVal,
|
||||||
Max: max,
|
Max: maxVal,
|
||||||
Width: float64Ptr(defaultColWidth),
|
Width: float64Ptr(defaultColWidth),
|
||||||
Hidden: !visible,
|
Hidden: !visible,
|
||||||
CustomWidth: true,
|
CustomWidth: true,
|
||||||
|
@ -427,7 +427,7 @@ func (f *File) SetColOutlineLevel(sheet, col string, level uint8) error {
|
||||||
//
|
//
|
||||||
// err = f.SetColStyle("Sheet1", "C:F", style)
|
// err = f.SetColStyle("Sheet1", "C:F", style)
|
||||||
func (f *File) SetColStyle(sheet, columns string, styleID int) error {
|
func (f *File) SetColStyle(sheet, columns string, styleID int) error {
|
||||||
min, max, err := f.parseColRange(columns)
|
minVal, maxVal, err := f.parseColRange(columns)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -453,10 +453,14 @@ func (f *File) SetColStyle(sheet, columns string, styleID int) error {
|
||||||
if ws.Cols == nil {
|
if ws.Cols == nil {
|
||||||
ws.Cols = &xlsxCols{}
|
ws.Cols = &xlsxCols{}
|
||||||
}
|
}
|
||||||
|
width := defaultColWidth
|
||||||
|
if ws.SheetFormatPr != nil && ws.SheetFormatPr.DefaultColWidth > 0 {
|
||||||
|
width = ws.SheetFormatPr.DefaultColWidth
|
||||||
|
}
|
||||||
ws.Cols.Col = flatCols(xlsxCol{
|
ws.Cols.Col = flatCols(xlsxCol{
|
||||||
Min: min,
|
Min: minVal,
|
||||||
Max: max,
|
Max: maxVal,
|
||||||
Width: float64Ptr(defaultColWidth),
|
Width: float64Ptr(width),
|
||||||
Style: styleID,
|
Style: styleID,
|
||||||
}, ws.Cols.Col, func(fc, c xlsxCol) xlsxCol {
|
}, ws.Cols.Col, func(fc, c xlsxCol) xlsxCol {
|
||||||
fc.BestFit = c.BestFit
|
fc.BestFit = c.BestFit
|
||||||
|
@ -470,7 +474,7 @@ func (f *File) SetColStyle(sheet, columns string, styleID int) error {
|
||||||
})
|
})
|
||||||
ws.mu.Unlock()
|
ws.mu.Unlock()
|
||||||
if rows := len(ws.SheetData.Row); rows > 0 {
|
if rows := len(ws.SheetData.Row); rows > 0 {
|
||||||
for col := min; col <= max; col++ {
|
for col := minVal; col <= maxVal; col++ {
|
||||||
from, _ := CoordinatesToCellName(col, 1)
|
from, _ := CoordinatesToCellName(col, 1)
|
||||||
to, _ := CoordinatesToCellName(col, rows)
|
to, _ := CoordinatesToCellName(col, rows)
|
||||||
err = f.SetCellStyle(sheet, from, to, styleID)
|
err = f.SetCellStyle(sheet, from, to, styleID)
|
||||||
|
@ -484,7 +488,7 @@ func (f *File) SetColStyle(sheet, columns string, styleID int) error {
|
||||||
//
|
//
|
||||||
// err := f.SetColWidth("Sheet1", "A", "H", 20)
|
// err := f.SetColWidth("Sheet1", "A", "H", 20)
|
||||||
func (f *File) SetColWidth(sheet, startCol, endCol string, width float64) error {
|
func (f *File) SetColWidth(sheet, startCol, endCol string, width float64) error {
|
||||||
min, max, err := f.parseColRange(startCol + ":" + endCol)
|
minVal, maxVal, err := f.parseColRange(startCol + ":" + endCol)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -501,8 +505,8 @@ func (f *File) SetColWidth(sheet, startCol, endCol string, width float64) error
|
||||||
ws.mu.Lock()
|
ws.mu.Lock()
|
||||||
defer ws.mu.Unlock()
|
defer ws.mu.Unlock()
|
||||||
col := xlsxCol{
|
col := xlsxCol{
|
||||||
Min: min,
|
Min: minVal,
|
||||||
Max: max,
|
Max: maxVal,
|
||||||
Width: float64Ptr(width),
|
Width: float64Ptr(width),
|
||||||
CustomWidth: true,
|
CustomWidth: true,
|
||||||
}
|
}
|
||||||
|
|
10
col_test.go
10
col_test.go
|
@ -366,6 +366,16 @@ func TestSetColStyle(t *testing.T) {
|
||||||
f.Styles = nil
|
f.Styles = nil
|
||||||
f.Pkg.Store(defaultXMLPathStyles, MacintoshCyrillicCharset)
|
f.Pkg.Store(defaultXMLPathStyles, MacintoshCyrillicCharset)
|
||||||
assert.EqualError(t, f.SetColStyle("Sheet1", "C:F", styleID), "XML syntax error on line 1: invalid UTF-8")
|
assert.EqualError(t, f.SetColStyle("Sheet1", "C:F", styleID), "XML syntax error on line 1: invalid UTF-8")
|
||||||
|
|
||||||
|
// Test set column style with worksheet properties columns default width settings
|
||||||
|
f = NewFile()
|
||||||
|
assert.NoError(t, f.SetSheetProps("Sheet1", &SheetPropsOptions{DefaultColWidth: float64Ptr(20)}))
|
||||||
|
style, err = f.NewStyle(&Style{Alignment: &Alignment{Vertical: "center"}})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NoError(t, f.SetColStyle("Sheet1", "A:Z", style))
|
||||||
|
width, err := f.GetColWidth("Sheet1", "B")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, 20.0, width)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestColWidth(t *testing.T) {
|
func TestColWidth(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue