From 68754a2075f12ba3c2bdf3646e4a3e7a3fd829f5 Mon Sep 17 00:00:00 2001 From: Jacques Boscq Date: Tue, 21 Jan 2020 23:42:44 +0100 Subject: [PATCH] SetColVisible() can parse a column range + typos. --- col.go | 59 ++++++++++++++++++++++++++++++++--------------------- col_test.go | 28 +++++++++++++++++++++++-- 2 files changed, 62 insertions(+), 25 deletions(-) diff --git a/col.go b/col.go index f7e6bcd..ff771f1 100644 --- a/col.go +++ b/col.go @@ -26,7 +26,7 @@ const ( // worksheet name and column name. For example, get visible state of column D // in Sheet1: // -// visiable, err := f.GetColVisible("Sheet1", "D") +// visible, err := f.GetColVisible("Sheet1", "D") // func (f *File) GetColVisible(sheet, col string) (bool, error) { visible := true @@ -52,45 +52,58 @@ func (f *File) GetColVisible(sheet, col string) (bool, error) { return visible, err } -// SetColVisible provides a function to set visible of a single column by given -// worksheet name and column name. For example, hide column D in Sheet1: +// SetColVisible provides a function to set visible columns by given worksheet +// name, columns range and visibility. +// +// For example hide column D on Sheet1: // // err := f.SetColVisible("Sheet1", "D", false) // -func (f *File) SetColVisible(sheet, col string, visible bool) error { - colNum, err := ColumnNameToNumber(col) +// Hide the columns from D to F (included) +// +// 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 { return err } - colData := xlsxCol{ - Min: colNum, - Max: colNum, - Hidden: !visible, - CustomWidth: true, + if len(colsTab) == 2 { + max, err = ColumnNameToNumber(colsTab[1]) + if err != nil { + return err + } + } else { + max = min + } + if max < min { + min, max = max, min } xlsx, err := f.workSheetReader(sheet) if err != nil { 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.Col = append(cols.Col, colData) 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 - colData.Max = colNum - colData.Hidden = !visible - colData.CustomWidth = true - xlsx.Cols.Col = append(xlsx.Cols.Col, colData) - return err + return nil } + // GetColOutlineLevel provides a function to get outline level of a single // column by given worksheet name and column name. For example, get outline // level of column D in Sheet1: diff --git a/col_test.go b/col_test.go index cdb7edf..08fac1c 100644 --- a/col_test.go +++ b/col_test.go @@ -12,17 +12,41 @@ func TestColumnVisibility(t *testing.T) { f, err := prepareTestBook1() 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", true)) visible, err := f.GetColVisible("Sheet1", "F") assert.Equal(t, true, visible) 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") 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", "*") assert.EqualError(t, err, `invalid column name "*"`) assert.EqualError(t, f.SetColVisible("Sheet1", "*", false), `invalid column name "*"`)