Skip to content

Commit b5216e2

Browse files
committed
New function MoveSheet has been added
- Update the unit tests
1 parent ef25ac8 commit b5216e2

File tree

4 files changed

+60
-12
lines changed

4 files changed

+60
-12
lines changed

cmd/go.sum

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,16 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
1313
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
1414
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d h1:llb0neMWDQe87IzJLS4Ci7psK/lVsjIS2otl+1WyRyY=
1515
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
16-
github.com/xuri/excelize/v2 v2.8.2-0.20240927065818-bebb8020699e h1:7/Og1+RKX9BMPuOfR+VjRha6Bz4au5vTxnuFpR0uv6M=
17-
github.com/xuri/excelize/v2 v2.8.2-0.20240927065818-bebb8020699e/go.mod h1:NlXCtlzkDyT7TF4yZvEKmoWx5eLmtJiJjPgJMtxKnv8=
18-
github.com/xuri/excelize/v2 v2.9.0 h1:1tgOaEq92IOEumR1/JfYS/eR0KHOCsRv/rYXXh6YJQE=
19-
github.com/xuri/excelize/v2 v2.9.0/go.mod h1:uqey4QBZ9gdMeWApPLdhm9x+9o2lq4iVmjiLfBS5hdE=
2016
github.com/xuri/excelize/v2 v2.9.1-0.20241021013604-d1937a0cde23 h1:+p06nEmhW37XGxhLvSWofLt1G9w+z2AbdHLz04ovbdU=
2117
github.com/xuri/excelize/v2 v2.9.1-0.20241021013604-d1937a0cde23/go.mod h1:uqey4QBZ9gdMeWApPLdhm9x+9o2lq4iVmjiLfBS5hdE=
2218
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 h1:hPVCafDV85blFTabnqKgNhDCkJX25eik94Si9cTER4A=
2319
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
24-
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
25-
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
2620
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
2721
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
28-
golang.org/x/image v0.19.0 h1:D9FX4QWkLfkeqaC62SonffIIuYdOk/UE2XKUBgRIBIQ=
29-
golang.org/x/image v0.19.0/go.mod h1:y0zrRqlQRWQ5PXaYCOMLTW2fpsxZ8Qh9I/ohnInJEys=
3022
golang.org/x/image v0.21.0 h1:c5qV36ajHpdj4Qi0GnE0jUc/yuo33OLFaa0d+crTD5s=
3123
golang.org/x/image v0.21.0/go.mod h1:vUbsLavqK/W303ZroQQVKQ+Af3Yl6Uz1Ppu5J/cLz78=
32-
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
33-
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
3424
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
3525
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
36-
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
37-
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
3826
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
3927
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
4028
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

cmd/main.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ func regInteropFunc(f *excelize.File, fn map[string]interface{}) interface{} {
367367
"InsertPageBreak": InsertPageBreak(f),
368368
"InsertRows": InsertRows(f),
369369
"MergeCell": MergeCell(f),
370+
"MoveSheet": MoveSheet(f),
370371
"NewConditionalStyle": NewConditionalStyle(f),
371372
"NewSheet": NewSheet(f),
372373
"NewStyle": NewStyle(f),
@@ -2768,6 +2769,28 @@ func MergeCell(f *excelize.File) func(this js.Value, args []js.Value) interface{
27682769
}
27692770
}
27702771

2772+
// MoveSheet moves a sheet to a specified position in the workbook. The function
2773+
// moves the source sheet before the target sheet. After moving, other sheets
2774+
// will be shifted to the left or right. If the sheet is already at the target
2775+
// position, the function will not perform any action. Not that this function
2776+
// will be ungroup all sheets after moving.
2777+
func MoveSheet(f *excelize.File) func(this js.Value, args []js.Value) interface{} {
2778+
return func(this js.Value, args []js.Value) interface{} {
2779+
ret := map[string]interface{}{"error": nil}
2780+
if err := prepareArgs(args, []argsRule{
2781+
{types: []js.Type{js.TypeString}},
2782+
{types: []js.Type{js.TypeString}},
2783+
}); err != nil {
2784+
ret["error"] = err.Error()
2785+
return js.ValueOf(ret)
2786+
}
2787+
if err := f.MoveSheet(args[0].String(), args[1].String()); err != nil {
2788+
ret["error"] = err.Error()
2789+
}
2790+
return js.ValueOf(ret)
2791+
}
2792+
}
2793+
27712794
// NewConditionalStyle provides a function to create style for conditional
27722795
// format by given style format. The parameters are the same with the
27732796
// NewStyle function. Note that the color field uses RGB color code and only

cmd/main_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,6 +1482,26 @@ func TestMergeCell(t *testing.T) {
14821482
assert.Equal(t, "sheet SheetN does not exist", ret.Get("error").String())
14831483
}
14841484

1485+
func TestMoveSheet(t *testing.T) {
1486+
f := NewFile(js.Value{}, []js.Value{})
1487+
assert.True(t, f.(js.Value).Get("error").IsNull())
1488+
1489+
ret := f.(js.Value).Call("NewSheet", js.ValueOf("Sheet2"))
1490+
assert.True(t, ret.Get("error").IsNull())
1491+
1492+
ret = f.(js.Value).Call("MoveSheet", js.ValueOf("Sheet2"), js.ValueOf("Sheet1"))
1493+
assert.True(t, ret.Get("error").IsNull())
1494+
1495+
ret = f.(js.Value).Call("MoveSheet", js.ValueOf("Sheet1"), js.ValueOf("SheetN"))
1496+
assert.Equal(t, "sheet SheetN does not exist", ret.Get("error").String())
1497+
1498+
ret = f.(js.Value).Call("MoveSheet")
1499+
assert.EqualError(t, errArgNum, ret.Get("error").String())
1500+
1501+
ret = f.(js.Value).Call("MoveSheet", js.ValueOf("Sheet1"), js.ValueOf(nil))
1502+
assert.EqualError(t, errArgType, ret.Get("error").String())
1503+
}
1504+
14851505
func TestNewConditionalStyle(t *testing.T) {
14861506
f := NewFile(js.Value{}, []js.Value{})
14871507
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
@@ -2488,6 +2488,23 @@ declare module 'excelize-wasm' {
24882488
*/
24892489
MergeCell(sheet: string, topLeftCell: string, bottomRightCell: string): { error: string | null }
24902490

2491+
/**
2492+
* MoveSheet moves a sheet to a specified position in the workbook. The
2493+
* function moves the source sheet before the target sheet. After moving,
2494+
* other sheets will be shifted to the left or right. If the sheet is
2495+
* already at the target position, the function will not perform any action.
2496+
* Not that this function will be ungroup all sheets after moving. For
2497+
* example, move Sheet2 before Sheet1:
2498+
*
2499+
* ```typescript
2500+
* const { error } = f.MoveSheet('Sheet2', 'Sheet1');
2501+
* ```
2502+
*
2503+
* @param source The source sheet name
2504+
* @param target The target sheet name
2505+
*/
2506+
MoveSheet(source: string, target: string): { error: string | null }
2507+
24912508
/**
24922509
* NewConditionalStyle provides a function to create style for conditional
24932510
* format by given style format. The parameters are the same with the

0 commit comments

Comments
 (0)