Skip to content

Commit

Permalink
New function GetPictureCells has been added
Browse files Browse the repository at this point in the history
  • Loading branch information
xuri committed Nov 17, 2023
1 parent 9cc6455 commit 1a6a444
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
25 changes: 25 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ func regInteropFunc(f *excelize.File, fn map[string]interface{}) interface{} {
"GetPageMargins": GetPageMargins(f),
"GetPanes": GetPanes(f),
"GetPictures": GetPictures(f),
"GetPictureCells": GetPictureCells(f),
"GetPivotTables": GetPivotTables(f),
"GetRowHeight": GetRowHeight(f),
"GetRowOutlineLevel": GetRowOutlineLevel(f),
Expand Down Expand Up @@ -358,6 +359,7 @@ func regInteropFunc(f *excelize.File, fn map[string]interface{}) interface{} {
"SetCellRichText": SetCellRichText(f),
"SetCellStr": SetCellStr(f),
"SetCellStyle": SetCellStyle(f),
"SetCellUint": SetCellInt(f),
"SetCellValue": SetCellValue(f),
"SetColOutlineLevel": SetColOutlineLevel(f),
"SetColStyle": SetColStyle(f),
Expand Down Expand Up @@ -2096,6 +2098,29 @@ func GetPictures(f *excelize.File) func(this js.Value, args []js.Value) interfac
}
}

// GetPictureCells returns all picture cell references in a worksheet by a
// specific worksheet name.
func GetPictureCells(f *excelize.File) func(this js.Value, args []js.Value) interface{} {
return func(this js.Value, args []js.Value) interface{} {
ret := map[string]interface{}{"cells": js.ValueOf([]interface{}{}), "error": nil}
if err := prepareArgs(args, []argsRule{{types: []js.Type{js.TypeString}}}); err != nil {
ret["error"] = err.Error()
return js.ValueOf(ret)
}
cells, err := f.GetPictureCells(args[0].String())
if err != nil {
ret["error"] = err.Error()
return js.ValueOf(ret)
}
expected := make([]interface{}, len(cells))
for i, name := range cells {
expected[i] = name
}
ret["cells"] = expected
return js.ValueOf(ret)
}
}

// GetPivotTables returns all pivot table definitions in a worksheet by given
// worksheet name.
func GetPivotTables(f *excelize.File) func(this js.Value, args []js.Value) interface{} {
Expand Down
14 changes: 14 additions & 0 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,11 @@ func TestAddPictureFromBytes(t *testing.T) {
assert.Equal(t, 1, ret.Get("pictures").Length())
assert.Equal(t, uint8Array.Length(), ret.Get("pictures").Index(0).Get("File").Length())

ret = f.(js.Value).Call("GetPictureCells", js.ValueOf("Sheet1"))
assert.True(t, ret.Get("error").IsNull(), ret.Get("error").String())
assert.Equal(t, 1, ret.Get("cells").Length())
assert.Equal(t, "A1", ret.Get("cells").Index(0).String())

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

Expand All @@ -531,14 +536,23 @@ func TestAddPictureFromBytes(t *testing.T) {
)
assert.EqualError(t, errArgType, ret.Get("error").String())

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

ret = f.(js.Value).Call("AddPictureFromBytes", js.ValueOf("Sheet1"), js.ValueOf("A1"), js.ValueOf(map[string]interface{}{"Extension": "png", "File": uint8Array, "Format": map[string]interface{}{}}))
assert.EqualError(t, excelize.ErrImgExt, ret.Get("error").String())

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

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

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

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

func TestPivotTable(t *testing.T) {
Expand Down
49 changes: 45 additions & 4 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,8 +634,10 @@ declare module 'excelize-wasm' {
export type Comment = {
Author?: string;
AuthorID?: number;
Cell?: string;
Cell: string;
Text?: string;
Width?: number;
Height?: number;
Paragraph?: RichTextRun[];
};

Expand Down Expand Up @@ -1024,9 +1026,32 @@ declare module 'excelize-wasm' {
AddChartSheet(sheet: string, chart: Chart, combo?: Chart): { error: string | null }

/**
* AddComment provides the method to add comment in a sheet by given
* worksheet index, cell and format set (such as author and text). Note
* that the max author length is 255 and the max text length is 32512.
* AddComment provides the method to add comments in a sheet by giving the
* worksheet name, cell reference, and format set (such as author and text).
* Note that the maximum author name length is 255 and the max text length
* is 32512. For example, add a rich-text comment with a specified comments
* box size in Sheet1!A5:
*
* ```typescript
* const { error } = f.AddComment("Sheet1", {
* Cell: "A5",
* Author: "Excelize",
* Height: 40,
* Width: 180,
* Paragraph: [
* {
* Font: {
* Bold: true,
* },
* Text: "Excelize: ",
* },
* {
* Text: "This is a comment.",
* },
* ],
* })
* ```
*
* @param sheet The worksheet name
* @param comment The comment options
*/
Expand Down Expand Up @@ -1505,6 +1530,13 @@ declare module 'excelize-wasm' {
*/
GetPictures(sheet: string, cell: string): { pictures: Picture[], error: string | null }

/**
* GetPictureCells returns all picture cell references in a worksheet by a
* specific worksheet name.
* @param sheet The worksheet name
*/
GetPictureCells(sheet: string): { cells: string[], error: string | null }

/**
* GetPivotTables returns all pivot table definitions in a worksheet by
* given worksheet name.
Expand Down Expand Up @@ -2029,6 +2061,15 @@ declare module 'excelize-wasm' {
*/
SetCellInt(sheet: string, cell: string, value: number): { error: string | null }

/**
* SetCellUint provides a function to set uint type value of a cell by given
* worksheet name, cell reference and cell value.
* @param sheet The worksheet name
* @param cell The cell reference
* @param value The cell value to be write
*/
SetCellUint(sheet: string, cell: string, value: number): { error: string | null }

/**
* 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
Expand Down

0 comments on commit 1a6a444

Please sign in to comment.