Skip to content

Commit d2be5cd

Browse files
authored
The SetPageLayout function support set page order of page layout (#2022)
- Add new fields PageOrder for PageLayoutOptions - Add a new exported error variable ErrPageSetupAdjustTo - An error will be return if the option value of the SetPageLayout function is invalid - Updated unit tests
1 parent b7375bc commit d2be5cd

File tree

5 files changed

+51
-6
lines changed

5 files changed

+51
-6
lines changed

errors.go

+9
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ var (
8888
// ErrOutlineLevel defined the error message on receive an invalid outline
8989
// level number.
9090
ErrOutlineLevel = errors.New("invalid outline level")
91+
// ErrPageSetupAdjustTo defined the error message for receiving a page setup
92+
// adjust to value exceeds limit.
93+
ErrPageSetupAdjustTo = errors.New("adjust to value must be between 10 and 400")
9194
// ErrParameterInvalid defined the error message on receive the invalid
9295
// parameter.
9396
ErrParameterInvalid = errors.New("parameter is invalid")
@@ -249,6 +252,12 @@ func newInvalidNameError(name string) error {
249252
return fmt.Errorf("invalid name %q, the name should be starts with a letter or underscore, can not include a space or character, and can not conflict with an existing name in the workbook", name)
250253
}
251254

255+
// newInvalidPageLayoutValueError defined the error message on receiving the invalid
256+
// page layout options value.
257+
func newInvalidPageLayoutValueError(name, value, msg string) error {
258+
return fmt.Errorf("invalid %s value %q, acceptable value should be one of %s", name, value, msg)
259+
}
260+
252261
// newInvalidRowNumberError defined the error message on receiving the invalid
253262
// row number.
254263
func newInvalidRowNumberError(row int) error {

sheet.go

+22-6
Original file line numberDiff line numberDiff line change
@@ -1609,8 +1609,7 @@ func (f *File) SetPageLayout(sheet string, opts *PageLayoutOptions) error {
16091609
if opts == nil {
16101610
return err
16111611
}
1612-
ws.setPageSetUp(opts)
1613-
return err
1612+
return ws.setPageSetUp(opts)
16141613
}
16151614

16161615
// newPageSetUp initialize page setup settings for the worksheet if which not
@@ -1622,12 +1621,15 @@ func (ws *xlsxWorksheet) newPageSetUp() {
16221621
}
16231622

16241623
// setPageSetUp set page setup settings for the worksheet by given options.
1625-
func (ws *xlsxWorksheet) setPageSetUp(opts *PageLayoutOptions) {
1624+
func (ws *xlsxWorksheet) setPageSetUp(opts *PageLayoutOptions) error {
16261625
if opts.Size != nil {
16271626
ws.newPageSetUp()
16281627
ws.PageSetUp.PaperSize = opts.Size
16291628
}
1630-
if opts.Orientation != nil && (*opts.Orientation == "portrait" || *opts.Orientation == "landscape") {
1629+
if opts.Orientation != nil {
1630+
if inStrSlice(supportedPageOrientation, *opts.Orientation, true) == -1 {
1631+
return newInvalidPageLayoutValueError("Orientation", *opts.Orientation, strings.Join(supportedPageOrientation, ", "))
1632+
}
16311633
ws.newPageSetUp()
16321634
ws.PageSetUp.Orientation = *opts.Orientation
16331635
}
@@ -1636,7 +1638,10 @@ func (ws *xlsxWorksheet) setPageSetUp(opts *PageLayoutOptions) {
16361638
ws.PageSetUp.FirstPageNumber = strconv.Itoa(int(*opts.FirstPageNumber))
16371639
ws.PageSetUp.UseFirstPageNumber = true
16381640
}
1639-
if opts.AdjustTo != nil && 10 <= *opts.AdjustTo && *opts.AdjustTo <= 400 {
1641+
if opts.AdjustTo != nil {
1642+
if *opts.AdjustTo < 10 || 400 < *opts.AdjustTo {
1643+
return ErrPageSetupAdjustTo
1644+
}
16401645
ws.newPageSetUp()
16411646
ws.PageSetUp.Scale = int(*opts.AdjustTo)
16421647
}
@@ -1652,13 +1657,21 @@ func (ws *xlsxWorksheet) setPageSetUp(opts *PageLayoutOptions) {
16521657
ws.newPageSetUp()
16531658
ws.PageSetUp.BlackAndWhite = *opts.BlackAndWhite
16541659
}
1660+
if opts.PageOrder != nil {
1661+
if inStrSlice(supportedPageOrder, *opts.PageOrder, true) == -1 {
1662+
return newInvalidPageLayoutValueError("PageOrder", *opts.PageOrder, strings.Join(supportedPageOrder, ", "))
1663+
}
1664+
ws.newPageSetUp()
1665+
ws.PageSetUp.PageOrder = *opts.PageOrder
1666+
}
1667+
return nil
16551668
}
16561669

16571670
// GetPageLayout provides a function to gets worksheet page layout.
16581671
func (f *File) GetPageLayout(sheet string) (PageLayoutOptions, error) {
16591672
opts := PageLayoutOptions{
16601673
Size: intPtr(0),
1661-
Orientation: stringPtr("portrait"),
1674+
Orientation: stringPtr(supportedPageOrientation[0]),
16621675
FirstPageNumber: uintPtr(1),
16631676
AdjustTo: uintPtr(100),
16641677
}
@@ -1686,6 +1699,9 @@ func (f *File) GetPageLayout(sheet string) (PageLayoutOptions, error) {
16861699
opts.FitToWidth = ws.PageSetUp.FitToWidth
16871700
}
16881701
opts.BlackAndWhite = boolPtr(ws.PageSetUp.BlackAndWhite)
1702+
if ws.PageSetUp.PageOrder != "" {
1703+
opts.PageOrder = stringPtr(ws.PageSetUp.PageOrder)
1704+
}
16891705
}
16901706
return opts, err
16911707
}

sheet_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ func TestSetPageLayout(t *testing.T) {
210210
FitToHeight: intPtr(2),
211211
FitToWidth: intPtr(2),
212212
BlackAndWhite: boolPtr(true),
213+
PageOrder: stringPtr("overThenDown"),
213214
}
214215
assert.NoError(t, f.SetPageLayout("Sheet1", &expected))
215216
opts, err := f.GetPageLayout("Sheet1")
@@ -219,6 +220,16 @@ func TestSetPageLayout(t *testing.T) {
219220
assert.EqualError(t, f.SetPageLayout("SheetN", nil), "sheet SheetN does not exist")
220221
// Test set page layout with invalid sheet name
221222
assert.EqualError(t, f.SetPageLayout("Sheet:1", nil), ErrSheetNameInvalid.Error())
223+
// Test set page layout with invalid parameters
224+
assert.EqualError(t, f.SetPageLayout("Sheet1", &PageLayoutOptions{
225+
AdjustTo: uintPtr(5),
226+
}), "adjust to value must be between 10 and 400")
227+
assert.EqualError(t, f.SetPageLayout("Sheet1", &PageLayoutOptions{
228+
Orientation: stringPtr("x"),
229+
}), "invalid Orientation value \"x\", acceptable value should be one of portrait, landscape")
230+
assert.EqualError(t, f.SetPageLayout("Sheet1", &PageLayoutOptions{
231+
PageOrder: stringPtr("x"),
232+
}), "invalid PageOrder value \"x\", acceptable value should be one of overThenDown, downThenOver")
222233
}
223234

224235
func TestGetPageLayout(t *testing.T) {

templates.go

+6
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,12 @@ var supportedDrawingUnderlineTypes = []string{
498498
// supportedPositioning defined supported positioning types.
499499
var supportedPositioning = []string{"absolute", "oneCell", "twoCell"}
500500

501+
// supportedPageOrientation defined supported page setup page orientation.
502+
var supportedPageOrientation = []string{"portrait", "landscape"}
503+
504+
// supportedPageOrder defined supported page setup page order.
505+
var supportedPageOrder = []string{"overThenDown", "downThenOver"}
506+
501507
// builtInDefinedNames defined built-in defined names are built with a _xlnm prefix.
502508
var builtInDefinedNames = []string{"_xlnm.Print_Area", "_xlnm.Print_Titles", "_xlnm.Criteria", "_xlnm._FilterDatabase", "_xlnm.Extract", "_xlnm.Consolidate_Area", "_xlnm.Database", "_xlnm.Sheet_Title"}
503509

xmlWorksheet.go

+3
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,9 @@ type PageLayoutOptions struct {
10061006
FitToWidth *int
10071007
// BlackAndWhite specified print black and white.
10081008
BlackAndWhite *bool
1009+
// PageOrder specifies the ordering of multiple pages. Values
1010+
// accepted: overThenDown, downThenOver
1011+
PageOrder *string
10091012
}
10101013

10111014
// ViewOptions directly maps the settings of sheet view.

0 commit comments

Comments
 (0)