From 9cc6455ffae014a8b922c6280487005d97441dc3 Mon Sep 17 00:00:00 2001 From: xuri Date: Sun, 12 Nov 2023 00:30:44 +0800 Subject: [PATCH] Made `GetConditionalStyle` function support apply options - Upgrade the Excelize to the master branch code - Update the unit test --- cmd/go.mod | 5 +++-- cmd/go.sum | 27 ++++++++++++++++----------- cmd/main.go | 25 +++++++++++++++++++++++++ cmd/main_test.go | 16 +++++++++++++++- src/index.d.ts | 7 +++++++ 5 files changed, 66 insertions(+), 14 deletions(-) diff --git a/cmd/go.mod b/cmd/go.mod index 9d38959..a56ef97 100644 --- a/cmd/go.mod +++ b/cmd/go.mod @@ -4,7 +4,8 @@ go 1.16 require ( github.com/stretchr/testify v1.8.1 - github.com/xuri/excelize/v2 v2.8.1-0.20230928005354-1c23dc3507c6 - github.com/xuri/nfp v0.0.0-20230919160717-d98342af3f05 // indirect + github.com/xuri/efp v0.0.0-20231025114914-d1ff6096ae53 // indirect + github.com/xuri/excelize/v2 v2.8.1-0.20231110160405-c7acf4fafef4 golang.org/x/image v0.12.0 + golang.org/x/net v0.18.0 // indirect ) diff --git a/cmd/go.sum b/cmd/go.sum index de9988f..6e11a8e 100644 --- a/cmd/go.sum +++ b/cmd/go.sum @@ -17,18 +17,19 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/xuri/efp v0.0.0-20230802181842-ad255f2331ca h1:uvPMDVyP7PXMMioYdyPH+0O+Ta/UO1WFfNYMO3Wz0eg= github.com/xuri/efp v0.0.0-20230802181842-ad255f2331ca/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI= -github.com/xuri/excelize/v2 v2.8.1-0.20230928005354-1c23dc3507c6 h1:xUVR0duWw+DpXY1tNvQMw/0C7Cb5IetLZpzULoRfWIk= -github.com/xuri/excelize/v2 v2.8.1-0.20230928005354-1c23dc3507c6/go.mod h1:GKocsEKQZgdn03padVqGDQpXV9NX2tNf1bqc9cFk7nE= -github.com/xuri/nfp v0.0.0-20230918160701-e5a3f5b24785/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ= +github.com/xuri/efp v0.0.0-20231025114914-d1ff6096ae53 h1:Chd9DkqERQQuHpXjR/HSV1jLZA6uaoiwwH3vSuF3IW0= +github.com/xuri/efp v0.0.0-20231025114914-d1ff6096ae53/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI= +github.com/xuri/excelize/v2 v2.8.1-0.20231110160405-c7acf4fafef4 h1:9xehxILB3K6+ywiX7KcqMbf357+dtpVxONSoRf3ZXFU= +github.com/xuri/excelize/v2 v2.8.1-0.20231110160405-c7acf4fafef4/go.mod h1:iwipck2q5caqRjROpapZpVqnlcAxBnE7CuLOJ8oNF/I= github.com/xuri/nfp v0.0.0-20230919160717-d98342af3f05 h1:qhbILQo1K3mphbwKh1vNm4oGezE1eF9fQWmNiIpSfI4= github.com/xuri/nfp v0.0.0-20230919160717-d98342af3f05/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.13.0 h1:mvySKfSWJ+UKUii46M40LOvyWfN0s2U+46/jDd0e6Ck= -golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc= +golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= +golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA= +golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g= golang.org/x/image v0.11.0/go.mod h1:bglhjqbqVuEb9e9+eNR45Jfu7D+T4Qan+NhQk8Ck2P8= golang.org/x/image v0.12.0 h1:w13vZbU4o5rKOFFR8y7M+c4A5jXDC0uXTdHYRP8X2DQ= golang.org/x/image v0.12.0/go.mod h1:Lu90jvHG7GfemOIcldsh9A2hS01ocl6oNO7ype5mEnk= @@ -39,8 +40,9 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= -golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8= -golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= +golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= +golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg= +golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -51,20 +53,23 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= -golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU= +golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= +golang.org/x/term v0.14.0/go.mod h1:TySc+nGkYR6qt8km8wUhuFRTVSMIX3XPR58y2lC8vww= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= -golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= diff --git a/cmd/main.go b/cmd/main.go index 1e646aa..c124924 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -309,6 +309,7 @@ func regInteropFunc(f *excelize.File, fn map[string]interface{}) interface{} { "GetColVisible": GetColVisible(f), "GetColWidth": GetColWidth(f), "GetComments": GetComments(f), + "GetConditionalStyle": GetConditionalStyle(f), "GetDefaultFont": GetDefaultFont(f), "GetDefinedName": GetDefinedName(f), "GetDocProps": GetDocProps(f), @@ -1883,6 +1884,30 @@ func GetComments(f *excelize.File) func(this js.Value, args []js.Value) interfac } } +// GetConditionalStyle returns conditional format style definition by specified +// style index. +func GetConditionalStyle(f *excelize.File) func(this js.Value, args []js.Value) interface{} { + return func(this js.Value, args []js.Value) interface{} { + ret := map[string]interface{}{"style": map[string]interface{}{}, "error": nil} + if err := prepareArgs(args, []argsRule{ + {types: []js.Type{js.TypeNumber}}, + }); err != nil { + ret["error"] = err.Error() + return js.ValueOf(ret) + } + style, err := f.GetConditionalStyle(args[0].Int()) + if err != nil { + ret["error"] = err.Error() + return js.ValueOf(ret) + } + if jsVal, err := goValueToJS(reflect.ValueOf(*style), + reflect.TypeOf(excelize.Style{})); err == nil { + ret["style"] = jsVal + } + return js.ValueOf(ret) + } +} + // GetDefaultFont provides the default font name currently set in the // workbook. The spreadsheet generated by excelize default font is Calibri. func GetDefaultFont(f *excelize.File) func(this js.Value, args []js.Value) interface{} { diff --git a/cmd/main_test.go b/cmd/main_test.go index 9a3bd3b..348d0ff 100644 --- a/cmd/main_test.go +++ b/cmd/main_test.go @@ -1397,7 +1397,15 @@ func TestNewConditionalStyle(t *testing.T) { }), ) assert.True(t, ret.Get("error").IsNull(), ret.Get("error").String()) - assert.Equal(t, 0, ret.Get("style").Int()) + styleID := ret.Get("style") + assert.Equal(t, 0, styleID.Int()) + + ret = f.(js.Value).Call("GetConditionalStyle", styleID) + assert.True(t, ret.Get("error").IsNull()) + assert.Equal(t, "pattern", ret.Get("style").Get("Fill").Get("Type").String()) + + ret = f.(js.Value).Call("GetConditionalStyle", js.ValueOf(2)) + assert.Equal(t, "invalid style ID 2", ret.Get("error").String()) ret = f.(js.Value).Call("NewConditionalStyle", js.ValueOf(map[string]interface{}{ @@ -1411,9 +1419,15 @@ func TestNewConditionalStyle(t *testing.T) { ret = f.(js.Value).Call("NewConditionalStyle") assert.EqualError(t, errArgNum, ret.Get("error").String()) + ret = f.(js.Value).Call("GetConditionalStyle") + assert.EqualError(t, errArgNum, ret.Get("error").String()) + ret = f.(js.Value).Call("NewConditionalStyle", js.ValueOf(map[string]interface{}{"Fill": 1})) assert.Equal(t, errArgType.Error(), ret.Get("error").String()) assert.Equal(t, 0, ret.Get("style").Int()) + + ret = f.(js.Value).Call("GetConditionalStyle", js.ValueOf(true)) + assert.EqualError(t, errArgType, ret.Get("error").String()) } func TestNewSheet(t *testing.T) { diff --git a/src/index.d.ts b/src/index.d.ts index a9daaaa..3ed70f3 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -1452,6 +1452,13 @@ declare module 'excelize-wasm' { */ GetComments(sheet: string): { comments: Comment[], error: string | null } + /** + * GetConditionalStyle returns conditional format style definition by + * specified style index. + * @param styleID The style ID + */ + GetConditionalStyle(styleID: number): { style: Style, error: string | null } + /** * GetDefaultFont provides the default font name currently set in the * workbook. The spreadsheet generated by excelize default font is Calibri.