Skip to content

Commit

Permalink
This fixed code review issue in PR qax-os#1154
Browse files Browse the repository at this point in the history
This fixed code review issue in PR qax-os#1154
  • Loading branch information
wangxuliBY authored Feb 25, 2022
1 parent e84130e commit fe58051
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
22 changes: 22 additions & 0 deletions workbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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) {
Expand Down
16 changes: 15 additions & 1 deletion workbook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
func ExampleFile_SetWorkbookPrOptions() {
f := NewFile()
if err := f.SetWorkbookPrOptions(
FilterPrivacy(false),
CodeName("code"),
); err != nil {
fmt.Println(err)
Expand All @@ -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: ""
}

Expand All @@ -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))
}

0 comments on commit fe58051

Please sign in to comment.