- New functions: `GetSheetVisible()` and `GetRowVisible()` added, relate issue #61;

- go test updated
This commit is contained in:
Ri Xu 2017-06-14 15:01:49 +08:00
parent efff54ccde
commit a9f671d98f
4 changed files with 68 additions and 27 deletions

View File

@ -468,9 +468,10 @@ func TestSheetVisibility(t *testing.T) {
if err != nil { if err != nil {
t.Log(err) t.Log(err)
} }
xlsx.HideSheet("Sheet2") xlsx.SetSheetVisible("Sheet2", false)
xlsx.HideSheet("Sheet1") xlsx.SetSheetVisible("Sheet1", false)
xlsx.UnhideSheet("Sheet1") xlsx.SetSheetVisible("Sheet1", true)
xlsx.GetSheetVisible("Sheet1")
err = xlsx.Save() err = xlsx.Save()
if err != nil { if err != nil {
t.Log(err) t.Log(err)
@ -482,8 +483,9 @@ func TestRowVisibility(t *testing.T) {
if err != nil { if err != nil {
t.Log(err) t.Log(err)
} }
xlsx.HideRow("Sheet3", 2) xlsx.SetRowVisible("Sheet3", 2, false)
xlsx.UnhideRow("Sheet3", 2) xlsx.SetRowVisible("Sheet3", 2, true)
xlsx.GetRowVisible("Sheet3", 2)
err = xlsx.Save() err = xlsx.Save()
if err != nil { if err != nil {
t.Log(err) t.Log(err)

21
rows.go
View File

@ -149,26 +149,33 @@ func (xlsx *xlsxC) getValueFrom(f *File, d *xlsxSST) (string, error) {
} }
} }
// HideRow provides a function to set hidden of a single row by given worksheet index and row index. For example, hide row 3 in Sheet1: // SetRowVisible provides a function to set visible of a single row by given
// worksheet index and row index. For example, hide row 3 in Sheet1:
// //
// xlsx.HideRow("Sheet1", 2) // xlsx.SetRowVisible("Sheet1", 2, false)
// //
func (f *File) HideRow(sheet string, rowIndex int) { func (f *File) SetRowVisible(sheet string, rowIndex int, visible bool) {
xlsx := f.workSheetReader(sheet) xlsx := f.workSheetReader(sheet)
rows := rowIndex + 1 rows := rowIndex + 1
cells := 0 cells := 0
completeRow(xlsx, rows, cells) completeRow(xlsx, rows, cells)
if visible {
xlsx.SheetData.Row[rowIndex].Hidden = false
return
}
xlsx.SheetData.Row[rowIndex].Hidden = true xlsx.SheetData.Row[rowIndex].Hidden = true
} }
// UnhideRow provides a function to set unhidden of a single row by given worksheet index and row index. For example, unhide row 3 in Sheet1: // GetRowVisible provides a function to get visible of a single row by given
// worksheet index and row index. For example, get visible state of row 3 in
// Sheet1:
// //
// xlsx.UnhideRow("Sheet1", 2) // xlsx.GetRowVisible("Sheet1", 2)
// //
func (f *File) UnhideRow(sheet string, rowIndex int) { func (f *File) GetRowVisible(sheet string, rowIndex int) bool {
xlsx := f.workSheetReader(sheet) xlsx := f.workSheetReader(sheet)
rows := rowIndex + 1 rows := rowIndex + 1
cells := 0 cells := 0
completeRow(xlsx, rows, cells) completeRow(xlsx, rows, cells)
xlsx.SheetData.Row[rowIndex].Hidden = false return !xlsx.SheetData.Row[rowIndex].Hidden
} }

View File

@ -435,19 +435,31 @@ func (f *File) copySheet(from, to int) {
} }
} }
// HideSheet provides function to hide worksheet by given name. A workbook must // SetSheetVisible provides function to set worksheet visible by given worksheet
// contain at least one visible worksheet. If the given worksheet has been // name. A workbook must contain at least one visible worksheet. If the given
// activated, this setting will be invalidated. Sheet state values as defined by // worksheet has been activated, this setting will be invalidated. Sheet state
// http://msdn.microsoft.com/en- // values as defined by http://msdn.microsoft.com/en-
// us/library/office/documentformat.openxml.spreadsheet.sheetstatevalues.aspx // us/library/office/documentformat.openxml.spreadsheet.sheetstatevalues.aspx
// //
// visible // visible
// hidden // hidden
// veryHidden // veryHidden
// //
func (f *File) HideSheet(name string) { // For example, hide Sheet1:
//
// xlsx.SetSheetVisible("Sheet1", false)
//
func (f *File) SetSheetVisible(name string, visible bool) {
name = trimSheetName(name) name = trimSheetName(name)
content := f.workbookReader() content := f.workbookReader()
if visible {
for k, v := range content.Sheets.Sheet {
if v.Name == name {
content.Sheets.Sheet[k].State = ""
}
}
return
}
count := 0 count := 0
for _, v := range content.Sheets.Sheet { for _, v := range content.Sheets.Sheet {
if v.State != "hidden" { if v.State != "hidden" {
@ -467,15 +479,23 @@ func (f *File) HideSheet(name string) {
} }
} }
// UnhideSheet provides function to unhide worksheet by given name. // GetSheetVisible provides function to get worksheet visible by given worksheet
func (f *File) UnhideSheet(name string) { // name. For example, get visible state of Sheet1:
//
// xlsx.GetSheetVisible("Sheet1")
//
func (f *File) GetSheetVisible(name string) bool {
name = trimSheetName(name) name = trimSheetName(name)
content := f.workbookReader() content := f.workbookReader()
visible := false
for k, v := range content.Sheets.Sheet { for k, v := range content.Sheets.Sheet {
if v.Name == name { if v.Name == name {
content.Sheets.Sheet[k].State = "" if content.Sheets.Sheet[k].State == "" || content.Sheets.Sheet[k].State == "visible" {
visible = true
}
} }
} }
return visible
} }
// trimSheetName provides function to trim invaild characters by given worksheet // trimSheetName provides function to trim invaild characters by given worksheet

View File

@ -172,13 +172,18 @@ func parseAutoFilterSet(formatSet string) *formatAutoFilter {
// //
// err = xlsx.AutoFilter("Sheet1", "A1", "D4", `{"column":"B","expression":"x != blanks"}`) // err = xlsx.AutoFilter("Sheet1", "A1", "D4", `{"column":"B","expression":"x != blanks"}`)
// //
// column defines the filter columns in a autofilter range based on simple criteria // column defines the filter columns in a autofilter range based on simple
// criteria
// //
// It isn't sufficient to just specify the filter condition. You must also hide any rows that don't match the filter condition. Rows are hidden using the HideRow() method. Excelize can't filter rows automatically since this isn't part of the file format. // It isn't sufficient to just specify the filter condition. You must also hide
// any rows that don't match the filter condition. Rows are hidden using the
// SetRowVisible() method. Excelize can't filter rows automatically since this
// isn't part of the file format.
// //
// Setting a filter criteria for a column: // Setting a filter criteria for a column:
// //
// expression defines the conditions, the following operators are available for setting the filter criteria: // expression defines the conditions, the following operators are available for
// setting the filter criteria:
// //
// == // ==
// != // !=
@ -189,7 +194,8 @@ func parseAutoFilterSet(formatSet string) *formatAutoFilter {
// and // and
// or // or
// //
// An expression can comprise a single statement or two statements separated by the and and or operators. For example: // An expression can comprise a single statement or two statements separated by
// the and and or operators. For example:
// //
// x < 2000 // x < 2000
// x > 2000 // x > 2000
@ -197,7 +203,8 @@ func parseAutoFilterSet(formatSet string) *formatAutoFilter {
// x > 2000 and x < 5000 // x > 2000 and x < 5000
// x == 2000 or x == 5000 // x == 2000 or x == 5000
// //
// Filtering of blank or non-blank data can be achieved by using a value of Blanks or NonBlanks in the expression: // Filtering of blank or non-blank data can be achieved by using a value of
// Blanks or NonBlanks in the expression:
// //
// x == Blanks // x == Blanks
// x == NonBlanks // x == NonBlanks
@ -211,9 +218,14 @@ func parseAutoFilterSet(formatSet string) *formatAutoFilter {
// x == *b* // contains b // x == *b* // contains b
// x != *b* // doesn't contains b // x != *b* // doesn't contains b
// //
// You can also use '*' to match any character or number and '?' to match any single character or number. No other regular expression quantifier is supported by Excel's filters. Excel's regular expression characters can be escaped using '~'. // You can also use '*' to match any character or number and '?' to match any
// single character or number. No other regular expression quantifier is
// supported by Excel's filters. Excel's regular expression characters can be
// escaped using '~'.
// //
// The placeholder variable x in the above examples can be replaced by any simple string. The actual placeholder name is ignored internally so the following are all equivalent: // The placeholder variable x in the above examples can be replaced by any
// simple string. The actual placeholder name is ignored internally so the
// following are all equivalent:
// //
// x < 2000 // x < 2000
// col < 2000 // col < 2000