Skip to content

Commit

Permalink
New function MoveSheet has been added
Browse files Browse the repository at this point in the history
- Update the unit tests
  • Loading branch information
xuri committed Oct 24, 2024
1 parent ef25ac8 commit b5216e2
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
12 changes: 0 additions & 12 deletions cmd/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,16 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d h1:llb0neMWDQe87IzJLS4Ci7psK/lVsjIS2otl+1WyRyY=
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
github.com/xuri/excelize/v2 v2.8.2-0.20240927065818-bebb8020699e h1:7/Og1+RKX9BMPuOfR+VjRha6Bz4au5vTxnuFpR0uv6M=
github.com/xuri/excelize/v2 v2.8.2-0.20240927065818-bebb8020699e/go.mod h1:NlXCtlzkDyT7TF4yZvEKmoWx5eLmtJiJjPgJMtxKnv8=
github.com/xuri/excelize/v2 v2.9.0 h1:1tgOaEq92IOEumR1/JfYS/eR0KHOCsRv/rYXXh6YJQE=
github.com/xuri/excelize/v2 v2.9.0/go.mod h1:uqey4QBZ9gdMeWApPLdhm9x+9o2lq4iVmjiLfBS5hdE=
github.com/xuri/excelize/v2 v2.9.1-0.20241021013604-d1937a0cde23 h1:+p06nEmhW37XGxhLvSWofLt1G9w+z2AbdHLz04ovbdU=
github.com/xuri/excelize/v2 v2.9.1-0.20241021013604-d1937a0cde23/go.mod h1:uqey4QBZ9gdMeWApPLdhm9x+9o2lq4iVmjiLfBS5hdE=
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 h1:hPVCafDV85blFTabnqKgNhDCkJX25eik94Si9cTER4A=
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
golang.org/x/image v0.19.0 h1:D9FX4QWkLfkeqaC62SonffIIuYdOk/UE2XKUBgRIBIQ=
golang.org/x/image v0.19.0/go.mod h1:y0zrRqlQRWQ5PXaYCOMLTW2fpsxZ8Qh9I/ohnInJEys=
golang.org/x/image v0.21.0 h1:c5qV36ajHpdj4Qi0GnE0jUc/yuo33OLFaa0d+crTD5s=
golang.org/x/image v0.21.0/go.mod h1:vUbsLavqK/W303ZroQQVKQ+Af3Yl6Uz1Ppu5J/cLz78=
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
23 changes: 23 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ func regInteropFunc(f *excelize.File, fn map[string]interface{}) interface{} {
"InsertPageBreak": InsertPageBreak(f),
"InsertRows": InsertRows(f),
"MergeCell": MergeCell(f),
"MoveSheet": MoveSheet(f),
"NewConditionalStyle": NewConditionalStyle(f),
"NewSheet": NewSheet(f),
"NewStyle": NewStyle(f),
Expand Down Expand Up @@ -2768,6 +2769,28 @@ func MergeCell(f *excelize.File) func(this js.Value, args []js.Value) interface{
}
}

// MoveSheet moves a sheet to a specified position in the workbook. The function
// moves the source sheet before the target sheet. After moving, other sheets
// will be shifted to the left or right. If the sheet is already at the target
// position, the function will not perform any action. Not that this function
// will be ungroup all sheets after moving.
func MoveSheet(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.MoveSheet(args[0].String(), args[1].String()); err != nil {
ret["error"] = err.Error()
}
return js.ValueOf(ret)
}
}

// NewConditionalStyle provides a function to create style for conditional
// format by given style format. The parameters are the same with the
// NewStyle function. Note that the color field uses RGB color code and only
Expand Down
20 changes: 20 additions & 0 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,26 @@ func TestMergeCell(t *testing.T) {
assert.Equal(t, "sheet SheetN does not exist", ret.Get("error").String())
}

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

ret := f.(js.Value).Call("NewSheet", js.ValueOf("Sheet2"))
assert.True(t, ret.Get("error").IsNull())

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

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

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

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

func TestNewConditionalStyle(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 @@ -2488,6 +2488,23 @@ declare module 'excelize-wasm' {
*/
MergeCell(sheet: string, topLeftCell: string, bottomRightCell: string): { error: string | null }

/**
* MoveSheet moves a sheet to a specified position in the workbook. The
* function moves the source sheet before the target sheet. After moving,
* other sheets will be shifted to the left or right. If the sheet is
* already at the target position, the function will not perform any action.
* Not that this function will be ungroup all sheets after moving. For
* example, move Sheet2 before Sheet1:
*
* ```typescript
* const { error } = f.MoveSheet('Sheet2', 'Sheet1');
* ```
*
* @param source The source sheet name
* @param target The target sheet name
*/
MoveSheet(source: string, target: string): { error: string | null }

/**
* NewConditionalStyle provides a function to create style for conditional
* format by given style format. The parameters are the same with the
Expand Down

0 comments on commit b5216e2

Please sign in to comment.