Skip to content

Commit 17b804b

Browse files
committed
New function SetCustomProps has been added
- Update unit tests - Upgrade the Excelize library version - Update en docs for the form control
1 parent 0affc67 commit 17b804b

File tree

5 files changed

+106
-20
lines changed

5 files changed

+106
-20
lines changed

cmd/go.mod

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.23.0
44

55
require (
66
github.com/stretchr/testify v1.10.0
7-
github.com/xuri/excelize/v2 v2.9.2-0.20250514064621-72b731a04dcc
7+
github.com/xuri/excelize/v2 v2.9.2-0.20250614235856-d434acdde46b
88
golang.org/x/image v0.27.0
99
)
1010

@@ -13,11 +13,11 @@ require (
1313
github.com/pmezard/go-difflib v1.0.0 // indirect
1414
github.com/richardlehane/mscfb v1.0.4 // indirect
1515
github.com/richardlehane/msoleps v1.0.4 // indirect
16-
github.com/tiendc/go-deepcopy v1.6.0 // indirect
16+
github.com/tiendc/go-deepcopy v1.6.1 // indirect
1717
github.com/xuri/efp v0.0.1 // indirect
18-
github.com/xuri/nfp v0.0.1 // indirect
19-
golang.org/x/crypto v0.38.0 // indirect
20-
golang.org/x/net v0.40.0 // indirect
21-
golang.org/x/text v0.25.0 // indirect
18+
github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9 // indirect
19+
golang.org/x/crypto v0.39.0 // indirect
20+
golang.org/x/net v0.41.0 // indirect
21+
golang.org/x/text v0.26.0 // indirect
2222
gopkg.in/yaml.v3 v3.0.1 // indirect
2323
)

cmd/go.sum

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ github.com/richardlehane/msoleps v1.0.4 h1:WuESlvhX3gH2IHcd8UqyCuFY5yiq/GR/yqaSM
99
github.com/richardlehane/msoleps v1.0.4/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg=
1010
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
1111
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
12-
github.com/tiendc/go-deepcopy v1.6.0 h1:0UtfV/imoCwlLxVsyfUd4hNHnB3drXsfle+wzSCA5Wo=
13-
github.com/tiendc/go-deepcopy v1.6.0/go.mod h1:toXoeQoUqXOOS/X4sKuiAoSk6elIdqc0pN7MTgOOo2I=
12+
github.com/tiendc/go-deepcopy v1.6.1 h1:uVRTItFeNHkMcLueHS7OCsxgxT9P8MzGB/taUa2Y4Tk=
13+
github.com/tiendc/go-deepcopy v1.6.1/go.mod h1:toXoeQoUqXOOS/X4sKuiAoSk6elIdqc0pN7MTgOOo2I=
1414
github.com/xuri/efp v0.0.1 h1:fws5Rv3myXyYni8uwj2qKjVaRP30PdjeYe2Y6FDsCL8=
1515
github.com/xuri/efp v0.0.1/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
16-
github.com/xuri/excelize/v2 v2.9.2-0.20250514064621-72b731a04dcc h1:3hrnD/UeCJhxTnB50we1yfh6WgBrgsH79B0OorprG1s=
17-
github.com/xuri/excelize/v2 v2.9.2-0.20250514064621-72b731a04dcc/go.mod h1:x7L6pKz2dvo9ejrRuD8Lnl98z4JLt0TGAwjhW+EiP8s=
18-
github.com/xuri/nfp v0.0.1 h1:MDamSGatIvp8uOmDP8FnmjuQpu90NzdJxo7242ANR9Q=
19-
github.com/xuri/nfp v0.0.1/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
20-
golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8=
21-
golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw=
16+
github.com/xuri/excelize/v2 v2.9.2-0.20250614235856-d434acdde46b h1:0+9AA5rFR07411NM9EdIagfecnG5weEFYgsiiPV/O+s=
17+
github.com/xuri/excelize/v2 v2.9.2-0.20250614235856-d434acdde46b/go.mod h1:isQygQdjiU88/HpJYtp+6TN/FH29HvVjWT8vFk3t5+w=
18+
github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9 h1:+C0TIdyyYmzadGaL/HBLbf3WdLgC29pgyhTjAT/0nuE=
19+
github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
20+
golang.org/x/crypto v0.39.0 h1:SHs+kF4LP+f+p14esP5jAoDpHU8Gu/v9lFRK6IT5imM=
21+
golang.org/x/crypto v0.39.0/go.mod h1:L+Xg3Wf6HoL4Bn4238Z6ft6KfEpN0tJGo53AAPC632U=
2222
golang.org/x/image v0.27.0 h1:C8gA4oWU/tKkdCfYT6T2u4faJu3MeNS5O8UPWlPF61w=
2323
golang.org/x/image v0.27.0/go.mod h1:xbdrClrAUway1MUTEZDq9mz/UpRwYAkFFNUslZtcB+g=
24-
golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY=
25-
golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds=
26-
golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4=
27-
golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA=
24+
golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw=
25+
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
26+
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
27+
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
2828
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2929
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3030
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

cmd/main.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ func regInteropFunc(f *excelize.File, fn map[string]interface{}) interface{} {
425425
"SetColVisible": SetColVisible(f),
426426
"SetColWidth": SetColWidth(f),
427427
"SetConditionalFormat": SetConditionalFormat(f),
428+
"SetCustomProps": SetCustomProps(f),
428429
"SetDefaultFont": SetDefaultFont(f),
429430
"SetDefinedName": SetDefinedName(f),
430431
"SetDocProps": SetDocProps(f),
@@ -1154,7 +1155,7 @@ func AddDataValidation(f *excelize.File) func(this js.Value, args []js.Value) in
11541155
}
11551156
}
11561157

1157-
// AddFormControl provides the method to add form control button in a worksheet
1158+
// AddFormControl provides the method to add form control object in a worksheet
11581159
// by given worksheet name and form control options. Supported form control
11591160
// type: button, check box, group box, label, option button, scroll bar and
11601161
// spinner. If set macro for the form control, the workbook extension should be
@@ -3636,6 +3637,45 @@ func SetConditionalFormat(f *excelize.File) func(this js.Value, args []js.Value)
36363637
}
36373638
}
36383639

3640+
// SetCustomProps provides a function to set custom file properties by given
3641+
// property name and value. If the property name already exists, it will be
3642+
// updated, otherwise a new property will be added. The value can be of type
3643+
// int32, float64, bool, string, time.Time or nil. The property will be delete
3644+
// if the value is nil. The function returns an error if the property value is
3645+
// not of the correct type.
3646+
func SetCustomProps(f *excelize.File) func(this js.Value, args []js.Value) interface{} {
3647+
return func(this js.Value, args []js.Value) interface{} {
3648+
ret := map[string]interface{}{"error": nil}
3649+
if err := prepareArgs(args, []argsRule{
3650+
{types: []js.Type{js.TypeObject}},
3651+
}); err != nil {
3652+
ret["error"] = err.Error()
3653+
return js.ValueOf(ret)
3654+
}
3655+
if _, err := jsValueToGo(args[0], reflect.TypeOf(excelize.CustomProperty{})); err != nil {
3656+
ret["error"] = err.Error()
3657+
return js.ValueOf(ret)
3658+
}
3659+
prop := excelize.CustomProperty{Name: args[0].Get("Name").String()}
3660+
val := args[0].Get("Value")
3661+
switch val.Type() {
3662+
case js.TypeNull:
3663+
prop.Value = nil
3664+
case js.TypeBoolean:
3665+
prop.Value = val.Bool()
3666+
case js.TypeNumber:
3667+
prop.Value = val.Float()
3668+
default: // js.TypeString:
3669+
prop.Value = val.String()
3670+
}
3671+
if err := f.SetCustomProps(prop); err != nil {
3672+
ret["error"] = err.Error()
3673+
return js.ValueOf(ret)
3674+
}
3675+
return js.ValueOf(ret)
3676+
}
3677+
}
3678+
36393679
// SetDefaultFont changes the default font in the workbook.
36403680
func SetDefaultFont(f *excelize.File) func(this js.Value, args []js.Value) interface{} {
36413681
return func(this js.Value, args []js.Value) interface{} {

cmd/main_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2272,6 +2272,32 @@ func TestSetConditionalFormat(t *testing.T) {
22722272
assert.Equal(t, "sheet SheetN does not exist", ret.Get("error").String())
22732273
}
22742274

2275+
func TestCustomProps(t *testing.T) {
2276+
f := NewFile(js.Value{}, []js.Value{})
2277+
assert.True(t, f.(js.Value).Get("error").IsNull())
2278+
2279+
for _, prop := range []interface{}{
2280+
map[string]interface{}{"Name": "Text Prop", "Value": "text"},
2281+
map[string]interface{}{"Name": "Boolean Prop 1", "Value": true},
2282+
map[string]interface{}{"Name": "Boolean Prop 2", "Value": false},
2283+
map[string]interface{}{"Name": "Number Prop 1", "Value": -123.456},
2284+
map[string]interface{}{"Name": "Number Prop 2", "Value": 1},
2285+
map[string]interface{}{"Name": "Number Prop 2", "Value": nil},
2286+
} {
2287+
ret := f.(js.Value).Call("SetCustomProps", js.ValueOf(prop))
2288+
assert.True(t, ret.Get("error").IsNull())
2289+
}
2290+
2291+
ret := f.(js.Value).Call("SetCustomProps")
2292+
assert.EqualError(t, errArgNum, ret.Get("error").String())
2293+
2294+
ret = f.(js.Value).Call("SetCustomProps", js.ValueOf(1))
2295+
assert.EqualError(t, errArgType, ret.Get("error").String())
2296+
2297+
ret = f.(js.Value).Call("SetCustomProps", js.ValueOf(map[string]interface{}{"Name": 1}))
2298+
assert.EqualError(t, errArgType, ret.Get("error").String())
2299+
}
2300+
22752301
func TestSetDefaultFont(t *testing.T) {
22762302
f := NewFile(js.Value{}, []js.Value{})
22772303
assert.True(t, f.(js.Value).Get("error").IsNull())

src/index.d.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,15 @@ declare module 'excelize-wasm' {
240240
StopIfTrue?: boolean;
241241
};
242242

243+
/**
244+
* CustomProperty directly maps the custom property of the workbook. The value
245+
* date type may be one of the following: number, boolean, string or null.
246+
*/
247+
export type CustomProperty = {
248+
Name: string;
249+
Value?: boolean | number | string | null;
250+
};
251+
243252
/**
244253
* Shape directly maps the format settings of the shape.
245254
*/
@@ -1827,7 +1836,7 @@ declare module 'excelize-wasm' {
18271836
AddPictureFromBytes(sheet: string, cell: string, pic: Picture): { error: string | null }
18281837

18291838
/**
1830-
* AddFormControl provides the method to add form control button in a
1839+
* AddFormControl provides the method to add form control object in a
18311840
* worksheet by given worksheet name and form control options. Supported
18321841
* form control type: button, check box, group box, label, option button,
18331842
* scroll bar and spinner. If set macro for the form control, the workbook
@@ -3283,6 +3292,17 @@ declare module 'excelize-wasm' {
32833292
*/
32843293
SetConditionalFormat(sheet: string, reference: string, opts: ConditionalFormatOptions[]): { error: string | null }
32853294

3295+
/**
3296+
* SetCustomProps provides a function to set custom file properties by given
3297+
* property name and value. If the property name already exists, it will be
3298+
* updated, otherwise a new property will be added. The value can be of type
3299+
* number, boolean, string, null. The property will be delete if the value
3300+
* is null. The function returns an error if the property value is not of
3301+
* the correct type.
3302+
* @param prop Custom property of the workbook
3303+
*/
3304+
SetCustomProps(prop: CustomProperty): { error: string | null }
3305+
32863306
/**
32873307
* SetDefaultFont changes the default font in the workbook.
32883308
* @param fontName The font name

0 commit comments

Comments
 (0)