summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRi Xu <xuri.me@gmail.com>2017-04-26 11:43:39 +0800
committerRi Xu <xuri.me@gmail.com>2017-04-26 11:43:39 +0800
commit2868bd3ec96db130b1116e4c576b3aab8efb868a (patch)
treee0eabd21cd8455df258779553c68f36d7495ba68
parent266d2c36e58ad1a53286ab2c01a2d41e3af883c7 (diff)
- New function `HideSheet()` and `UnhideSheet()` added;
- go test updated
-rw-r--r--excelize_test.go14
-rw-r--r--sheet.go54
2 files changed, 63 insertions, 5 deletions
diff --git a/excelize_test.go b/excelize_test.go
index 1b23dca..813cef2 100644
--- a/excelize_test.go
+++ b/excelize_test.go
@@ -438,6 +438,20 @@ func TestGetPicture(t *testing.T) {
t.Log(file, len(raw))
}
+func TestSheetVisibility(t *testing.T) {
+ xlsx, err := OpenFile("./test/Workbook_2.xlsx")
+ if err != nil {
+ t.Log(err)
+ }
+ xlsx.HideSheet("Sheet2")
+ xlsx.HideSheet("Sheet1")
+ xlsx.UnhideSheet("Sheet1")
+ err = xlsx.Save()
+ if err != nil {
+ t.Log(err)
+ }
+}
+
func TestCopySheet(t *testing.T) {
xlsx, err := OpenFile("./test/Workbook_2.xlsx")
if err != nil {
diff --git a/sheet.go b/sheet.go
index f571320..59e7cda 100644
--- a/sheet.go
+++ b/sheet.go
@@ -243,11 +243,8 @@ func (f *File) GetActiveSheetIndex() int {
// name in the formula or reference associated with the cell. So there may be
// problem formula error or reference missing.
func (f *File) SetSheetName(oldName, newName string) {
- r := strings.NewReplacer(":", "", "\\", "", "/", "", "?", "", "*", "", "[", "", "]", "")
- newName = r.Replace(newName)
- if len(newName) > 31 {
- newName = newName[0:31]
- }
+ oldName = trimSheetName(oldName)
+ newName = trimSheetName(newName)
content := f.workbookReader()
for k, v := range content.Sheets.Sheet {
if v.Name == oldName {
@@ -438,3 +435,50 @@ func (f *File) copySheet(from, to int) {
f.XLSX[toRels] = f.XLSX[fromRels]
}
}
+
+// HideSheet provides function to hide worksheet by given name. A workbook must
+// contain at least one visible worksheet. If the given worksheet has been
+// activated, this setting will be invalidated.
+func (f *File) HideSheet(name string) {
+ name = trimSheetName(name)
+ content := f.workbookReader()
+ count := 0
+ for _, v := range content.Sheets.Sheet {
+ if v.State != "hidden" {
+ count++
+ }
+ }
+ for k, v := range content.Sheets.Sheet {
+ sheetIndex := k + 1
+ xlsx := f.workSheetReader("sheet" + strconv.Itoa(sheetIndex))
+ tabSelected := false
+ if len(xlsx.SheetViews.SheetView) > 0 {
+ tabSelected = xlsx.SheetViews.SheetView[0].TabSelected
+ }
+ if v.Name == name && count > 1 && !tabSelected {
+ content.Sheets.Sheet[k].State = "hidden"
+ }
+ }
+}
+
+// UnhideSheet provides function to unhide worksheet by given name.
+func (f *File) UnhideSheet(name string) {
+ name = trimSheetName(name)
+ content := f.workbookReader()
+ for k, v := range content.Sheets.Sheet {
+ if v.Name == name {
+ content.Sheets.Sheet[k].State = ""
+ }
+ }
+}
+
+// trimSheetName provides function to trim invaild characters by given worksheet
+// name.
+func trimSheetName(name string) string {
+ r := strings.NewReplacer(":", "", "\\", "", "/", "", "?", "", "*", "", "[", "", "]", "")
+ name = r.Replace(name)
+ if len(name) > 31 {
+ name = name[0:31]
+ }
+ return name
+}