From 9928bbc7c80dc9b3ab6599b9e472c2e00936ae84 Mon Sep 17 00:00:00 2001 From: Ri Xu Date: Thu, 15 Jun 2017 11:03:29 +0800 Subject: [PATCH] - New functions `GetColVisible()` and `SetColVisible()` added, relate issue #61; - go test updated; - Fix typo --- col.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ excelize_test.go | 20 ++++++++++++++++++ table.go | 2 +- 3 files changed, 74 insertions(+), 1 deletion(-) diff --git a/col.go b/col.go index cb1c3b4..66e28b8 100644 --- a/col.go +++ b/col.go @@ -13,6 +13,59 @@ const ( EMU int = 9525 ) +// GetColVisible provides a function to get visible of a single column by given +// worksheet index and column name. For example, get visible state of column D +// in Sheet1: +// +// xlsx.GetColVisible("Sheet1", "D") +// +func (f *File) GetColVisible(sheet, column string) bool { + xlsx := f.workSheetReader(sheet) + col := titleToNumber(strings.ToUpper(column)) + 1 + visible := true + if xlsx.Cols == nil { + return visible + } + for _, c := range xlsx.Cols.Col { + if c.Min <= col && col <= c.Max { + visible = !c.Hidden + } + } + return visible +} + +// SetColVisible provides a function to set visible of a single column by given +// worksheet index and column name. For example, hide column D in Sheet1: +// +// xlsx.SetColVisible("Sheet1", "D", false) +// +func (f *File) SetColVisible(sheet, column string, visible bool) { + xlsx := f.workSheetReader(sheet) + c := titleToNumber(strings.ToUpper(column)) + 1 + col := xlsxCol{ + Min: c, + Max: c, + Hidden: !visible, + CustomWidth: true, + } + if xlsx.Cols == nil { + cols := xlsxCols{} + cols.Col = append(cols.Col, col) + xlsx.Cols = &cols + return + } + for _, v := range xlsx.Cols.Col { + if v.Min <= c && c <= v.Max { + col = v + } + } + col.Min = c + col.Max = c + col.Hidden = !visible + col.CustomWidth = true + xlsx.Cols.Col = append(xlsx.Cols.Col, col) +} + // SetColWidth provides function to set the width of a single column or multiple // columns. For example: // diff --git a/excelize_test.go b/excelize_test.go index 027d9f8..93b6f91 100644 --- a/excelize_test.go +++ b/excelize_test.go @@ -492,6 +492,26 @@ func TestRowVisibility(t *testing.T) { } } +func TestColumnVisibility(t *testing.T) { + xlsx, err := OpenFile("./test/Workbook_2.xlsx") + if err != nil { + t.Log(err) + } + xlsx.SetColVisible("Sheet1", "F", false) + xlsx.SetColVisible("Sheet1", "F", true) + xlsx.GetColVisible("Sheet1", "F") + xlsx.SetColVisible("Sheet3", "E", false) + err = xlsx.Save() + if err != nil { + t.Log(err) + } + xlsx, err = OpenFile("./test/Workbook_3.xlsx") + if err != nil { + t.Log(err) + } + xlsx.GetColVisible("Sheet1", "B") +} + func TestCopySheet(t *testing.T) { xlsx, err := OpenFile("./test/Workbook_2.xlsx") if err != nil { diff --git a/table.go b/table.go index a713e39..72f9a9a 100644 --- a/table.go +++ b/table.go @@ -297,7 +297,7 @@ func (f *File) autoFilter(sheet, ref string, refRange, hxAxis int, formatSet *fo return nil } -// writeAutoFilter provides funtion to check for single or double custom filters +// writeAutoFilter provides function to check for single or double custom filters // as default filters and handle them accordingly. func (f *File) writeAutoFilter(filter *xlsxAutoFilter, exp []int, tokens []string) { if len(exp) == 1 && exp[0] == 2 {