Skip to content

Commit

Permalink
New function GetWorkbookProps has been added
Browse files Browse the repository at this point in the history
  • Loading branch information
xuri committed May 8, 2023
1 parent a2333f8 commit b573772
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
22 changes: 22 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ func regInteropFunc(f *excelize.File, fn map[string]interface{}) interface{} {
"GetSheetName": GetSheetName(f),
"GetSheetProps": GetSheetProps(f),
"GetSheetVisible": GetSheetVisible(f),
"GetWorkbookProps": GetWorkbookProps(f),
"GroupSheets": GroupSheets(f),
"InsertCols": InsertCols(f),
"InsertPageBreak": InsertPageBreak(f),
Expand Down Expand Up @@ -2100,6 +2101,27 @@ func GetSheetVisible(f *excelize.File) func(this js.Value, args []js.Value) inte
}
}

// GetWorkbookProps provides a function to gets workbook properties.
func GetWorkbookProps(f *excelize.File) func(this js.Value, args []js.Value) interface{} {
return func(this js.Value, args []js.Value) interface{} {
ret := map[string]interface{}{"props": map[string]interface{}{}, "error": nil}
if err := prepareArgs(args, []argsRule{}); err != nil {
ret["error"] = err.Error()
return js.ValueOf(ret)
}
props, err := f.GetWorkbookProps()
if err != nil {
ret["error"] = err.Error()
return js.ValueOf(ret)
}
if jsVal, err := goValueToJS(reflect.ValueOf(props),
reflect.TypeOf(excelize.WorkbookPropsOptions{})); err == nil {
ret["props"] = jsVal
}
return js.ValueOf(ret)
}
}

// GroupSheets provides a function to group worksheets by given worksheets
// name. Group worksheets must contain an active worksheet.
func GroupSheets(f *excelize.File) func(this js.Value, args []js.Value) interface{} {
Expand Down
10 changes: 9 additions & 1 deletion cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2228,7 +2228,7 @@ func TestSetSheetVisible(t *testing.T) {
assert.True(t, ret.Get("error").IsNull())
}

func TestSetWorkbookProps(t *testing.T) {
func TestWorkbookProps(t *testing.T) {
f := NewFile(js.Value{}, []js.Value{})
assert.True(t, f.(js.Value).Get("error").IsNull())

Expand All @@ -2241,9 +2241,17 @@ func TestSetWorkbookProps(t *testing.T) {
)
assert.True(t, ret.Get("error").IsNull())

ret = f.(js.Value).Call("GetWorkbookProps")
assert.True(t, ret.Get("error").IsNull())
assert.True(t, ret.Get("props").Get("Date1904").Bool())
assert.Equal(t, "code", ret.Get("props").Get("CodeName").String())

ret = f.(js.Value).Call("SetWorkbookProps")
assert.EqualError(t, errArgNum, ret.Get("error").String())

ret = f.(js.Value).Call("GetWorkbookProps", js.ValueOf(true))
assert.EqualError(t, errArgNum, ret.Get("error").String())

ret = f.(js.Value).Call("SetWorkbookProps",
js.ValueOf(map[string]interface{}{"CodeName": true}))
assert.EqualError(t, errArgType, ret.Get("error").String())
Expand Down
5 changes: 5 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,11 @@ declare module 'excelize-wasm' {
*/
GetSheetVisible(sheet: string): { visible: boolean, error: string | null }

/**
* GetWorkbookProps provides a function to gets workbook properties.
*/
GetWorkbookProps(): { props: WorkbookPropsOptions, error: string | null }

/**
* GroupSheets provides a function to group worksheets by given worksheets
* name. Group worksheets must contain an active worksheet.
Expand Down

0 comments on commit b573772

Please sign in to comment.