Skip to content

Commit

Permalink
Add new functions AddIgnoredErrors and AddVBAProject
Browse files Browse the repository at this point in the history
- Add new exported IgnoredErrorsType enumeration
- Upgrade the Excelize library version
- Update the unit tests and documentation
  • Loading branch information
xuri committed Dec 28, 2024
1 parent 1295274 commit e920d00
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 10 deletions.
6 changes: 3 additions & 3 deletions cmd/go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module github.com/xuri/excelize-wasm/cmd

go 1.18
go 1.20

require (
github.com/stretchr/testify v1.9.0
github.com/xuri/excelize/v2 v2.9.1-0.20241212084348-b53bad35417e
github.com/xuri/excelize/v2 v2.9.1-0.20241221071117-9934bf5c8634
golang.org/x/image v0.23.0
)

Expand All @@ -17,7 +17,7 @@ require (
github.com/xuri/efp v0.0.0-20241211021726-c4e992084aa6 // indirect
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/net v0.32.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/text v0.21.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
8 changes: 4 additions & 4 deletions cmd/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ github.com/tiendc/go-deepcopy v1.2.0 h1:6vCCs+qdLQHzFqY1fcPirsAWOmrLbuccilfp8UzD
github.com/tiendc/go-deepcopy v1.2.0/go.mod h1:toXoeQoUqXOOS/X4sKuiAoSk6elIdqc0pN7MTgOOo2I=
github.com/xuri/efp v0.0.0-20241211021726-c4e992084aa6 h1:8m6DWBG+dlFNbx5ynvrE7NgI+Y7OlZVMVTpayoW+rCc=
github.com/xuri/efp v0.0.0-20241211021726-c4e992084aa6/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
github.com/xuri/excelize/v2 v2.9.1-0.20241212084348-b53bad35417e h1:jQsdgMMXOtbfKlwLUH/O/vbeFaOjfH5b4hq9v8W9Cfo=
github.com/xuri/excelize/v2 v2.9.1-0.20241212084348-b53bad35417e/go.mod h1:BUUJqAS92MYasIroanizqBN1x/8uoGby7IR4BjT0Vnk=
github.com/xuri/excelize/v2 v2.9.1-0.20241221071117-9934bf5c8634 h1:GDmRYes16eUdzi5a4Iv2nqSn3jZUch8DNttR/qNe1NU=
github.com/xuri/excelize/v2 v2.9.1-0.20241221071117-9934bf5c8634/go.mod h1:NBRx6e5FHFx4mHLiYG1QBONNvNNSs/wrtzS+h56/A6k=
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.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/image v0.23.0 h1:HseQ7c2OpPKTPVzNjG5fwJsOTCiiwS4QdsYi5XU6H68=
golang.org/x/image v0.23.0/go.mod h1:wJJBTdLfCCf3tiHa1fNxpZmUI4mmoZvwMCPP0ddoNKY=
golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI=
golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
53 changes: 52 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,16 @@ func regConstants() {
"HeaderFooterImagePositionLeft": int(excelize.HeaderFooterImagePositionLeft),
"HeaderFooterImagePositionCenter": int(excelize.HeaderFooterImagePositionCenter),
"HeaderFooterImagePositionRight": int(excelize.HeaderFooterImagePositionRight),
// IgnoredErrorsType enumeration
"IgnoredErrorsEvalError": int(excelize.IgnoredErrorsEvalError),
"IgnoredErrorsTwoDigitTextYear": int(excelize.IgnoredErrorsTwoDigitTextYear),
"IgnoredErrorsNumberStoredAsText": int(excelize.IgnoredErrorsNumberStoredAsText),
"IgnoredErrorsFormula": int(excelize.IgnoredErrorsFormula),
"IgnoredErrorsFormulaRange": int(excelize.IgnoredErrorsFormulaRange),
"IgnoredErrorsUnlockedFormula": int(excelize.IgnoredErrorsUnlockedFormula),
"IgnoredErrorsEmptyCellReference": int(excelize.IgnoredErrorsEmptyCellReference),
"IgnoredErrorsListDataValidation": int(excelize.IgnoredErrorsListDataValidation),
"IgnoredErrorsCalculatedColumn": int(excelize.IgnoredErrorsCalculatedColumn),
// PictureInsertType enumeration
"PictureInsertTypePlaceOverCells": int(excelize.PictureInsertTypePlaceOverCells),
"PictureInsertTypePlaceInCell": int(excelize.PictureInsertTypePlaceInCell),
Expand All @@ -314,12 +324,14 @@ func regInteropFunc(f *excelize.File, fn map[string]interface{}) interface{} {
"AddDataValidation": AddDataValidation(f),
"AddFormControl": AddFormControl(f),
"AddHeaderFooterImage": AddHeaderFooterImage(f),
"AddIgnoredErrors": AddIgnoredErrors(f),
"AddPictureFromBytes": AddPictureFromBytes(f),
"AddPivotTable": AddPivotTable(f),
"AddShape": AddShape(f),
"AddSlicer": AddSlicer(f),
"AddSparkline": AddSparkline(f),
"AddTable": AddTable(f),
"AddVBAProject": AddVBAProject(f),
"AutoFilter": AutoFilter(f),
"CalcCellValue": CalcCellValue(f),
"CopySheet": CopySheet(f),
Expand Down Expand Up @@ -1199,6 +1211,25 @@ func AddHeaderFooterImage(f *excelize.File) func(this js.Value, args []js.Value)
}
}

// AddIgnoredErrors provides the method to ignored error for a range of cells.
func AddIgnoredErrors(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}},
{types: []js.Type{js.TypeNumber}},
}); err != nil {
ret["error"] = err.Error()
return js.ValueOf(ret)
}
if err := f.AddIgnoredErrors(args[0].String(), args[1].String(), excelize.IgnoredErrorsType(args[2].Int())); err != nil {
ret["error"] = err.Error()
}
return js.ValueOf(ret)
}
}

// AddPictureFromBytes provides the method to add picture in a sheet by given
// picture format set (such as offset, scale, aspect ratio setting and print
// settings), file base name, extension name and file bytes.
Expand Down Expand Up @@ -1361,6 +1392,26 @@ func AddTable(f *excelize.File) func(this js.Value, args []js.Value) interface{}
}
}

// AddVBAProject provides the method to add vbaProject.bin file which contains
// functions and/or macros. The file extension should be XLSM or XLTM.
func AddVBAProject(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.TypeObject}},
}); err != nil {
ret["error"] = err.Error()
return js.ValueOf(ret)
}
buf := make([]byte, args[0].Get("length").Int())
js.CopyBytesToGo(buf, args[0])
if err := f.AddVBAProject(buf); err != nil {
ret["error"] = err.Error()
}
return js.ValueOf(ret)
}
}

// AutoFilter provides the method to add auto filter in a worksheet by given
// worksheet name, range reference and settings. An auto filter in Excel is a
// way of filtering a 2D range of data based on some simple criteria.
Expand Down Expand Up @@ -3314,7 +3365,7 @@ func SetCellInt(f *excelize.File) func(this js.Value, args []js.Value) interface
}

// SetCellRichText provides a function to set cell with rich text by given
// worksheet.
// worksheet name, cell reference and rich text runs.
func SetCellRichText(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}
Expand Down
40 changes: 40 additions & 0 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,23 @@ func TestAddHeaderFooterImage(t *testing.T) {
assert.Equal(t, "sheet SheetN does not exist", ret.Get("error").String())
}

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

ret := f.(js.Value).Call("AddIgnoredErrors", js.ValueOf("Sheet1"), js.ValueOf("A1"), js.ValueOf(int(excelize.IgnoredErrorsEvalError)))
assert.True(t, ret.Get("error").IsNull(), ret.Get("error").String())

ret = f.(js.Value).Call("AddIgnoredErrors", js.ValueOf("SheetN"), js.ValueOf("A1"), js.ValueOf(int(excelize.IgnoredErrorsEvalError)))
assert.Equal(t, "sheet SheetN does not exist", ret.Get("error").String())

ret = f.(js.Value).Call("AddIgnoredErrors", js.ValueOf("Sheet1"), js.ValueOf("A1"))
assert.EqualError(t, errArgNum, ret.Get("error").String())

ret = f.(js.Value).Call("AddIgnoredErrors", js.ValueOf("SheetN"), js.ValueOf("A1"), js.ValueOf(true))
assert.EqualError(t, errArgType, ret.Get("error").String())
}

func TestAddPictureFromBytes(t *testing.T) {
buf, err := os.ReadFile(filepath.Join("..", "chart.png"))
assert.NoError(t, err)
Expand Down Expand Up @@ -854,6 +871,29 @@ func TestTable(t *testing.T) {
assert.Equal(t, "table X does not exist", ret.Get("error").String())
}

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

oleIdentifier := []byte{0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1}
uint8Array := js.Global().Get("Uint8Array").New(js.ValueOf(len(oleIdentifier)))
for k, v := range oleIdentifier {
uint8Array.SetIndex(k, v)
}
ret := f.(js.Value).Call("AddVBAProject", js.ValueOf(uint8Array))
assert.True(t, ret.Get("error").IsNull())

uint8Array = js.Global().Get("Uint8Array").New(js.ValueOf(1))
ret = f.(js.Value).Call("AddVBAProject", js.ValueOf(uint8Array))
assert.Equal(t, excelize.ErrAddVBAProject.Error(), ret.Get("error").String())

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

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

func TestAutoFilter(t *testing.T) {
f := NewFile(js.Value{}, []js.Value{})
assert.True(t, f.(js.Value).Get("error").IsNull())
Expand Down
45 changes: 43 additions & 2 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,21 @@ declare module 'excelize-wasm' {
HeaderFooterImagePositionRight,
}

/**
* IgnoredErrorsType is the type of ignored errors.
*/
export enum IgnoredErrorsType {
IgnoredErrorsEvalError,
IgnoredErrorsTwoDigitTextYear,
IgnoredErrorsNumberStoredAsText,
IgnoredErrorsFormula,
IgnoredErrorsFormulaRange,
IgnoredErrorsUnlockedFormula,
IgnoredErrorsEmptyCellReference,
IgnoredErrorsListDataValidation,
IgnoredErrorsCalculatedColumn,
}

/**
* HeaderFooterImageOptions defines the settings for an image to be accessible
* from the worksheet header and footer options.
Expand Down Expand Up @@ -1698,6 +1713,15 @@ declare module 'excelize-wasm' {
*/
AddDataValidation(sheet: string, dv: DataValidation): { error: string | null }

/**
* AddDataValidation provides the method to ignored error for a range of
* cells.
* @param sheet The worksheet name
* @param rangeRef The top-left and right-bottom cell range reference
* @param ignoredErrorsType The enumeration value of ignored errors type
*/
AddIgnoredErrors(sheet: string, rangeRef: string, ignoredErrorsType: IgnoredErrorsType): { error: string | null }

/**
* AddPictureFromBytes provides the method to add picture in a sheet by given
* picture format set (such as offset, scale, aspect ratio setting and print
Expand Down Expand Up @@ -2000,6 +2024,14 @@ declare module 'excelize-wasm' {
*/
AddTable(sheet: string, opts: TableOptions): { error: string | null }

/**
* AddVBAProject provides the method to add vbaProject.bin file which
* contains functions and/or macros. The file extension should be XLSM or
* XLTM.
* @param file The contents buffer of the vbaProject.bin file
*/
AddVBAProject(file: Uint8Array): { error: string | null }

/**
* AutoFilter provides the method to add auto filter in a worksheet by
* given worksheet name, range reference and settings. An auto filter in
Expand Down Expand Up @@ -2970,8 +3002,8 @@ declare module 'excelize-wasm' {

/**
* SetCellRichText provides a function to set cell with rich text by given
* worksheet. For example, set rich text on the A1 cell of the worksheet
* named Sheet1:
* worksheet name, cell reference and rich text runs. For example, set rich
* text on the A1 cell of the worksheet named Sheet1:
*
* ```typescript
* const { init } = require('excelize-wasm');
Expand Down Expand Up @@ -3858,5 +3890,14 @@ declare module 'excelize-wasm' {
HeaderFooterImagePositionLeft: typeof HeaderFooterImagePositionType.HeaderFooterImagePositionLeft;
HeaderFooterImagePositionCenter: typeof HeaderFooterImagePositionType.HeaderFooterImagePositionCenter;
HeaderFooterImagePositionRight: typeof HeaderFooterImagePositionType.HeaderFooterImagePositionRight;
IgnoredErrorsEvalError: typeof IgnoredErrorsType.IgnoredErrorsEvalError;
IgnoredErrorsTwoDigitTextYear: typeof IgnoredErrorsType.IgnoredErrorsTwoDigitTextYear;
IgnoredErrorsNumberStoredAsText: typeof IgnoredErrorsType.IgnoredErrorsNumberStoredAsText;
IgnoredErrorsFormula: typeof IgnoredErrorsType.IgnoredErrorsFormula;
IgnoredErrorsFormulaRange: typeof IgnoredErrorsType.IgnoredErrorsFormulaRange;
IgnoredErrorsUnlockedFormula: typeof IgnoredErrorsType.IgnoredErrorsUnlockedFormula;
IgnoredErrorsEmptyCellReference: typeof IgnoredErrorsType.IgnoredErrorsEmptyCellReference;
IgnoredErrorsListDataValidation: typeof IgnoredErrorsType.IgnoredErrorsListDataValidation;
IgnoredErrorsCalculatedColumn: typeof IgnoredErrorsType.IgnoredErrorsCalculatedColumn;
}>;
}

0 comments on commit e920d00

Please sign in to comment.