forked from p30928647/excelize
- New function `HideSheet()` and `UnhideSheet()` added;
- go test updated
This commit is contained in:
parent
266d2c36e5
commit
2868bd3ec9
|
@ -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 {
|
||||
|
|
54
sheet.go
54
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue