Skip to content

Commit

Permalink
New function GetFormControls has been added (xuri#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
cnmlgbgithub authored Aug 31, 2023
1 parent 42d2872 commit 8d38d2b
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 3 deletions.
49 changes: 46 additions & 3 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,53 @@ jobs:
GOOS: js
GOARCH: wasm
GO111MODULE: on

steps:
- name: Build
uses: ./.github/workflows/go

- name: Install Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}

- name: Checkout code
uses: actions/checkout@v3

- name: Get dependencies
working-directory: ./cmd
run: |
go vet ./...
- name: Test
run: |
cd cmd
GOOS=js GOARCH=wasm GO111MODULE=on go test -exec="$(go env GOROOT)/misc/wasm/go_js_wasm_exec" -v ./... -coverprofile=coverage.txt -covermode=atomic
- name: Codecov
uses: codecov/codecov-action@v3
with:
directory: ./cmd
file: coverage.txt
flags: unittests
name: codecov-umbrella

- name: Build WASM
run: |
cd cmd
GOOS=js GOARCH=wasm GO111MODULE=on CGO_ENABLED=0 go build -v -a -ldflags="-w -s" -gcflags=-trimpath="$(go env GOPATH)" -asmflags=-trimpath="$(go env GOPATH)" -o ../dist/excelize.wasm main.go
gzip -f --best ../dist/excelize.wasm
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18

- name: NPM Build
run: |
npm install
node ./node_modules/.bin/rollup -c
cp LICENSE ./dist
cp README.md ./dist
cp src/package.json ./dist
cp src/index.d.ts ./dist
- name: NPM Publish
uses: JS-DevTools/npm-publish@v2
Expand Down
30 changes: 30 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ func regInteropFunc(f *excelize.File, fn map[string]interface{}) interface{} {
"GetDefaultFont": GetDefaultFont(f),
"GetDefinedName": GetDefinedName(f),
"GetDocProps": GetDocProps(f),
"GetFormControls": GetFormControls(f),
"GetPageLayout": GetPageLayout(f),
"GetPageMargins": GetPageMargins(f),
"GetPanes": GetPanes(f),
Expand Down Expand Up @@ -1893,6 +1894,35 @@ func GetDocProps(f *excelize.File) func(this js.Value, args []js.Value) interfac
}
}

// GetFormControls retrieves all form controls in a worksheet by a given
// worksheet name. Note that, this function does not support getting the width
// and height of the form controls currently.
func GetFormControls(f *excelize.File) func(this js.Value, args []js.Value) interface{} {
return func(this js.Value, args []js.Value) interface{} {
ret := map[string]interface{}{"formControls": []interface{}{}, "error": nil}
if err := prepareArgs(args, []argsRule{
{types: []js.Type{js.TypeString}},
}); err != nil {
ret["error"] = err.Error()
return js.ValueOf(ret)
}
formControls, err := f.GetFormControls(args[0].String())
if err != nil {
ret["error"] = err.Error()
return js.ValueOf(ret)
}
for _, formCtrl := range formControls {
if jsVal, err := goValueToJS(reflect.ValueOf(formCtrl),
reflect.TypeOf(excelize.FormControl{})); err == nil {
x := ret["formControls"].([]interface{})
x = append(x, jsVal)
ret["formControls"] = x
}
}
return js.ValueOf(ret)
}
}

// GetPageLayout provides a function to gets worksheet page layout.
func GetPageLayout(f *excelize.File) func(this js.Value, args []js.Value) interface{} {
return func(this js.Value, args []js.Value) interface{} {
Expand Down
13 changes: 13 additions & 0 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,10 @@ func TestFormControl(t *testing.T) {
}))
assert.True(t, ret.Get("error").IsNull())

ret = f.(js.Value).Call("GetFormControls", js.ValueOf("Sheet1"))
assert.True(t, ret.Get("error").IsNull())
assert.Equal(t, 1, ret.Get("formControls").Length())

ret = f.(js.Value).Call("DeleteFormControl", js.ValueOf("Sheet1"), js.ValueOf("A1"))
assert.True(t, ret.Get("error").IsNull())

Expand All @@ -467,6 +471,15 @@ func TestFormControl(t *testing.T) {
}))
assert.Equal(t, "sheet SheetN does not exist", ret.Get("error").String())

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

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

ret = f.(js.Value).Call("GetFormControls", js.ValueOf(true))
assert.Equal(t, errArgType.Error(), ret.Get("error").String())

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

Expand Down
8 changes: 8 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,14 @@ declare module 'excelize-wasm' {
*/
GetDocProps(): { props: DocProperties, error: string | null }

/**
* GetFormControls retrieves all form controls in a worksheet by a given
* worksheet name. Note that, this function does not support getting the
* width and height of the form controls currently.
* @param sheet The worksheet name
*/
GetFormControls(sheet: string): { formControls: FormControl[], error: string | null }

/**
* GetPageLayout provides a function to gets worksheet page layout.
* @param sheet The worksheet name
Expand Down

0 comments on commit 8d38d2b

Please sign in to comment.