Skip to content

Commit

Permalink
This closes #33, new function SetSheetDimension and `GetSheetDimens…
Browse files Browse the repository at this point in the history
…ion` has been added

- Update the unit tests and documentation
  • Loading branch information
xuri committed Nov 11, 2024
1 parent ec50088 commit 57955f7
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
42 changes: 42 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ func regInteropFunc(f *excelize.File, fn map[string]interface{}) interface{} {
"GetRowOutlineLevel": GetRowOutlineLevel(f),
"GetRows": GetRows(f),
"GetRowVisible": GetRowVisible(f),
"GetSheetDimension": GetSheetDimension(f),
"GetSheetIndex": GetSheetIndex(f),
"GetSheetList": GetSheetList(f),
"GetSheetMap": GetSheetMap(f),
Expand Down Expand Up @@ -423,6 +424,7 @@ func regInteropFunc(f *excelize.File, fn map[string]interface{}) interface{} {
"SetRowVisible": SetRowVisible(f),
"SetSheetBackgroundFromBytes": SetSheetBackgroundFromBytes(f),
"SetSheetCol": SetSheetCol(f),
"SetSheetDimension": SetSheetDimension(f),
"SetSheetName": SetSheetName(f),
"SetSheetProps": SetSheetProps(f),
"SetSheetRow": SetSheetRow(f),
Expand Down Expand Up @@ -2412,6 +2414,24 @@ func GetRowVisible(f *excelize.File) func(this js.Value, args []js.Value) interf
}
}

// GetSheetDimension provides the method to get the used range of the worksheet.
func GetSheetDimension(f *excelize.File) func(this js.Value, args []js.Value) interface{} {
return func(this js.Value, args []js.Value) interface{} {
ret := map[string]interface{}{"dimension": false, "error": nil}
err := prepareArgs(args, []argsRule{
{types: []js.Type{js.TypeString}},
})
if err != nil {
ret["error"] = err.Error()
return js.ValueOf(ret)
}
if ret["dimension"], err = f.GetSheetDimension(args[0].String()); err != nil {
ret["error"] = err.Error()
}
return js.ValueOf(ret)
}
}

// GetRows return all the rows in a sheet by given worksheet name, returned as
// a two-dimensional array, where the value of the cell is converted to the
// string type. If the cell format can be applied to the value of the cell,
Expand Down Expand Up @@ -3834,6 +3854,28 @@ func SetSheetCol(f *excelize.File) func(this js.Value, args []js.Value) interfac
}
}

// SetSheetDimension provides the method to set or remove the used range of the
// worksheet by a given range reference. It specifies the row and column bounds
// of used cells in the worksheet. The range reference is set using the A1
// reference style(e.g., "A1:D5"). Passing an empty range reference will remove
// the used range of the worksheet.
func SetSheetDimension(f *excelize.File) func(this js.Value, args []js.Value) interface{} {
return func(this js.Value, args []js.Value) interface{} {
ret := map[string]interface{}{"error": nil}
if err := prepareArgs(args, []argsRule{
{types: []js.Type{js.TypeString}},
{types: []js.Type{js.TypeString}},
}); err != nil {
ret["error"] = err.Error()
return js.ValueOf(ret)
}
if err := f.SetSheetDimension(args[0].String(), args[1].String()); err != nil {
ret["error"] = err.Error()
}
return js.ValueOf(ret)
}
}

// SetSheetName provides a function to set the worksheet name by given source
// and target worksheet names. Maximum 31 characters are allowed in sheet
// title and this function only changes the name of the sheet and will not
Expand Down
30 changes: 30 additions & 0 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,36 @@ func TestGetRowVisible(t *testing.T) {
assert.False(t, ret.Get("visible").Bool())
}

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

ret := f.(js.Value).Call("SetSheetDimension", js.ValueOf("Sheet1"), js.ValueOf("A1:D5"))
assert.True(t, ret.Get("error").IsNull())

ret = f.(js.Value).Call("GetSheetDimension", js.ValueOf("Sheet1"))
assert.Equal(t, "A1:D5", ret.Get("dimension").String())
assert.True(t, ret.Get("error").IsNull())

ret = f.(js.Value).Call("SetSheetDimension", js.ValueOf("Sheet1"), js.ValueOf(true))
assert.EqualError(t, errArgType, ret.Get("error").String())

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

ret = f.(js.Value).Call("SetSheetDimension", js.ValueOf("SheetN"), js.ValueOf("A1:D5"))
assert.Equal(t, "sheet SheetN does not exist", ret.Get("error").String())

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

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

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

func TestGetRows(t *testing.T) {
f := NewFile(js.Value{}, []js.Value{})
assert.True(t, f.(js.Value).Get("error").IsNull())
Expand Down
17 changes: 17 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2367,6 +2367,12 @@ declare module 'excelize-wasm' {
*/
GetRowVisible(sheet: string, row: number): { visible: boolean, error: string | null }

/**
* GetSheetDimension provides the method to get the used range of the worksheet.
* @param sheet The worksheet name
*/
GetSheetDimension(sheet: string): { dimension: string, error: string | null }

/**
* GetRows return all the rows in a sheet by given worksheet name, returned
* as a two-dimensional array, where the value of the cell is converted to
Expand Down Expand Up @@ -3577,6 +3583,17 @@ declare module 'excelize-wasm' {
*/
SetSheetBackgroundFromBytes(sheet: string, extension: string, picture: Uint8Array): { error: string | null }

/**
* SetSheetDimension provides the method to set or remove the used range of
* the worksheet by a given range reference. It specifies the row and column
* bounds of used cells in the worksheet. The range reference is set using
* the A1 reference style(e.g., "A1:D5"). Passing an empty range reference
* will remove the used range of the worksheet.
* @param sheet The worksheet name
* @param rangeRef The top-left and right-bottom cell range reference
*/
SetSheetDimension(sheet: string, rangeRef: string): { error: string | null }

/**
* SetSheetName provides a function to set the worksheet name by given
* source and target worksheet names. Maximum 31 characters are allowed in
Expand Down

0 comments on commit 57955f7

Please sign in to comment.