Skip to content

Commit

Permalink
support to set print black and white and specified the first printed …
Browse files Browse the repository at this point in the history
…page number
  • Loading branch information
xuri committed Jan 16, 2021
1 parent 054bb9f commit b260485
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 20 deletions.
56 changes: 51 additions & 5 deletions sheet.go
Expand Up @@ -1135,14 +1135,19 @@ type PageLayoutOptionPtr interface {
}

type (
// BlackAndWhite specified print black and white.
BlackAndWhite bool
// FirstPageNumber specified first printed page number. If no value is
// specified, then 'automatic' is assumed.
FirstPageNumber uint
// PageLayoutOrientation defines the orientation of page layout for a
// worksheet.
PageLayoutOrientation string
// PageLayoutPaperSize defines the paper size of the worksheet
// PageLayoutPaperSize defines the paper size of the worksheet.
PageLayoutPaperSize int
// FitToHeight specified number of vertical pages to fit on
// FitToHeight specified number of vertical pages to fit on.
FitToHeight int
// FitToWidth specified number of horizontal pages to fit on
// FitToWidth specified number of horizontal pages to fit on.
FitToWidth int
// PageLayoutScale defines the print scaling. This attribute is restricted
// to values ranging from 10 (10%) to 400 (400%). This setting is
Expand All @@ -1157,6 +1162,41 @@ const (
OrientationLandscape = "landscape"
)

// setPageLayout provides a method to set the print black and white for the
// worksheet.
func (p BlackAndWhite) setPageLayout(ps *xlsxPageSetUp) {
ps.BlackAndWhite = bool(p)
}

// getPageLayout provides a method to get the print black and white for the
// worksheet.
func (p *BlackAndWhite) getPageLayout(ps *xlsxPageSetUp) {
if ps == nil {
*p = false
return
}
*p = BlackAndWhite(ps.BlackAndWhite)
}

// setPageLayout provides a method to set the first printed page number for
// the worksheet.
func (p FirstPageNumber) setPageLayout(ps *xlsxPageSetUp) {
if 0 < uint(p) {
ps.FirstPageNumber = uint(p)
ps.UseFirstPageNumber = true
}
}

// getPageLayout provides a method to get the first printed page number for
// the worksheet.
func (p *FirstPageNumber) getPageLayout(ps *xlsxPageSetUp) {
if ps == nil || ps.FirstPageNumber == 0 || !ps.UseFirstPageNumber {
*p = 1
return
}
*p = FirstPageNumber(ps.FirstPageNumber)
}

// setPageLayout provides a method to set the orientation for the worksheet.
func (o PageLayoutOrientation) setPageLayout(ps *xlsxPageSetUp) {
ps.Orientation = string(o)
Expand Down Expand Up @@ -1238,8 +1278,14 @@ func (p *PageLayoutScale) getPageLayout(ps *xlsxPageSetUp) {
// SetPageLayout provides a function to sets worksheet page layout.
//
// Available options:
// PageLayoutOrientation(string)
// PageLayoutPaperSize(int)
//
// BlackAndWhite(bool)
// FirstPageNumber(uint)
// PageLayoutOrientation(string)
// PageLayoutPaperSize(int)
// FitToHeight(int)
// FitToWidth(int)
// PageLayoutScale(uint)
//
// The following shows the paper size sorted by excelize index number:
//
Expand Down
32 changes: 21 additions & 11 deletions sheet_test.go
Expand Up @@ -13,15 +13,11 @@ import (

func ExampleFile_SetPageLayout() {
f := NewFile()

if err := f.SetPageLayout(
"Sheet1",
BlackAndWhite(true),
FirstPageNumber(2),
PageLayoutOrientation(OrientationLandscape),
); err != nil {
fmt.Println(err)
}
if err := f.SetPageLayout(
"Sheet1",
PageLayoutPaperSize(10),
FitToHeight(2),
FitToWidth(2),
Expand All @@ -35,12 +31,20 @@ func ExampleFile_SetPageLayout() {
func ExampleFile_GetPageLayout() {
f := NewFile()
var (
orientation PageLayoutOrientation
paperSize PageLayoutPaperSize
fitToHeight FitToHeight
fitToWidth FitToWidth
scale PageLayoutScale
blackAndWhite BlackAndWhite
firstPageNumber FirstPageNumber
orientation PageLayoutOrientation
paperSize PageLayoutPaperSize
fitToHeight FitToHeight
fitToWidth FitToWidth
scale PageLayoutScale
)
if err := f.GetPageLayout("Sheet1", &blackAndWhite); err != nil {
fmt.Println(err)
}
if err := f.GetPageLayout("Sheet1", &firstPageNumber); err != nil {
fmt.Println(err)
}
if err := f.GetPageLayout("Sheet1", &orientation); err != nil {
fmt.Println(err)
}
Expand All @@ -57,13 +61,17 @@ func ExampleFile_GetPageLayout() {
fmt.Println(err)
}
fmt.Println("Defaults:")
fmt.Printf("- print black and white: %t\n", blackAndWhite)
fmt.Printf("- page number for first printed page: %d\n", firstPageNumber)
fmt.Printf("- orientation: %q\n", orientation)
fmt.Printf("- paper size: %d\n", paperSize)
fmt.Printf("- fit to height: %d\n", fitToHeight)
fmt.Printf("- fit to width: %d\n", fitToWidth)
fmt.Printf("- scale: %d\n", scale)
// Output:
// Defaults:
// - print black and white: false
// - page number for first printed page: 1
// - orientation: "portrait"
// - paper size: 1
// - fit to height: 1
Expand Down Expand Up @@ -103,6 +111,8 @@ func TestPageLayoutOption(t *testing.T) {
container PageLayoutOptionPtr
nonDefault PageLayoutOption
}{
{new(BlackAndWhite), BlackAndWhite(true)},
{new(FirstPageNumber), FirstPageNumber(2)},
{new(PageLayoutOrientation), PageLayoutOrientation(OrientationLandscape)},
{new(PageLayoutPaperSize), PageLayoutPaperSize(10)},
{new(FitToHeight), FitToHeight(2)},
Expand Down
8 changes: 4 additions & 4 deletions xmlWorksheet.go
Expand Up @@ -108,13 +108,13 @@ type xlsxPageSetUp struct {
XMLName xml.Name `xml:"pageSetup"`
BlackAndWhite bool `xml:"blackAndWhite,attr,omitempty"`
CellComments string `xml:"cellComments,attr,omitempty"`
Copies int `xml:"copies,attr,omitempty"`
Copies uint `xml:"copies,attr,omitempty"`
Draft bool `xml:"draft,attr,omitempty"`
Errors string `xml:"errors,attr,omitempty"`
FirstPageNumber int `xml:"firstPageNumber,attr,omitempty"`
FirstPageNumber uint `xml:"firstPageNumber,attr,omitempty"`
FitToHeight int `xml:"fitToHeight,attr,omitempty"`
FitToWidth int `xml:"fitToWidth,attr,omitempty"`
HorizontalDPI int `xml:"horizontalDpi,attr,omitempty"`
HorizontalDPI uint `xml:"horizontalDpi,attr,omitempty"`
RID string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr,omitempty"`
Orientation string `xml:"orientation,attr,omitempty"`
PageOrder string `xml:"pageOrder,attr,omitempty"`
Expand All @@ -124,7 +124,7 @@ type xlsxPageSetUp struct {
Scale uint `xml:"scale,attr,omitempty"`
UseFirstPageNumber bool `xml:"useFirstPageNumber,attr,omitempty"`
UsePrinterDefaults bool `xml:"usePrinterDefaults,attr,omitempty"`
VerticalDPI int `xml:"verticalDpi,attr,omitempty"`
VerticalDPI uint `xml:"verticalDpi,attr,omitempty"`
}

// xlsxPrintOptions directly maps the printOptions element in the namespace
Expand Down

0 comments on commit b260485

Please sign in to comment.