Skip to content

Commit ec50088

Browse files
committed
New function AddHeaderFooterImage has been added
- 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
1 parent 8eed580 commit ec50088

File tree

5 files changed

+135
-13
lines changed

5 files changed

+135
-13
lines changed

cmd/go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.18
44

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

@@ -16,8 +16,8 @@ require (
1616
github.com/richardlehane/msoleps v1.0.4 // indirect
1717
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d // indirect
1818
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 // indirect
19-
golang.org/x/crypto v0.28.0 // indirect
20-
golang.org/x/net v0.30.0 // indirect
21-
golang.org/x/text v0.19.0 // indirect
19+
golang.org/x/crypto v0.29.0 // indirect
20+
golang.org/x/net v0.31.0 // indirect
21+
golang.org/x/text v0.20.0 // indirect
2222
gopkg.in/yaml.v3 v3.0.1 // indirect
2323
)

cmd/go.sum

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
1313
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
1414
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d h1:llb0neMWDQe87IzJLS4Ci7psK/lVsjIS2otl+1WyRyY=
1515
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
16-
github.com/xuri/excelize/v2 v2.9.1-0.20241025005259-0d5d1c53b2bd h1:MI1Md1guoYC7X9UvvR9PUmO4HMZQIsL62I8TnxUoq9s=
17-
github.com/xuri/excelize/v2 v2.9.1-0.20241025005259-0d5d1c53b2bd/go.mod h1:uqey4QBZ9gdMeWApPLdhm9x+9o2lq4iVmjiLfBS5hdE=
16+
github.com/xuri/excelize/v2 v2.9.1-0.20241109103642-30d3561d0e9b h1:p8MMpT62oX0OiH5mH1pgn8eqmJEQ8OFzhNWSLsvycrg=
17+
github.com/xuri/excelize/v2 v2.9.1-0.20241109103642-30d3561d0e9b/go.mod h1:hqauNt6IfU9wsGCZy/g+pWJMb4tk7KmrgRj8qHDwox0=
1818
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 h1:hPVCafDV85blFTabnqKgNhDCkJX25eik94Si9cTER4A=
1919
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
20-
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
21-
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
20+
golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ=
21+
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
2222
golang.org/x/image v0.21.0 h1:c5qV36ajHpdj4Qi0GnE0jUc/yuo33OLFaa0d+crTD5s=
2323
golang.org/x/image v0.21.0/go.mod h1:vUbsLavqK/W303ZroQQVKQ+Af3Yl6Uz1Ppu5J/cLz78=
24-
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
25-
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
26-
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
27-
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
24+
golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo=
25+
golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM=
26+
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
27+
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
2828
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2929
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3030
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

cmd/main.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@ func regConstants() {
292292
"ChartTickLabelHigh": int(excelize.ChartTickLabelHigh),
293293
"ChartTickLabelLow": int(excelize.ChartTickLabelLow),
294294
"ChartTickLabelNone": int(excelize.ChartTickLabelNone),
295+
// HeaderFooterImagePositionType enumeration
296+
"HeaderFooterImagePositionLeft": int(excelize.HeaderFooterImagePositionLeft),
297+
"HeaderFooterImagePositionCenter": int(excelize.HeaderFooterImagePositionCenter),
298+
"HeaderFooterImagePositionRight": int(excelize.HeaderFooterImagePositionRight),
295299
// PictureInsertType enumeration
296300
"PictureInsertTypePlaceOverCells": int(excelize.PictureInsertTypePlaceOverCells),
297301
"PictureInsertTypePlaceInCell": int(excelize.PictureInsertTypePlaceInCell),
@@ -309,6 +313,7 @@ func regInteropFunc(f *excelize.File, fn map[string]interface{}) interface{} {
309313
"AddComment": AddComment(f),
310314
"AddDataValidation": AddDataValidation(f),
311315
"AddFormControl": AddFormControl(f),
316+
"AddHeaderFooterImage": AddHeaderFooterImage(f),
312317
"AddPictureFromBytes": AddPictureFromBytes(f),
313318
"AddPivotTable": AddPivotTable(f),
314319
"AddShape": AddShape(f),
@@ -1162,6 +1167,36 @@ func AddFormControl(f *excelize.File) func(this js.Value, args []js.Value) inter
11621167
}
11631168
}
11641169

1170+
// AddHeaderFooterImage provides a mechanism to set the graphics that can be
1171+
// referenced in the header and footer definitions via &G, supported image
1172+
// types: EMF, EMZ, GIF, JPEG, JPG, PNG, SVG, TIF, TIFF, WMF, and WMZ.
1173+
//
1174+
// The extension should be provided with a "." in front, e.g. ".png".
1175+
// The width and height should have units in them, e.g. "100pt".
1176+
func AddHeaderFooterImage(f *excelize.File) func(this js.Value, args []js.Value) interface{} {
1177+
return func(this js.Value, args []js.Value) interface{} {
1178+
ret := map[string]interface{}{"error": nil}
1179+
if err := prepareArgs(args, []argsRule{
1180+
{types: []js.Type{js.TypeString}},
1181+
{types: []js.Type{js.TypeObject}},
1182+
}); err != nil {
1183+
ret["error"] = err.Error()
1184+
return js.ValueOf(ret)
1185+
}
1186+
var opts excelize.HeaderFooterImageOptions
1187+
goVal, err := jsValueToGo(args[1], reflect.TypeOf(excelize.HeaderFooterImageOptions{}))
1188+
if err != nil {
1189+
ret["error"] = err.Error()
1190+
return js.ValueOf(ret)
1191+
}
1192+
opts = goVal.Elem().Interface().(excelize.HeaderFooterImageOptions)
1193+
if err := f.AddHeaderFooterImage(args[0].String(), &opts); err != nil {
1194+
ret["error"] = err.Error()
1195+
}
1196+
return js.ValueOf(ret)
1197+
}
1198+
}
1199+
11651200
// AddPictureFromBytes provides the method to add picture in a sheet by given
11661201
// picture format set (such as offset, scale, aspect ratio setting and print
11671202
// settings), file base name, extension name and file bytes.

cmd/main_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,49 @@ func TestFormControl(t *testing.T) {
492492
assert.Equal(t, "sheet SheetN does not exist", ret.Get("error").String())
493493
}
494494

495+
func TestAddHeaderFooterImage(t *testing.T) {
496+
buf, err := os.ReadFile(filepath.Join("..", "chart.png"))
497+
assert.NoError(t, err)
498+
499+
uint8Array := js.Global().Get("Uint8Array").New(js.ValueOf(len(buf)))
500+
for k, v := range buf {
501+
uint8Array.SetIndex(k, v)
502+
}
503+
504+
f := NewFile(js.Value{}, []js.Value{})
505+
assert.True(t, f.(js.Value).Get("error").IsNull())
506+
507+
opts := js.ValueOf(map[string]interface{}{
508+
"File": js.ValueOf(uint8Array),
509+
"IsFooter": true,
510+
"FirstPage": true,
511+
"Extension": ".png",
512+
"Width": "50pt",
513+
"Height": "32pt",
514+
})
515+
ret := f.(js.Value).Call("AddHeaderFooterImage", js.ValueOf("Sheet1"), opts)
516+
assert.True(t, ret.Get("error").IsNull(), ret.Get("error").String())
517+
518+
ret = f.(js.Value).Call("AddHeaderFooterImage", js.ValueOf("Sheet1"),
519+
js.ValueOf(map[string]interface{}{"File": js.ValueOf(uint8Array), "Extension": "png"}),
520+
)
521+
assert.Equal(t, "unsupported image extension", ret.Get("error").String())
522+
523+
ret = f.(js.Value).Call("AddHeaderFooterImage")
524+
assert.EqualError(t, errArgNum, ret.Get("error").String())
525+
526+
ret = f.(js.Value).Call("AddHeaderFooterImage", js.ValueOf("Sheet1"), js.ValueOf(true))
527+
assert.EqualError(t, errArgType, ret.Get("error").String())
528+
529+
ret = f.(js.Value).Call("AddHeaderFooterImage", js.ValueOf("Sheet1"),
530+
js.ValueOf(map[string]interface{}{"Extension": true}),
531+
)
532+
assert.EqualError(t, errArgType, ret.Get("error").String())
533+
534+
ret = f.(js.Value).Call("AddHeaderFooterImage", js.ValueOf("SheetN"), opts)
535+
assert.Equal(t, "sheet SheetN does not exist", ret.Get("error").String())
536+
}
537+
495538
func TestAddPictureFromBytes(t *testing.T) {
496539
buf, err := os.ReadFile(filepath.Join("..", "chart.png"))
497540
assert.NoError(t, err)
@@ -2241,6 +2284,7 @@ func TestPageLayout(t *testing.T) {
22412284
"FitToHeight": 2,
22422285
"FitToWidth": 2,
22432286
"BlackAndWhite": true,
2287+
"PageOrder": "overThenDown",
22442288
}),
22452289
)
22462290
assert.True(t, ret.Get("error").IsNull())

src/index.d.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,30 @@ declare module 'excelize-wasm' {
614614
DataLabelPosition?: ChartDataLabelPositionType;
615615
};
616616

617+
/**
618+
* HeaderFooterImagePositionType is the type of header and footer image
619+
* position.
620+
*/
621+
export enum HeaderFooterImagePositionType {
622+
HeaderFooterImagePositionLeft,
623+
HeaderFooterImagePositionCenter,
624+
HeaderFooterImagePositionRight,
625+
}
626+
627+
/**
628+
* HeaderFooterImageOptions defines the settings for an image to be accessible
629+
* from the worksheet header and footer options.
630+
*/
631+
export type HeaderFooterImageOptions = {
632+
Position?: HeaderFooterImagePositionType;
633+
File: Uint8Array;
634+
IsFooter?: boolean;
635+
FirstPage?: boolean;
636+
Extension: string;
637+
Width?: string;
638+
Height?: string;
639+
};
640+
617641
/**
618642
* PivotTableOptions directly maps the format settings of the pivot table.
619643
*
@@ -878,6 +902,9 @@ declare module 'excelize-wasm' {
878902
FitToWidth?: number;
879903
// BlackAndWhite specified print black and white.
880904
BlackAndWhite?: boolean;
905+
// PageOrder specifies the ordering of multiple pages. Values
906+
// accepted: overThenDown, downThenOver
907+
PageOrder?: string;
881908
};
882909

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

1824+
/**
1825+
* AddHeaderFooterImage provides a mechanism to set the graphics that can be
1826+
* referenced in the header and footer definitions via &G, supported image
1827+
* types: EMF, EMZ, GIF, JPEG, JPG, PNG, SVG, TIF, TIFF, WMF, and WMZ.
1828+
*
1829+
* The extension should be provided with a "." in front, e.g. ".png".
1830+
* The width and height should have units in them, e.g. "100pt".
1831+
*
1832+
* @param sheet The worksheet name
1833+
* @param opts The header footer image options
1834+
*/
1835+
AddHeaderFooterImage(sheet: string, opts: HeaderFooterImageOptions): { error: string | null }
1836+
17971837
/**
17981838
* AddPivotTable provides the method to add pivot table by given pivot
17991839
* table options. Note that the same fields can not in Columns, Rows and
@@ -3249,7 +3289,7 @@ declare module 'excelize-wasm' {
32493289
* |
32503290
* &F | Current workbook's file name
32513291
* |
3252-
* &G | Drawing object as background (Not support currently)
3292+
* &G | Drawing object as background (Use AddHeaderFooterImage)
32533293
* |
32543294
* &H | Shadow text format
32553295
* |
@@ -3767,5 +3807,8 @@ declare module 'excelize-wasm' {
37673807
ChartTickLabelHigh: typeof ChartTickLabelPositionType.ChartTickLabelHigh;
37683808
ChartTickLabelLow: typeof ChartTickLabelPositionType.ChartTickLabelLow;
37693809
ChartTickLabelNone: typeof ChartTickLabelPositionType.ChartTickLabelNone;
3810+
HeaderFooterImagePositionLeft: typeof HeaderFooterImagePositionType.HeaderFooterImagePositionLeft;
3811+
HeaderFooterImagePositionCenter: typeof HeaderFooterImagePositionType.HeaderFooterImagePositionCenter;
3812+
HeaderFooterImagePositionRight: typeof HeaderFooterImagePositionType.HeaderFooterImagePositionRight;
37703813
}>;
37713814
}

0 commit comments

Comments
 (0)