From 68754a2075f12ba3c2bdf3646e4a3e7a3fd829f5 Mon Sep 17 00:00:00 2001
From: Jacques Boscq <jacques@boscq.fr>
Date: Tue, 21 Jan 2020 23:42:44 +0100
Subject: SetColVisible() can parse a column range + typos.

---
 col.go | 59 ++++++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 36 insertions(+), 23 deletions(-)

(limited to 'col.go')

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:
-- 
cgit v1.2.1