Skip to content

Commit

Permalink
New functions GetTables and DeleteTable has been added
Browse files Browse the repository at this point in the history
  • Loading branch information
xuri committed Sep 4, 2023
1 parent 62fa747 commit 260c577
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ jobs:
cp src/index.d.ts ./dist
- name: NPM Publish
uses: JS-DevTools/npm-publish@v1
uses: JS-DevTools/npm-publish@v2
with:
token: ${{secrets.NPM_TOKEN}}
package: ./dist/package.json
47 changes: 47 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ func regInteropFunc(f *excelize.File, fn map[string]interface{}) interface{} {
"DeleteFormControl": DeleteFormControl(f),
"DeletePicture": DeletePicture(f),
"DeleteSheet": DeleteSheet(f),
"DeleteTable": DeleteTable(f),
"DuplicateRow": DuplicateRow(f),
"DuplicateRowTo": DuplicateRowTo(f),
"GetActiveSheetIndex": GetActiveSheetIndex(f),
Expand Down Expand Up @@ -327,6 +328,7 @@ func regInteropFunc(f *excelize.File, fn map[string]interface{}) interface{} {
"GetSheetView": GetSheetView(f),
"GetSheetVisible": GetSheetVisible(f),
"GetStyle": GetStyle(f),
"GetTables": GetTables(f),
"GetWorkbookProps": GetWorkbookProps(f),
"GroupSheets": GroupSheets(f),
"InsertCols": InsertCols(f),
Expand Down Expand Up @@ -1468,6 +1470,23 @@ func DeleteSheet(f *excelize.File) func(this js.Value, args []js.Value) interfac
}
}

// DeleteTable provides the method to delete table by given table name.
func DeleteTable(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}},
}); err != nil {
ret["error"] = err.Error()
return js.ValueOf(ret)
}
if err := f.DeleteTable(args[0].String()); err != nil {
ret["error"] = err.Error()
}
return js.ValueOf(ret)
}
}

// DuplicateRow inserts a copy of specified row (by its Excel row number)
// below. Use this method with caution, which will affect changes in
// references such as formulas, charts, and so on. If there is any referenced
Expand Down Expand Up @@ -2294,6 +2313,34 @@ func GetStyle(f *excelize.File) func(this js.Value, args []js.Value) interface{}
}
}

// GetTables provides the method to get all tables in a worksheet by given
// worksheet name.
func GetTables(f *excelize.File) func(this js.Value, args []js.Value) interface{} {
return func(this js.Value, args []js.Value) interface{} {
ret := map[string]interface{}{"tables": []interface{}{}, "error": nil}
if err := prepareArgs(args, []argsRule{
{types: []js.Type{js.TypeString}},
}); err != nil {
ret["error"] = err.Error()
return js.ValueOf(ret)
}
tables, err := f.GetTables(args[0].String())
if err != nil {
ret["error"] = err.Error()
return js.ValueOf(ret)
}
for _, tbl := range tables {
if jsVal, err := goValueToJS(reflect.ValueOf(tbl),
reflect.TypeOf(excelize.Table{})); err == nil {
x := ret["tables"].([]interface{})
x = append(x, jsVal)
ret["tables"] = x
}
}
return js.ValueOf(ret)
}
}

// GetWorkbookProps provides a function to gets workbook properties.
func GetWorkbookProps(f *excelize.File) func(this js.Value, args []js.Value) interface{} {
return func(this js.Value, args []js.Value) interface{} {
Expand Down
28 changes: 27 additions & 1 deletion cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,13 +666,21 @@ func TestAddSparkline(t *testing.T) {
assert.Equal(t, "sheet SheetN does not exist", ret.Get("error").String())
}

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

ret := f.(js.Value).Call("AddTable", js.ValueOf("Sheet1"), js.ValueOf(map[string]interface{}{"Range": "B26:A21"}))
assert.True(t, ret.Get("error").IsNull())

ret = f.(js.Value).Call("GetTables", js.ValueOf("Sheet1"))
assert.True(t, ret.Get("error").IsNull())
assert.Equal(t, 1, ret.Get("tables").Length())
assert.Equal(t, "A21:B26", ret.Get("tables").Index(0).Get("Range").String())

ret = f.(js.Value).Call("DeleteTable", js.ValueOf("Table1"))
assert.True(t, ret.Get("error").IsNull())

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

Expand All @@ -681,6 +689,24 @@ func TestAddTable(t *testing.T) {

ret = f.(js.Value).Call("AddTable", js.ValueOf("SheetN"), js.ValueOf(map[string]interface{}{"Range": "B26:A21"}))
assert.Equal(t, "sheet SheetN does not exist", ret.Get("error").String())

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

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

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

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

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

ret = f.(js.Value).Call("DeleteTable", js.ValueOf("X"))
assert.Equal(t, "table X does not exist", ret.Get("error").String())
}

func TestAutoFilter(t *testing.T) {
Expand Down
13 changes: 13 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,12 @@ declare module 'excelize-wasm' {
*/
DeleteSheet(sheet: string): { error: string | null }

/**
* DeleteTable provides the method to delete table by given table name.
* @param name The table name
*/
DeleteTable(name: string): { error: string | null }

/**
* DuplicateRow inserts a copy of specified row (by its Excel row number)
* below. Use this method with caution, which will affect changes in
Expand Down Expand Up @@ -1536,6 +1542,13 @@ declare module 'excelize-wasm' {
*/
GetStyle(styleID: number): { style: Style, error: string | null }

/**
* GetTables provides the method to get all tables in a worksheet by given
* worksheet name.
* @param sheet The worksheet name
*/
GetTables(sheet: string): { tables: TableOptions[], error: string | null }

/**
* GetWorkbookProps provides a function to gets workbook properties.
*/
Expand Down

0 comments on commit 260c577

Please sign in to comment.