Skip to content

Commit

Permalink
New function AddHeaderFooterImage has been added
Browse files Browse the repository at this point in the history
- The SetPageLayout function support set page order of page layout
- Add new exported HeaderFooterImagePositionType enumeration
- Add new exported HeaderFooterImageOptions data type
- Add new fields PageOrder for PageLayoutOptions
- Upgrade the Excelize library version
- Update the unit tests and documentation
  • Loading branch information
xuri committed Nov 10, 2024
1 parent 8eed580 commit ec50088
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 13 deletions.
8 changes: 4 additions & 4 deletions cmd/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/stretchr/testify v1.8.4
github.com/xuri/excelize/v2 v2.9.1-0.20241025005259-0d5d1c53b2bd
github.com/xuri/excelize/v2 v2.9.1-0.20241109103642-30d3561d0e9b
golang.org/x/image v0.21.0
)

Expand All @@ -16,8 +16,8 @@ require (
github.com/richardlehane/msoleps v1.0.4 // indirect
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d // indirect
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/net v0.31.0 // indirect
golang.org/x/text v0.20.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
16 changes: 8 additions & 8 deletions cmd/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d h1:llb0neMWDQe87IzJLS4Ci7psK/lVsjIS2otl+1WyRyY=
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
github.com/xuri/excelize/v2 v2.9.1-0.20241025005259-0d5d1c53b2bd h1:MI1Md1guoYC7X9UvvR9PUmO4HMZQIsL62I8TnxUoq9s=
github.com/xuri/excelize/v2 v2.9.1-0.20241025005259-0d5d1c53b2bd/go.mod h1:uqey4QBZ9gdMeWApPLdhm9x+9o2lq4iVmjiLfBS5hdE=
github.com/xuri/excelize/v2 v2.9.1-0.20241109103642-30d3561d0e9b h1:p8MMpT62oX0OiH5mH1pgn8eqmJEQ8OFzhNWSLsvycrg=
github.com/xuri/excelize/v2 v2.9.1-0.20241109103642-30d3561d0e9b/go.mod h1:hqauNt6IfU9wsGCZy/g+pWJMb4tk7KmrgRj8qHDwox0=
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 h1:hPVCafDV85blFTabnqKgNhDCkJX25eik94Si9cTER4A=
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ=
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
golang.org/x/image v0.21.0 h1:c5qV36ajHpdj4Qi0GnE0jUc/yuo33OLFaa0d+crTD5s=
golang.org/x/image v0.21.0/go.mod h1:vUbsLavqK/W303ZroQQVKQ+Af3Yl6Uz1Ppu5J/cLz78=
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo=
golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
35 changes: 35 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ func regConstants() {
"ChartTickLabelHigh": int(excelize.ChartTickLabelHigh),
"ChartTickLabelLow": int(excelize.ChartTickLabelLow),
"ChartTickLabelNone": int(excelize.ChartTickLabelNone),
// HeaderFooterImagePositionType enumeration
"HeaderFooterImagePositionLeft": int(excelize.HeaderFooterImagePositionLeft),
"HeaderFooterImagePositionCenter": int(excelize.HeaderFooterImagePositionCenter),
"HeaderFooterImagePositionRight": int(excelize.HeaderFooterImagePositionRight),
// PictureInsertType enumeration
"PictureInsertTypePlaceOverCells": int(excelize.PictureInsertTypePlaceOverCells),
"PictureInsertTypePlaceInCell": int(excelize.PictureInsertTypePlaceInCell),
Expand All @@ -309,6 +313,7 @@ func regInteropFunc(f *excelize.File, fn map[string]interface{}) interface{} {
"AddComment": AddComment(f),
"AddDataValidation": AddDataValidation(f),
"AddFormControl": AddFormControl(f),
"AddHeaderFooterImage": AddHeaderFooterImage(f),
"AddPictureFromBytes": AddPictureFromBytes(f),
"AddPivotTable": AddPivotTable(f),
"AddShape": AddShape(f),
Expand Down Expand Up @@ -1162,6 +1167,36 @@ func AddFormControl(f *excelize.File) func(this js.Value, args []js.Value) inter
}
}

// AddHeaderFooterImage provides a mechanism to set the graphics that can be
// referenced in the header and footer definitions via &G, supported image
// types: EMF, EMZ, GIF, JPEG, JPG, PNG, SVG, TIF, TIFF, WMF, and WMZ.
//
// The extension should be provided with a "." in front, e.g. ".png".
// The width and height should have units in them, e.g. "100pt".
func AddHeaderFooterImage(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}},
{types: []js.Type{js.TypeObject}},
}); err != nil {
ret["error"] = err.Error()
return js.ValueOf(ret)
}
var opts excelize.HeaderFooterImageOptions
goVal, err := jsValueToGo(args[1], reflect.TypeOf(excelize.HeaderFooterImageOptions{}))
if err != nil {
ret["error"] = err.Error()
return js.ValueOf(ret)
}
opts = goVal.Elem().Interface().(excelize.HeaderFooterImageOptions)
if err := f.AddHeaderFooterImage(args[0].String(), &opts); err != nil {
ret["error"] = err.Error()
}
return js.ValueOf(ret)
}
}

// AddPictureFromBytes provides the method to add picture in a sheet by given
// picture format set (such as offset, scale, aspect ratio setting and print
// settings), file base name, extension name and file bytes.
Expand Down
44 changes: 44 additions & 0 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,49 @@ func TestFormControl(t *testing.T) {
assert.Equal(t, "sheet SheetN does not exist", ret.Get("error").String())
}

func TestAddHeaderFooterImage(t *testing.T) {
buf, err := os.ReadFile(filepath.Join("..", "chart.png"))
assert.NoError(t, err)

uint8Array := js.Global().Get("Uint8Array").New(js.ValueOf(len(buf)))
for k, v := range buf {
uint8Array.SetIndex(k, v)
}

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

opts := js.ValueOf(map[string]interface{}{
"File": js.ValueOf(uint8Array),
"IsFooter": true,
"FirstPage": true,
"Extension": ".png",
"Width": "50pt",
"Height": "32pt",
})
ret := f.(js.Value).Call("AddHeaderFooterImage", js.ValueOf("Sheet1"), opts)
assert.True(t, ret.Get("error").IsNull(), ret.Get("error").String())

ret = f.(js.Value).Call("AddHeaderFooterImage", js.ValueOf("Sheet1"),
js.ValueOf(map[string]interface{}{"File": js.ValueOf(uint8Array), "Extension": "png"}),
)
assert.Equal(t, "unsupported image extension", ret.Get("error").String())

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

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

ret = f.(js.Value).Call("AddHeaderFooterImage", js.ValueOf("Sheet1"),
js.ValueOf(map[string]interface{}{"Extension": true}),
)
assert.EqualError(t, errArgType, ret.Get("error").String())

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

func TestAddPictureFromBytes(t *testing.T) {
buf, err := os.ReadFile(filepath.Join("..", "chart.png"))
assert.NoError(t, err)
Expand Down Expand Up @@ -2241,6 +2284,7 @@ func TestPageLayout(t *testing.T) {
"FitToHeight": 2,
"FitToWidth": 2,
"BlackAndWhite": true,
"PageOrder": "overThenDown",
}),
)
assert.True(t, ret.Get("error").IsNull())
Expand Down
45 changes: 44 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,30 @@ declare module 'excelize-wasm' {
DataLabelPosition?: ChartDataLabelPositionType;
};

/**
* HeaderFooterImagePositionType is the type of header and footer image
* position.
*/
export enum HeaderFooterImagePositionType {
HeaderFooterImagePositionLeft,
HeaderFooterImagePositionCenter,
HeaderFooterImagePositionRight,
}

/**
* HeaderFooterImageOptions defines the settings for an image to be accessible
* from the worksheet header and footer options.
*/
export type HeaderFooterImageOptions = {
Position?: HeaderFooterImagePositionType;
File: Uint8Array;
IsFooter?: boolean;
FirstPage?: boolean;
Extension: string;
Width?: string;
Height?: string;
};

/**
* PivotTableOptions directly maps the format settings of the pivot table.
*
Expand Down Expand Up @@ -878,6 +902,9 @@ declare module 'excelize-wasm' {
FitToWidth?: number;
// BlackAndWhite specified print black and white.
BlackAndWhite?: boolean;
// PageOrder specifies the ordering of multiple pages. Values
// accepted: overThenDown, downThenOver
PageOrder?: string;
};

/**
Expand Down Expand Up @@ -1794,6 +1821,19 @@ declare module 'excelize-wasm' {
*/
AddFormControl(sheet: string, opts: FormControl): { error: string | null }

/**
* AddHeaderFooterImage provides a mechanism to set the graphics that can be
* referenced in the header and footer definitions via &G, supported image
* types: EMF, EMZ, GIF, JPEG, JPG, PNG, SVG, TIF, TIFF, WMF, and WMZ.
*
* The extension should be provided with a "." in front, e.g. ".png".
* The width and height should have units in them, e.g. "100pt".
*
* @param sheet The worksheet name
* @param opts The header footer image options
*/
AddHeaderFooterImage(sheet: string, opts: HeaderFooterImageOptions): { error: string | null }

/**
* AddPivotTable provides the method to add pivot table by given pivot
* table options. Note that the same fields can not in Columns, Rows and
Expand Down Expand Up @@ -3249,7 +3289,7 @@ declare module 'excelize-wasm' {
* |
* &F | Current workbook's file name
* |
* &G | Drawing object as background (Not support currently)
* &G | Drawing object as background (Use AddHeaderFooterImage)
* |
* &H | Shadow text format
* |
Expand Down Expand Up @@ -3767,5 +3807,8 @@ declare module 'excelize-wasm' {
ChartTickLabelHigh: typeof ChartTickLabelPositionType.ChartTickLabelHigh;
ChartTickLabelLow: typeof ChartTickLabelPositionType.ChartTickLabelLow;
ChartTickLabelNone: typeof ChartTickLabelPositionType.ChartTickLabelNone;
HeaderFooterImagePositionLeft: typeof HeaderFooterImagePositionType.HeaderFooterImagePositionLeft;
HeaderFooterImagePositionCenter: typeof HeaderFooterImagePositionType.HeaderFooterImagePositionCenter;
HeaderFooterImagePositionRight: typeof HeaderFooterImagePositionType.HeaderFooterImagePositionRight;
}>;
}

0 comments on commit ec50088

Please sign in to comment.