Skip to content

Commit e920d00

Browse files
committed
Add new functions AddIgnoredErrors and AddVBAProject
- Add new exported IgnoredErrorsType enumeration - Upgrade the Excelize library version - Update the unit tests and documentation
1 parent 1295274 commit e920d00

File tree

5 files changed

+142
-10
lines changed

5 files changed

+142
-10
lines changed

cmd/go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module github.com/xuri/excelize-wasm/cmd
22

3-
go 1.18
3+
go 1.20
44

55
require (
66
github.com/stretchr/testify v1.9.0
7-
github.com/xuri/excelize/v2 v2.9.1-0.20241212084348-b53bad35417e
7+
github.com/xuri/excelize/v2 v2.9.1-0.20241221071117-9934bf5c8634
88
golang.org/x/image v0.23.0
99
)
1010

@@ -17,7 +17,7 @@ require (
1717
github.com/xuri/efp v0.0.0-20241211021726-c4e992084aa6 // indirect
1818
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 // indirect
1919
golang.org/x/crypto v0.31.0 // indirect
20-
golang.org/x/net v0.32.0 // indirect
20+
golang.org/x/net v0.33.0 // indirect
2121
golang.org/x/text v0.21.0 // indirect
2222
gopkg.in/yaml.v3 v3.0.1 // indirect
2323
)

cmd/go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ github.com/tiendc/go-deepcopy v1.2.0 h1:6vCCs+qdLQHzFqY1fcPirsAWOmrLbuccilfp8UzD
1313
github.com/tiendc/go-deepcopy v1.2.0/go.mod h1:toXoeQoUqXOOS/X4sKuiAoSk6elIdqc0pN7MTgOOo2I=
1414
github.com/xuri/efp v0.0.0-20241211021726-c4e992084aa6 h1:8m6DWBG+dlFNbx5ynvrE7NgI+Y7OlZVMVTpayoW+rCc=
1515
github.com/xuri/efp v0.0.0-20241211021726-c4e992084aa6/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
16-
github.com/xuri/excelize/v2 v2.9.1-0.20241212084348-b53bad35417e h1:jQsdgMMXOtbfKlwLUH/O/vbeFaOjfH5b4hq9v8W9Cfo=
17-
github.com/xuri/excelize/v2 v2.9.1-0.20241212084348-b53bad35417e/go.mod h1:BUUJqAS92MYasIroanizqBN1x/8uoGby7IR4BjT0Vnk=
16+
github.com/xuri/excelize/v2 v2.9.1-0.20241221071117-9934bf5c8634 h1:GDmRYes16eUdzi5a4Iv2nqSn3jZUch8DNttR/qNe1NU=
17+
github.com/xuri/excelize/v2 v2.9.1-0.20241221071117-9934bf5c8634/go.mod h1:NBRx6e5FHFx4mHLiYG1QBONNvNNSs/wrtzS+h56/A6k=
1818
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 h1:hPVCafDV85blFTabnqKgNhDCkJX25eik94Si9cTER4A=
1919
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
2020
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
2121
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
2222
golang.org/x/image v0.23.0 h1:HseQ7c2OpPKTPVzNjG5fwJsOTCiiwS4QdsYi5XU6H68=
2323
golang.org/x/image v0.23.0/go.mod h1:wJJBTdLfCCf3tiHa1fNxpZmUI4mmoZvwMCPP0ddoNKY=
24-
golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI=
25-
golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs=
24+
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
25+
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
2626
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
2727
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
2828
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

cmd/main.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,16 @@ func regConstants() {
296296
"HeaderFooterImagePositionLeft": int(excelize.HeaderFooterImagePositionLeft),
297297
"HeaderFooterImagePositionCenter": int(excelize.HeaderFooterImagePositionCenter),
298298
"HeaderFooterImagePositionRight": int(excelize.HeaderFooterImagePositionRight),
299+
// IgnoredErrorsType enumeration
300+
"IgnoredErrorsEvalError": int(excelize.IgnoredErrorsEvalError),
301+
"IgnoredErrorsTwoDigitTextYear": int(excelize.IgnoredErrorsTwoDigitTextYear),
302+
"IgnoredErrorsNumberStoredAsText": int(excelize.IgnoredErrorsNumberStoredAsText),
303+
"IgnoredErrorsFormula": int(excelize.IgnoredErrorsFormula),
304+
"IgnoredErrorsFormulaRange": int(excelize.IgnoredErrorsFormulaRange),
305+
"IgnoredErrorsUnlockedFormula": int(excelize.IgnoredErrorsUnlockedFormula),
306+
"IgnoredErrorsEmptyCellReference": int(excelize.IgnoredErrorsEmptyCellReference),
307+
"IgnoredErrorsListDataValidation": int(excelize.IgnoredErrorsListDataValidation),
308+
"IgnoredErrorsCalculatedColumn": int(excelize.IgnoredErrorsCalculatedColumn),
299309
// PictureInsertType enumeration
300310
"PictureInsertTypePlaceOverCells": int(excelize.PictureInsertTypePlaceOverCells),
301311
"PictureInsertTypePlaceInCell": int(excelize.PictureInsertTypePlaceInCell),
@@ -314,12 +324,14 @@ func regInteropFunc(f *excelize.File, fn map[string]interface{}) interface{} {
314324
"AddDataValidation": AddDataValidation(f),
315325
"AddFormControl": AddFormControl(f),
316326
"AddHeaderFooterImage": AddHeaderFooterImage(f),
327+
"AddIgnoredErrors": AddIgnoredErrors(f),
317328
"AddPictureFromBytes": AddPictureFromBytes(f),
318329
"AddPivotTable": AddPivotTable(f),
319330
"AddShape": AddShape(f),
320331
"AddSlicer": AddSlicer(f),
321332
"AddSparkline": AddSparkline(f),
322333
"AddTable": AddTable(f),
334+
"AddVBAProject": AddVBAProject(f),
323335
"AutoFilter": AutoFilter(f),
324336
"CalcCellValue": CalcCellValue(f),
325337
"CopySheet": CopySheet(f),
@@ -1199,6 +1211,25 @@ func AddHeaderFooterImage(f *excelize.File) func(this js.Value, args []js.Value)
11991211
}
12001212
}
12011213

1214+
// AddIgnoredErrors provides the method to ignored error for a range of cells.
1215+
func AddIgnoredErrors(f *excelize.File) func(this js.Value, args []js.Value) interface{} {
1216+
return func(this js.Value, args []js.Value) interface{} {
1217+
ret := map[string]interface{}{"error": nil}
1218+
if err := prepareArgs(args, []argsRule{
1219+
{types: []js.Type{js.TypeString}},
1220+
{types: []js.Type{js.TypeString}},
1221+
{types: []js.Type{js.TypeNumber}},
1222+
}); err != nil {
1223+
ret["error"] = err.Error()
1224+
return js.ValueOf(ret)
1225+
}
1226+
if err := f.AddIgnoredErrors(args[0].String(), args[1].String(), excelize.IgnoredErrorsType(args[2].Int())); err != nil {
1227+
ret["error"] = err.Error()
1228+
}
1229+
return js.ValueOf(ret)
1230+
}
1231+
}
1232+
12021233
// AddPictureFromBytes provides the method to add picture in a sheet by given
12031234
// picture format set (such as offset, scale, aspect ratio setting and print
12041235
// settings), file base name, extension name and file bytes.
@@ -1361,6 +1392,26 @@ func AddTable(f *excelize.File) func(this js.Value, args []js.Value) interface{}
13611392
}
13621393
}
13631394

1395+
// AddVBAProject provides the method to add vbaProject.bin file which contains
1396+
// functions and/or macros. The file extension should be XLSM or XLTM.
1397+
func AddVBAProject(f *excelize.File) func(this js.Value, args []js.Value) interface{} {
1398+
return func(this js.Value, args []js.Value) interface{} {
1399+
ret := map[string]interface{}{"error": nil}
1400+
if err := prepareArgs(args, []argsRule{
1401+
{types: []js.Type{js.TypeObject}},
1402+
}); err != nil {
1403+
ret["error"] = err.Error()
1404+
return js.ValueOf(ret)
1405+
}
1406+
buf := make([]byte, args[0].Get("length").Int())
1407+
js.CopyBytesToGo(buf, args[0])
1408+
if err := f.AddVBAProject(buf); err != nil {
1409+
ret["error"] = err.Error()
1410+
}
1411+
return js.ValueOf(ret)
1412+
}
1413+
}
1414+
13641415
// AutoFilter provides the method to add auto filter in a worksheet by given
13651416
// worksheet name, range reference and settings. An auto filter in Excel is a
13661417
// way of filtering a 2D range of data based on some simple criteria.
@@ -3314,7 +3365,7 @@ func SetCellInt(f *excelize.File) func(this js.Value, args []js.Value) interface
33143365
}
33153366

33163367
// SetCellRichText provides a function to set cell with rich text by given
3317-
// worksheet.
3368+
// worksheet name, cell reference and rich text runs.
33183369
func SetCellRichText(f *excelize.File) func(this js.Value, args []js.Value) interface{} {
33193370
return func(this js.Value, args []js.Value) interface{} {
33203371
ret := map[string]interface{}{"error": nil}

cmd/main_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,23 @@ func TestAddHeaderFooterImage(t *testing.T) {
535535
assert.Equal(t, "sheet SheetN does not exist", ret.Get("error").String())
536536
}
537537

538+
func TestAddIgnoredErrors(t *testing.T) {
539+
f := NewFile(js.Value{}, []js.Value{})
540+
assert.True(t, f.(js.Value).Get("error").IsNull())
541+
542+
ret := f.(js.Value).Call("AddIgnoredErrors", js.ValueOf("Sheet1"), js.ValueOf("A1"), js.ValueOf(int(excelize.IgnoredErrorsEvalError)))
543+
assert.True(t, ret.Get("error").IsNull(), ret.Get("error").String())
544+
545+
ret = f.(js.Value).Call("AddIgnoredErrors", js.ValueOf("SheetN"), js.ValueOf("A1"), js.ValueOf(int(excelize.IgnoredErrorsEvalError)))
546+
assert.Equal(t, "sheet SheetN does not exist", ret.Get("error").String())
547+
548+
ret = f.(js.Value).Call("AddIgnoredErrors", js.ValueOf("Sheet1"), js.ValueOf("A1"))
549+
assert.EqualError(t, errArgNum, ret.Get("error").String())
550+
551+
ret = f.(js.Value).Call("AddIgnoredErrors", js.ValueOf("SheetN"), js.ValueOf("A1"), js.ValueOf(true))
552+
assert.EqualError(t, errArgType, ret.Get("error").String())
553+
}
554+
538555
func TestAddPictureFromBytes(t *testing.T) {
539556
buf, err := os.ReadFile(filepath.Join("..", "chart.png"))
540557
assert.NoError(t, err)
@@ -854,6 +871,29 @@ func TestTable(t *testing.T) {
854871
assert.Equal(t, "table X does not exist", ret.Get("error").String())
855872
}
856873

874+
func TestAddVBAProject(t *testing.T) {
875+
f := NewFile(js.Value{}, []js.Value{})
876+
assert.True(t, f.(js.Value).Get("error").IsNull())
877+
878+
oleIdentifier := []byte{0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1}
879+
uint8Array := js.Global().Get("Uint8Array").New(js.ValueOf(len(oleIdentifier)))
880+
for k, v := range oleIdentifier {
881+
uint8Array.SetIndex(k, v)
882+
}
883+
ret := f.(js.Value).Call("AddVBAProject", js.ValueOf(uint8Array))
884+
assert.True(t, ret.Get("error").IsNull())
885+
886+
uint8Array = js.Global().Get("Uint8Array").New(js.ValueOf(1))
887+
ret = f.(js.Value).Call("AddVBAProject", js.ValueOf(uint8Array))
888+
assert.Equal(t, excelize.ErrAddVBAProject.Error(), ret.Get("error").String())
889+
890+
ret = f.(js.Value).Call("AddVBAProject")
891+
assert.EqualError(t, errArgNum, ret.Get("error").String())
892+
893+
ret = f.(js.Value).Call("AddVBAProject", js.ValueOf(true))
894+
assert.EqualError(t, errArgType, ret.Get("error").String())
895+
}
896+
857897
func TestAutoFilter(t *testing.T) {
858898
f := NewFile(js.Value{}, []js.Value{})
859899
assert.True(t, f.(js.Value).Get("error").IsNull())

src/index.d.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,21 @@ declare module 'excelize-wasm' {
627627
HeaderFooterImagePositionRight,
628628
}
629629

630+
/**
631+
* IgnoredErrorsType is the type of ignored errors.
632+
*/
633+
export enum IgnoredErrorsType {
634+
IgnoredErrorsEvalError,
635+
IgnoredErrorsTwoDigitTextYear,
636+
IgnoredErrorsNumberStoredAsText,
637+
IgnoredErrorsFormula,
638+
IgnoredErrorsFormulaRange,
639+
IgnoredErrorsUnlockedFormula,
640+
IgnoredErrorsEmptyCellReference,
641+
IgnoredErrorsListDataValidation,
642+
IgnoredErrorsCalculatedColumn,
643+
}
644+
630645
/**
631646
* HeaderFooterImageOptions defines the settings for an image to be accessible
632647
* from the worksheet header and footer options.
@@ -1698,6 +1713,15 @@ declare module 'excelize-wasm' {
16981713
*/
16991714
AddDataValidation(sheet: string, dv: DataValidation): { error: string | null }
17001715

1716+
/**
1717+
* AddDataValidation provides the method to ignored error for a range of
1718+
* cells.
1719+
* @param sheet The worksheet name
1720+
* @param rangeRef The top-left and right-bottom cell range reference
1721+
* @param ignoredErrorsType The enumeration value of ignored errors type
1722+
*/
1723+
AddIgnoredErrors(sheet: string, rangeRef: string, ignoredErrorsType: IgnoredErrorsType): { error: string | null }
1724+
17011725
/**
17021726
* AddPictureFromBytes provides the method to add picture in a sheet by given
17031727
* picture format set (such as offset, scale, aspect ratio setting and print
@@ -2000,6 +2024,14 @@ declare module 'excelize-wasm' {
20002024
*/
20012025
AddTable(sheet: string, opts: TableOptions): { error: string | null }
20022026

2027+
/**
2028+
* AddVBAProject provides the method to add vbaProject.bin file which
2029+
* contains functions and/or macros. The file extension should be XLSM or
2030+
* XLTM.
2031+
* @param file The contents buffer of the vbaProject.bin file
2032+
*/
2033+
AddVBAProject(file: Uint8Array): { error: string | null }
2034+
20032035
/**
20042036
* AutoFilter provides the method to add auto filter in a worksheet by
20052037
* given worksheet name, range reference and settings. An auto filter in
@@ -2970,8 +3002,8 @@ declare module 'excelize-wasm' {
29703002

29713003
/**
29723004
* SetCellRichText provides a function to set cell with rich text by given
2973-
* worksheet. For example, set rich text on the A1 cell of the worksheet
2974-
* named Sheet1:
3005+
* worksheet name, cell reference and rich text runs. For example, set rich
3006+
* text on the A1 cell of the worksheet named Sheet1:
29753007
*
29763008
* ```typescript
29773009
* const { init } = require('excelize-wasm');
@@ -3858,5 +3890,14 @@ declare module 'excelize-wasm' {
38583890
HeaderFooterImagePositionLeft: typeof HeaderFooterImagePositionType.HeaderFooterImagePositionLeft;
38593891
HeaderFooterImagePositionCenter: typeof HeaderFooterImagePositionType.HeaderFooterImagePositionCenter;
38603892
HeaderFooterImagePositionRight: typeof HeaderFooterImagePositionType.HeaderFooterImagePositionRight;
3893+
IgnoredErrorsEvalError: typeof IgnoredErrorsType.IgnoredErrorsEvalError;
3894+
IgnoredErrorsTwoDigitTextYear: typeof IgnoredErrorsType.IgnoredErrorsTwoDigitTextYear;
3895+
IgnoredErrorsNumberStoredAsText: typeof IgnoredErrorsType.IgnoredErrorsNumberStoredAsText;
3896+
IgnoredErrorsFormula: typeof IgnoredErrorsType.IgnoredErrorsFormula;
3897+
IgnoredErrorsFormulaRange: typeof IgnoredErrorsType.IgnoredErrorsFormulaRange;
3898+
IgnoredErrorsUnlockedFormula: typeof IgnoredErrorsType.IgnoredErrorsUnlockedFormula;
3899+
IgnoredErrorsEmptyCellReference: typeof IgnoredErrorsType.IgnoredErrorsEmptyCellReference;
3900+
IgnoredErrorsListDataValidation: typeof IgnoredErrorsType.IgnoredErrorsListDataValidation;
3901+
IgnoredErrorsCalculatedColumn: typeof IgnoredErrorsType.IgnoredErrorsCalculatedColumn;
38613902
}>;
38623903
}

0 commit comments

Comments
 (0)