diff --git a/workbook.go b/workbook.go index a2263b2d..3d9fa480 100644 --- a/workbook.go +++ b/workbook.go @@ -32,6 +32,11 @@ type WorkbookPrOptionPtr interface { getWorkbookPrOption(pr *xlsxWorkbookPr) } +type ( + // FilterPrivacy is an option used for WorkbookPrOption + FilterPrivacy bool +) + // setWorkbook update workbook property of the spreadsheet. Maximum 31 // characters are allowed in sheet title. func (f *File) setWorkbook(name string, sheetID, rid int) { @@ -104,6 +109,7 @@ func (f *File) workBookWriter() { // SetWorkbookPrOptions provides a function to sets workbook properties. // // Available options: +// FilterPrivacy(bool) // CodeName(string) func (f *File) SetWorkbookPrOptions(opts ...WorkbookPrOption) error { wb := f.workbookReader() @@ -118,6 +124,11 @@ func (f *File) SetWorkbookPrOptions(opts ...WorkbookPrOption) error { return nil } +// setWorkbookPrOption implements the WorkbookPrOption interface. +func (o FilterPrivacy) setWorkbookPrOption(pr *xlsxWorkbookPr) { + pr.FilterPrivacy = bool(o) +} + // setWorkbookPrOption implements the WorkbookPrOption interface. func (o CodeName) setWorkbookPrOption(pr *xlsxWorkbookPr) { pr.CodeName = string(o) @@ -126,6 +137,7 @@ func (o CodeName) setWorkbookPrOption(pr *xlsxWorkbookPr) { // GetWorkbookPrOptions provides a function to gets workbook properties. // // Available options: +// FilterPrivacy(bool) // CodeName(string) func (f *File) GetWorkbookPrOptions(opts ...WorkbookPrOptionPtr) error { wb := f.workbookReader() @@ -136,6 +148,16 @@ func (f *File) GetWorkbookPrOptions(opts ...WorkbookPrOptionPtr) error { return nil } +// getWorkbookPrOption implements the WorkbookPrOption interface and get the +// filter privacy of thw workbook. +func (o *FilterPrivacy) getWorkbookPrOption(pr *xlsxWorkbookPr) { + if pr == nil { + *o = false + return + } + *o = FilterPrivacy(pr.FilterPrivacy) +} + // getWorkbookPrOption implements the WorkbookPrOption interface and get the // code name of thw workbook. func (o *CodeName) getWorkbookPrOption(pr *xlsxWorkbookPr) { diff --git a/workbook_test.go b/workbook_test.go index 90fc3d70..e31caf26 100644 --- a/workbook_test.go +++ b/workbook_test.go @@ -10,6 +10,7 @@ import ( func ExampleFile_SetWorkbookPrOptions() { f := NewFile() if err := f.SetWorkbookPrOptions( + FilterPrivacy(false), CodeName("code"), ); err != nil { fmt.Println(err) @@ -19,14 +20,22 @@ func ExampleFile_SetWorkbookPrOptions() { func ExampleFile_GetWorkbookPrOptions() { f := NewFile() - var codeName CodeName + var ( + filterPrivacy FilterPrivacy + codeName CodeName + ) + if err := f.GetWorkbookPrOptions(&filterPrivacy); err != nil { + fmt.Println(err) + } if err := f.GetWorkbookPrOptions(&codeName); err != nil { fmt.Println(err) } fmt.Println("Defaults:") + fmt.Printf("- filterPrivacy: %t\n", filterPrivacy) fmt.Printf("- codeName: %q\n", codeName) // Output: // Defaults: + // - filterPrivacy: true // - codeName: "" } @@ -40,4 +49,9 @@ func TestWorkbookPr(t *testing.T) { assert.NoError(t, f.SetWorkbookPrOptions(CodeName("code"))) assert.NoError(t, f.GetWorkbookPrOptions(&codeName)) assert.Equal(t, "code", string(codeName)) + + wb.WorkbookPr = nil + var filterPrivacy FilterPrivacy + assert.NoError(t, f.GetWorkbookPrOptions(&filterPrivacy)) + assert.Equal(t, false, bool(filterPrivacy)) }