Skip to content

Commit

Permalink
This closes xuri#14, new function GetStyle has been added
Browse files Browse the repository at this point in the history
  • Loading branch information
lidp20 committed Aug 31, 2023
1 parent 8d38d2b commit 209ba33
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
15 changes: 8 additions & 7 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
name: Publish
on:
push:
branches: [ main ]
branches:
- main
jobs:

publish:
strategy:
matrix:
Expand Down Expand Up @@ -64,9 +66,8 @@ jobs:
cp src/package.json ./dist
cp src/index.d.ts ./dist
- name: NPM Publish
uses: JS-DevTools/npm-publish@v2
with:
token: ${{secrets.NPM_TOKEN}}
package: ./dist/package.json
strategy: upgrade
- name: NPM Publish
uses: JS-DevTools/npm-publish@v1
with:
token: ${{secrets.NPM_TOKEN}}
package: ./dist/package.json
24 changes: 24 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ func regInteropFunc(f *excelize.File, fn map[string]interface{}) interface{} {
"GetSheetProps": GetSheetProps(f),
"GetSheetView": GetSheetView(f),
"GetSheetVisible": GetSheetVisible(f),
"GetStyle": GetStyle(f),
"GetWorkbookProps": GetWorkbookProps(f),
"GroupSheets": GroupSheets(f),
"InsertCols": InsertCols(f),
Expand Down Expand Up @@ -2270,6 +2271,29 @@ func GetSheetVisible(f *excelize.File) func(this js.Value, args []js.Value) inte
}
}

// GetStyle provides a function to get style definition by given style index.
func GetStyle(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.GetStyle(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)
}
}

// 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
17 changes: 16 additions & 1 deletion cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,7 @@ func TestNewSheet(t *testing.T) {
assert.Equal(t, 0, ret.Get("index").Int())
}

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

Expand Down Expand Up @@ -1401,6 +1401,12 @@ func TestNewStyle(t *testing.T) {
assert.True(t, ret.Get("error").IsNull(), ret.Get("error").String())
assert.Equal(t, 1, ret.Get("style").Int())

ret = f.(js.Value).Call("GetStyle", ret.Get("style"))
assert.True(t, ret.Get("error").IsNull())
assert.Equal(t, "0.00", ret.Get("style").Get("CustomNumFmt").String())
assert.True(t, ret.Get("style").Get("Font").Get("Bold").Bool())
assert.Equal(t, "single", ret.Get("style").Get("Font").Get("Underline").String())

for _, arg := range []map[string]interface{}{
{"NumFmt": "1"},
{"DecimalPlaces": "2"},
Expand Down Expand Up @@ -1453,6 +1459,15 @@ func TestNewStyle(t *testing.T) {

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

ret = f.(js.Value).Call("GetStyle", js.ValueOf(-1))
assert.Equal(t, "invalid style ID -1", ret.Get("error").String())

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

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

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

/**
* GetStyle provides a function to get style definition by given style index.
* @param styleID The style ID
*/
GetStyle(styleID: number): { style: Style, error: string | null }

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

0 comments on commit 209ba33

Please sign in to comment.