Skip to content

Commit 57955f7

Browse files
committed
This closes #33, new function SetSheetDimension and GetSheetDimension has been added
- Update the unit tests and documentation
1 parent ec50088 commit 57955f7

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

cmd/main.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ func regInteropFunc(f *excelize.File, fn map[string]interface{}) interface{} {
366366
"GetRowOutlineLevel": GetRowOutlineLevel(f),
367367
"GetRows": GetRows(f),
368368
"GetRowVisible": GetRowVisible(f),
369+
"GetSheetDimension": GetSheetDimension(f),
369370
"GetSheetIndex": GetSheetIndex(f),
370371
"GetSheetList": GetSheetList(f),
371372
"GetSheetMap": GetSheetMap(f),
@@ -423,6 +424,7 @@ func regInteropFunc(f *excelize.File, fn map[string]interface{}) interface{} {
423424
"SetRowVisible": SetRowVisible(f),
424425
"SetSheetBackgroundFromBytes": SetSheetBackgroundFromBytes(f),
425426
"SetSheetCol": SetSheetCol(f),
427+
"SetSheetDimension": SetSheetDimension(f),
426428
"SetSheetName": SetSheetName(f),
427429
"SetSheetProps": SetSheetProps(f),
428430
"SetSheetRow": SetSheetRow(f),
@@ -2412,6 +2414,24 @@ func GetRowVisible(f *excelize.File) func(this js.Value, args []js.Value) interf
24122414
}
24132415
}
24142416

2417+
// GetSheetDimension provides the method to get the used range of the worksheet.
2418+
func GetSheetDimension(f *excelize.File) func(this js.Value, args []js.Value) interface{} {
2419+
return func(this js.Value, args []js.Value) interface{} {
2420+
ret := map[string]interface{}{"dimension": false, "error": nil}
2421+
err := prepareArgs(args, []argsRule{
2422+
{types: []js.Type{js.TypeString}},
2423+
})
2424+
if err != nil {
2425+
ret["error"] = err.Error()
2426+
return js.ValueOf(ret)
2427+
}
2428+
if ret["dimension"], err = f.GetSheetDimension(args[0].String()); err != nil {
2429+
ret["error"] = err.Error()
2430+
}
2431+
return js.ValueOf(ret)
2432+
}
2433+
}
2434+
24152435
// GetRows return all the rows in a sheet by given worksheet name, returned as
24162436
// a two-dimensional array, where the value of the cell is converted to the
24172437
// string type. If the cell format can be applied to the value of the cell,
@@ -3834,6 +3854,28 @@ func SetSheetCol(f *excelize.File) func(this js.Value, args []js.Value) interfac
38343854
}
38353855
}
38363856

3857+
// SetSheetDimension provides the method to set or remove the used range of the
3858+
// worksheet by a given range reference. It specifies the row and column bounds
3859+
// of used cells in the worksheet. The range reference is set using the A1
3860+
// reference style(e.g., "A1:D5"). Passing an empty range reference will remove
3861+
// the used range of the worksheet.
3862+
func SetSheetDimension(f *excelize.File) func(this js.Value, args []js.Value) interface{} {
3863+
return func(this js.Value, args []js.Value) interface{} {
3864+
ret := map[string]interface{}{"error": nil}
3865+
if err := prepareArgs(args, []argsRule{
3866+
{types: []js.Type{js.TypeString}},
3867+
{types: []js.Type{js.TypeString}},
3868+
}); err != nil {
3869+
ret["error"] = err.Error()
3870+
return js.ValueOf(ret)
3871+
}
3872+
if err := f.SetSheetDimension(args[0].String(), args[1].String()); err != nil {
3873+
ret["error"] = err.Error()
3874+
}
3875+
return js.ValueOf(ret)
3876+
}
3877+
}
3878+
38373879
// SetSheetName provides a function to set the worksheet name by given source
38383880
// and target worksheet names. Maximum 31 characters are allowed in sheet
38393881
// title and this function only changes the name of the sheet and will not

cmd/main_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,36 @@ func TestGetRowVisible(t *testing.T) {
13641364
assert.False(t, ret.Get("visible").Bool())
13651365
}
13661366

1367+
func TestSheetDimension(t *testing.T) {
1368+
f := NewFile(js.Value{}, []js.Value{})
1369+
assert.True(t, f.(js.Value).Get("error").IsNull())
1370+
1371+
ret := f.(js.Value).Call("SetSheetDimension", js.ValueOf("Sheet1"), js.ValueOf("A1:D5"))
1372+
assert.True(t, ret.Get("error").IsNull())
1373+
1374+
ret = f.(js.Value).Call("GetSheetDimension", js.ValueOf("Sheet1"))
1375+
assert.Equal(t, "A1:D5", ret.Get("dimension").String())
1376+
assert.True(t, ret.Get("error").IsNull())
1377+
1378+
ret = f.(js.Value).Call("SetSheetDimension", js.ValueOf("Sheet1"), js.ValueOf(true))
1379+
assert.EqualError(t, errArgType, ret.Get("error").String())
1380+
1381+
ret = f.(js.Value).Call("SetSheetDimension")
1382+
assert.EqualError(t, errArgNum, ret.Get("error").String())
1383+
1384+
ret = f.(js.Value).Call("SetSheetDimension", js.ValueOf("SheetN"), js.ValueOf("A1:D5"))
1385+
assert.Equal(t, "sheet SheetN does not exist", ret.Get("error").String())
1386+
1387+
ret = f.(js.Value).Call("GetSheetDimension", js.ValueOf(true))
1388+
assert.EqualError(t, errArgType, ret.Get("error").String())
1389+
1390+
ret = f.(js.Value).Call("GetSheetDimension")
1391+
assert.EqualError(t, errArgNum, ret.Get("error").String())
1392+
1393+
ret = f.(js.Value).Call("GetSheetDimension", js.ValueOf("SheetN"))
1394+
assert.Equal(t, "sheet SheetN does not exist", ret.Get("error").String())
1395+
}
1396+
13671397
func TestGetRows(t *testing.T) {
13681398
f := NewFile(js.Value{}, []js.Value{})
13691399
assert.True(t, f.(js.Value).Get("error").IsNull())

src/index.d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2367,6 +2367,12 @@ declare module 'excelize-wasm' {
23672367
*/
23682368
GetRowVisible(sheet: string, row: number): { visible: boolean, error: string | null }
23692369

2370+
/**
2371+
* GetSheetDimension provides the method to get the used range of the worksheet.
2372+
* @param sheet The worksheet name
2373+
*/
2374+
GetSheetDimension(sheet: string): { dimension: string, error: string | null }
2375+
23702376
/**
23712377
* GetRows return all the rows in a sheet by given worksheet name, returned
23722378
* as a two-dimensional array, where the value of the cell is converted to
@@ -3577,6 +3583,17 @@ declare module 'excelize-wasm' {
35773583
*/
35783584
SetSheetBackgroundFromBytes(sheet: string, extension: string, picture: Uint8Array): { error: string | null }
35793585

3586+
/**
3587+
* SetSheetDimension provides the method to set or remove the used range of
3588+
* the worksheet by a given range reference. It specifies the row and column
3589+
* bounds of used cells in the worksheet. The range reference is set using
3590+
* the A1 reference style(e.g., "A1:D5"). Passing an empty range reference
3591+
* will remove the used range of the worksheet.
3592+
* @param sheet The worksheet name
3593+
* @param rangeRef The top-left and right-bottom cell range reference
3594+
*/
3595+
SetSheetDimension(sheet: string, rangeRef: string): { error: string | null }
3596+
35803597
/**
35813598
* SetSheetName provides a function to set the worksheet name by given
35823599
* source and target worksheet names. Maximum 31 characters are allowed in

0 commit comments

Comments
 (0)