Skip to content

Commit

Permalink
This support set column style with default width in sheet props setti…
Browse files Browse the repository at this point in the history
…ngs (qax-os#1728)
  • Loading branch information
zcgly authored Nov 24, 2023
1 parent 41259b4 commit bce2789
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
26 changes: 15 additions & 11 deletions col.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func (f *File) GetColVisible(sheet, col string) (bool, error) {
//
// err := f.SetColVisible("Sheet1", "D:F", false)
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 {
return err
}
Expand All @@ -301,8 +301,8 @@ func (f *File) SetColVisible(sheet, columns string, visible bool) error {
ws.mu.Lock()
defer ws.mu.Unlock()
colData := xlsxCol{
Min: min,
Max: max,
Min: minVal,
Max: maxVal,
Width: float64Ptr(defaultColWidth),
Hidden: !visible,
CustomWidth: true,
Expand Down Expand Up @@ -427,7 +427,7 @@ func (f *File) SetColOutlineLevel(sheet, col string, level uint8) error {
//
// err = f.SetColStyle("Sheet1", "C:F", style)
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 {
return err
}
Expand All @@ -453,10 +453,14 @@ func (f *File) SetColStyle(sheet, columns string, styleID int) error {
if ws.Cols == nil {
ws.Cols = &xlsxCols{}
}
width := defaultColWidth
if ws.SheetFormatPr != nil && ws.SheetFormatPr.DefaultColWidth > 0 {
width = ws.SheetFormatPr.DefaultColWidth
}
ws.Cols.Col = flatCols(xlsxCol{
Min: min,
Max: max,
Width: float64Ptr(defaultColWidth),
Min: minVal,
Max: maxVal,
Width: float64Ptr(width),
Style: styleID,
}, ws.Cols.Col, func(fc, c xlsxCol) xlsxCol {
fc.BestFit = c.BestFit
Expand All @@ -470,7 +474,7 @@ func (f *File) SetColStyle(sheet, columns string, styleID int) error {
})
ws.mu.Unlock()
if rows := len(ws.SheetData.Row); rows > 0 {
for col := min; col <= max; col++ {
for col := minVal; col <= maxVal; col++ {
from, _ := CoordinatesToCellName(col, 1)
to, _ := CoordinatesToCellName(col, rows)
err = f.SetCellStyle(sheet, from, to, styleID)
Expand All @@ -484,7 +488,7 @@ func (f *File) SetColStyle(sheet, columns string, styleID int) error {
//
// err := f.SetColWidth("Sheet1", "A", "H", 20)
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 {
return err
}
Expand All @@ -501,8 +505,8 @@ func (f *File) SetColWidth(sheet, startCol, endCol string, width float64) error
ws.mu.Lock()
defer ws.mu.Unlock()
col := xlsxCol{
Min: min,
Max: max,
Min: minVal,
Max: maxVal,
Width: float64Ptr(width),
CustomWidth: true,
}
Expand Down
10 changes: 10 additions & 0 deletions col_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,16 @@ func TestSetColStyle(t *testing.T) {
f.Styles = nil
f.Pkg.Store(defaultXMLPathStyles, MacintoshCyrillicCharset)
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) {
Expand Down

0 comments on commit bce2789

Please sign in to comment.