This fixed code review issue in PR #1154 (#1159)

This commit is contained in:
wangxuliBY 2022-02-25 21:37:49 +08:00 committed by GitHub
parent e84130e55c
commit 3231817169
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 1 deletions

View File

@ -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) {

View File

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