Skip to content

Commit

Permalink
New functions GetMergeCells, GetCellValue, GetStartAxis and GetEndAxi…
Browse files Browse the repository at this point in the history
…s has been added (#28)

- Update the unit tests and documentation

Co-authored-by: ZhaoYiran <[email protected]>
  • Loading branch information
LogikMeister and ZhaoYiran authored Jul 1, 2024
1 parent cf27ddf commit 5dea100
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
45 changes: 45 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ func regInteropFunc(f *excelize.File, fn map[string]interface{}) interface{} {
"GetDocProps": GetDocProps(f),
"GetFormControls": GetFormControls(f),
"GetHeaderFooter": GetHeaderFooter(f),
"GetMergeCells": GetMergeCells(f),
"GetPageLayout": GetPageLayout(f),
"GetPageMargins": GetPageMargins(f),
"GetPanes": GetPanes(f),
Expand Down Expand Up @@ -414,6 +415,24 @@ func regInteropFunc(f *excelize.File, fn map[string]interface{}) interface{} {
return js.ValueOf(fn)
}

// regMergeCellFunc register functions that implemented MergeCell interface.
func regMergeCellFunc(mergeCell *excelize.MergeCell, fn map[string]interface{}) interface{} {
for name, impl := range map[string]func(this js.Value, args []js.Value) interface{}{
"GetCellValue": func(this js.Value, args []js.Value) interface{} {
return js.ValueOf(mergeCell.GetCellValue())
},
"GetStartAxis": func(this js.Value, args []js.Value) interface{} {
return js.ValueOf(mergeCell.GetStartAxis())
},
"GetEndAxis": func(this js.Value, args []js.Value) interface{} {
return js.ValueOf(mergeCell.GetEndAxis())
},
} {
fn[name] = js.FuncOf(impl)
}
return js.ValueOf(fn)
}

// inTypeSlice provides a method to check if an element is present in an
// JavaScript type value array, and return the index of its location,
// otherwise return -1.
Expand Down Expand Up @@ -2061,6 +2080,32 @@ func GetHeaderFooter(f *excelize.File) func(this js.Value, args []js.Value) inte
}
}

// GetMergeCells provides a function to get all merged cells from a specific
// worksheet.
func GetMergeCells(f *excelize.File) func(this js.Value, args []js.Value) interface{} {
return func(this js.Value, args []js.Value) interface{} {
ret := map[string]interface{}{"mergeCells": []interface{}{}, "error": nil}
if err := prepareArgs(args, []argsRule{
{types: []js.Type{js.TypeString}},
}); err != nil {
ret["error"] = err.Error()
return js.ValueOf(ret)
}
mergeCells, err := f.GetMergeCells(args[0].String())
if err != nil {
ret["error"] = err.Error()
return js.ValueOf(ret)
}
fn := map[string]interface{}{"error": nil}
result := make([]interface{}, len(mergeCells))
for i, mergeCell := range mergeCells {
result[i] = regMergeCellFunc(&mergeCell, fn)
}
ret["mergeCells"] = js.ValueOf(result)
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
29 changes: 29 additions & 0 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,35 @@ func TestGetDefaultFont(t *testing.T) {
assert.EqualError(t, errArgNum, ret.Get("error").String())
}

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

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

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

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

mergeCell := ret.Get("mergeCells").Index(0)
assert.Equal(t, "value", mergeCell.Call("GetCellValue").String())
assert.Equal(t, "A1", mergeCell.Call("GetStartAxis").String())
assert.Equal(t, "C3", mergeCell.Call("GetEndAxis").String())

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

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

ret = f.(js.Value).Call("GetMergeCells", js.ValueOf("SheetN"))
assert.Equal(t, "sheet SheetN does not exist", ret.Get("error").String())
assert.Equal(t, 0, ret.Get("mergeCells").Length())
}

func TestGetRowHeight(t *testing.T) {
f := NewFile(js.Value{}, []js.Value{})
assert.True(t, f.(js.Value).Get("error").IsNull())
Expand Down
16 changes: 16 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,15 @@ declare module 'excelize-wasm' {
FirstFooter?: string;
};

/**
* MergeCell define a merged cell data.
*/
export interface MergeCell {
GetCellValue: () => string;
GetStartAxis: () => string;
GetEndAxis: () => string;
};

/**
* PageLayoutOptions directly maps the settings of page layout.
*/
Expand Down Expand Up @@ -2141,6 +2150,13 @@ declare module 'excelize-wasm' {
*/
GetHeaderFooter(sheet: string): { opts: HeaderFooterOptions, error: string | null }

/**
* GetMergeCells provides a function to get all merged cells from a specific
* worksheet.
* @param sheet The worksheet name
*/
GetMergeCells(sheet: string): { mergeCells: MergeCell[], error: string | null }

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

0 comments on commit 5dea100

Please sign in to comment.