Merge pull request #565 from Amodio/master

SetColVisible() can parse a column range + typos.
formula
xuri 5 years ago committed by GitHub
commit a691c1048d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -26,7 +26,7 @@ const (
// worksheet name and column name. For example, get visible state of column D // worksheet name and column name. For example, get visible state of column D
// in Sheet1: // in Sheet1:
// //
// visiable, err := f.GetColVisible("Sheet1", "D") // visible, err := f.GetColVisible("Sheet1", "D")
// //
func (f *File) GetColVisible(sheet, col string) (bool, error) { func (f *File) GetColVisible(sheet, col string) (bool, error) {
visible := true visible := true
@ -52,45 +52,58 @@ func (f *File) GetColVisible(sheet, col string) (bool, error) {
return visible, err return visible, err
} }
// SetColVisible provides a function to set visible of a single column by given // SetColVisible provides a function to set visible columns by given worksheet
// worksheet name and column name. For example, hide column D in Sheet1: // name, columns range and visibility.
//
// For example hide column D on Sheet1:
// //
// err := f.SetColVisible("Sheet1", "D", false) // err := f.SetColVisible("Sheet1", "D", false)
// //
func (f *File) SetColVisible(sheet, col string, visible bool) error { // Hide the columns from D to F (included)
colNum, err := ColumnNameToNumber(col) //
// err := f.SetColVisible("Sheet1", "D:F", false)
//
func (f *File) SetColVisible(sheet, columns string, visible bool) error {
var max int
colsTab := strings.Split(columns, ":")
min, err := ColumnNameToNumber(colsTab[0])
if err != nil { if err != nil {
return err return err
} }
colData := xlsxCol{ if len(colsTab) == 2 {
Min: colNum, max, err = ColumnNameToNumber(colsTab[1])
Max: colNum, if err != nil {
Hidden: !visible, return err
CustomWidth: true, }
} else {
max = min
}
if max < min {
min, max = max, min
} }
xlsx, err := f.workSheetReader(sheet) xlsx, err := f.workSheetReader(sheet)
if err != nil { if err != nil {
return err return err
} }
if xlsx.Cols == nil { colData := xlsxCol{
Min: min,
Max: max,
Width: 9, // default width
Hidden: !visible,
CustomWidth: true,
}
if xlsx.Cols != nil {
xlsx.Cols.Col = append(xlsx.Cols.Col, colData)
} else {
cols := xlsxCols{} cols := xlsxCols{}
cols.Col = append(cols.Col, colData) cols.Col = append(cols.Col, colData)
xlsx.Cols = &cols xlsx.Cols = &cols
return err
}
for v := range xlsx.Cols.Col {
if xlsx.Cols.Col[v].Min <= colNum && colNum <= xlsx.Cols.Col[v].Max {
colData = xlsx.Cols.Col[v]
}
} }
colData.Min = colNum return nil
colData.Max = colNum
colData.Hidden = !visible
colData.CustomWidth = true
xlsx.Cols.Col = append(xlsx.Cols.Col, colData)
return err
} }
// GetColOutlineLevel provides a function to get outline level of a single // GetColOutlineLevel provides a function to get outline level of a single
// column by given worksheet name and column name. For example, get outline // column by given worksheet name and column name. For example, get outline
// level of column D in Sheet1: // level of column D in Sheet1:

@ -12,17 +12,41 @@ func TestColumnVisibility(t *testing.T) {
f, err := prepareTestBook1() f, err := prepareTestBook1()
assert.NoError(t, err) assert.NoError(t, err)
// Hide/display a column with SetColVisible
assert.NoError(t, f.SetColVisible("Sheet1", "F", false)) assert.NoError(t, f.SetColVisible("Sheet1", "F", false))
assert.NoError(t, f.SetColVisible("Sheet1", "F", true)) assert.NoError(t, f.SetColVisible("Sheet1", "F", true))
visible, err := f.GetColVisible("Sheet1", "F") visible, err := f.GetColVisible("Sheet1", "F")
assert.Equal(t, true, visible) assert.Equal(t, true, visible)
assert.NoError(t, err) assert.NoError(t, err)
// Test get column visiable on not exists worksheet. // Test hiding a few columns SetColVisible(...false)...
assert.NoError(t, f.SetColVisible("Sheet1", "F:V", false))
visible, err = f.GetColVisible("Sheet1", "F")
assert.Equal(t, false, visible)
assert.NoError(t, err)
visible, err = f.GetColVisible("Sheet1", "U")
assert.Equal(t, false, visible)
assert.NoError(t, err)
visible, err = f.GetColVisible("Sheet1", "V")
assert.Equal(t, false, visible)
assert.NoError(t, err)
// ...and displaying them back SetColVisible(...true)
assert.NoError(t, f.SetColVisible("Sheet1", "F:V", true))
visible, err = f.GetColVisible("Sheet1", "F")
assert.Equal(t, true, visible)
assert.NoError(t, err)
visible, err = f.GetColVisible("Sheet1", "U")
assert.Equal(t, true, visible)
assert.NoError(t, err)
visible, err = f.GetColVisible("Sheet1", "G")
assert.Equal(t, true, visible)
assert.NoError(t, err)
// Test get column visible on an inexistent worksheet.
_, err = f.GetColVisible("SheetN", "F") _, err = f.GetColVisible("SheetN", "F")
assert.EqualError(t, err, "sheet SheetN is not exist") assert.EqualError(t, err, "sheet SheetN is not exist")
// Test get column visiable with illegal cell coordinates. // Test get column visible with illegal cell coordinates.
_, err = f.GetColVisible("Sheet1", "*") _, err = f.GetColVisible("Sheet1", "*")
assert.EqualError(t, err, `invalid column name "*"`) assert.EqualError(t, err, `invalid column name "*"`)
assert.EqualError(t, f.SetColVisible("Sheet1", "*", false), `invalid column name "*"`) assert.EqualError(t, f.SetColVisible("Sheet1", "*", false), `invalid column name "*"`)

Loading…
Cancel
Save