Skip to content

Commit a691c10

Browse files
authored
Merge pull request qax-os#565 from Amodio/master
SetColVisible() can parse a column range + typos.
2 parents cbc3fd2 + 68754a2 commit a691c10

File tree

2 files changed

+62
-25
lines changed

2 files changed

+62
-25
lines changed

col.go

+36-23
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const (
2626
// worksheet name and column name. For example, get visible state of column D
2727
// in Sheet1:
2828
//
29-
// visiable, err := f.GetColVisible("Sheet1", "D")
29+
// visible, err := f.GetColVisible("Sheet1", "D")
3030
//
3131
func (f *File) GetColVisible(sheet, col string) (bool, error) {
3232
visible := true
@@ -52,45 +52,58 @@ func (f *File) GetColVisible(sheet, col string) (bool, error) {
5252
return visible, err
5353
}
5454

55-
// SetColVisible provides a function to set visible of a single column by given
56-
// worksheet name and column name. For example, hide column D in Sheet1:
55+
// SetColVisible provides a function to set visible columns by given worksheet
56+
// name, columns range and visibility.
57+
//
58+
// For example hide column D on Sheet1:
5759
//
5860
// err := f.SetColVisible("Sheet1", "D", false)
5961
//
60-
func (f *File) SetColVisible(sheet, col string, visible bool) error {
61-
colNum, err := ColumnNameToNumber(col)
62+
// Hide the columns from D to F (included)
63+
//
64+
// err := f.SetColVisible("Sheet1", "D:F", false)
65+
//
66+
func (f *File) SetColVisible(sheet, columns string, visible bool) error {
67+
var max int
68+
69+
colsTab := strings.Split(columns, ":")
70+
min, err := ColumnNameToNumber(colsTab[0])
6271
if err != nil {
6372
return err
6473
}
65-
colData := xlsxCol{
66-
Min: colNum,
67-
Max: colNum,
68-
Hidden: !visible,
69-
CustomWidth: true,
74+
if len(colsTab) == 2 {
75+
max, err = ColumnNameToNumber(colsTab[1])
76+
if err != nil {
77+
return err
78+
}
79+
} else {
80+
max = min
81+
}
82+
if max < min {
83+
min, max = max, min
7084
}
7185
xlsx, err := f.workSheetReader(sheet)
7286
if err != nil {
7387
return err
7488
}
75-
if xlsx.Cols == nil {
89+
colData := xlsxCol{
90+
Min: min,
91+
Max: max,
92+
Width: 9, // default width
93+
Hidden: !visible,
94+
CustomWidth: true,
95+
}
96+
if xlsx.Cols != nil {
97+
xlsx.Cols.Col = append(xlsx.Cols.Col, colData)
98+
} else {
7699
cols := xlsxCols{}
77100
cols.Col = append(cols.Col, colData)
78101
xlsx.Cols = &cols
79-
return err
80102
}
81-
for v := range xlsx.Cols.Col {
82-
if xlsx.Cols.Col[v].Min <= colNum && colNum <= xlsx.Cols.Col[v].Max {
83-
colData = xlsx.Cols.Col[v]
84-
}
85-
}
86-
colData.Min = colNum
87-
colData.Max = colNum
88-
colData.Hidden = !visible
89-
colData.CustomWidth = true
90-
xlsx.Cols.Col = append(xlsx.Cols.Col, colData)
91-
return err
103+
return nil
92104
}
93105

106+
94107
// GetColOutlineLevel provides a function to get outline level of a single
95108
// column by given worksheet name and column name. For example, get outline
96109
// level of column D in Sheet1:

col_test.go

+26-2
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,41 @@ func TestColumnVisibility(t *testing.T) {
1212
f, err := prepareTestBook1()
1313
assert.NoError(t, err)
1414

15+
// Hide/display a column with SetColVisible
1516
assert.NoError(t, f.SetColVisible("Sheet1", "F", false))
1617
assert.NoError(t, f.SetColVisible("Sheet1", "F", true))
1718
visible, err := f.GetColVisible("Sheet1", "F")
1819
assert.Equal(t, true, visible)
1920
assert.NoError(t, err)
2021

21-
// Test get column visiable on not exists worksheet.
22+
// Test hiding a few columns SetColVisible(...false)...
23+
assert.NoError(t, f.SetColVisible("Sheet1", "F:V", false))
24+
visible, err = f.GetColVisible("Sheet1", "F")
25+
assert.Equal(t, false, visible)
26+
assert.NoError(t, err)
27+
visible, err = f.GetColVisible("Sheet1", "U")
28+
assert.Equal(t, false, visible)
29+
assert.NoError(t, err)
30+
visible, err = f.GetColVisible("Sheet1", "V")
31+
assert.Equal(t, false, visible)
32+
assert.NoError(t, err)
33+
// ...and displaying them back SetColVisible(...true)
34+
assert.NoError(t, f.SetColVisible("Sheet1", "F:V", true))
35+
visible, err = f.GetColVisible("Sheet1", "F")
36+
assert.Equal(t, true, visible)
37+
assert.NoError(t, err)
38+
visible, err = f.GetColVisible("Sheet1", "U")
39+
assert.Equal(t, true, visible)
40+
assert.NoError(t, err)
41+
visible, err = f.GetColVisible("Sheet1", "G")
42+
assert.Equal(t, true, visible)
43+
assert.NoError(t, err)
44+
45+
// Test get column visible on an inexistent worksheet.
2246
_, err = f.GetColVisible("SheetN", "F")
2347
assert.EqualError(t, err, "sheet SheetN is not exist")
2448

25-
// Test get column visiable with illegal cell coordinates.
49+
// Test get column visible with illegal cell coordinates.
2650
_, err = f.GetColVisible("Sheet1", "*")
2751
assert.EqualError(t, err, `invalid column name "*"`)
2852
assert.EqualError(t, f.SetColVisible("Sheet1", "*", false), `invalid column name "*"`)

0 commit comments

Comments
 (0)