Skip to content

Commit

Permalink
New function GetPageLayout has been added
Browse files Browse the repository at this point in the history
  • Loading branch information
xuri committed May 1, 2023
1 parent 124e896 commit 56c8edf
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
26 changes: 25 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ func regInteropFunc(f *excelize.File, fn map[string]interface{}) interface{} {
"GetDefaultFont": GetDefaultFont(f),
"GetDefinedName": GetDefinedName(f),
"GetDocProps": GetDocProps(f),
"GetPageLayout": GetPageLayout(f),
"GetPictures": GetPictures(f),
"GetRowHeight": GetRowHeight(f),
"GetRowOutlineLevel": GetRowOutlineLevel(f),
Expand Down Expand Up @@ -1781,7 +1782,7 @@ func GetDefinedName(f *excelize.File) func(this js.Value, args []js.Value) inter
// GetDocProps provides a function to get document core properties.
func GetDocProps(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": nil, "error": nil}
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)
Expand All @@ -1799,6 +1800,29 @@ func GetDocProps(f *excelize.File) func(this js.Value, args []js.Value) interfac
}
}

// GetPageLayout provides a function to gets worksheet page layout.
func GetPageLayout(f *excelize.File) func(this js.Value, args []js.Value) interface{} {
return func(this js.Value, args []js.Value) interface{} {
ret := map[string]interface{}{"opts": map[string]interface{}{}, "error": nil}
if err := prepareArgs(args, []argsRule{
{types: []js.Type{js.TypeString}},
}); err != nil {
ret["error"] = err.Error()
return js.ValueOf(ret)
}
opts, err := f.GetPageLayout(args[0].String())
if err != nil {
ret["error"] = err.Error()
return js.ValueOf(ret)
}
if jsVal, err := goValueToJS(reflect.ValueOf(opts),
reflect.TypeOf(excelize.PageLayoutOptions{})); err == nil {
ret["opts"] = jsVal
}
return js.ValueOf(ret)
}
}

// GetPictures provides a function to get picture meta info and raw content
// embed in spreadsheet by given worksheet and cell name. This function
// returns the image contents as []byte data types.
Expand Down
15 changes: 14 additions & 1 deletion cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1902,7 +1902,7 @@ func TestSetHeaderFooter(t *testing.T) {
assert.Equal(t, "field OddHeader must be less than or equal to 255 characters", ret.Get("error").String())
}

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

Expand Down Expand Up @@ -1930,6 +1930,19 @@ func TestSetPageLayout(t *testing.T) {
js.ValueOf(map[string]interface{}{"Size": 1}),
)
assert.Equal(t, "sheet SheetN does not exist", ret.Get("error").String())

ret = f.(js.Value).Call("GetPageLayout", js.ValueOf("Sheet1"))
assert.True(t, ret.Get("error").IsNull())

assert.Equal(t, "landscape", ret.Get("opts").Get("Orientation").String())
assert.Equal(t, 120, ret.Get("opts").Get("AdjustTo").Int())
assert.True(t, ret.Get("opts").Get("BlackAndWhite").Bool())

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

ret = f.(js.Value).Call("GetPageLayout", js.ValueOf("SheetN"))
assert.Equal(t, "sheet SheetN does not exist", ret.Get("error").String())
}

func TestSetPageMargins(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,12 @@ declare module 'excelize-wasm' {
*/
GetDocProps(): { props: DocProperties, error: string | null }

/**
* GetPageLayout provides a function to gets worksheet page layout.
* @param sheet The worksheet name
*/
GetPageLayout(sheet: string): { opts: PageLayoutOptions, error: string | null }

/**
* GetPictures provides a function to get picture meta info and raw content
* embed in spreadsheet by given worksheet and cell name.
Expand Down

0 comments on commit 56c8edf

Please sign in to comment.