summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxuri <xuri.me@gmail.com>2021-01-17 01:06:08 +0800
committerxuri <xuri.me@gmail.com>2021-01-17 01:06:08 +0800
commitb260485f29038ca8df9993edb1c021672b3df7e4 (patch)
treed0c1307a6262461d8405aa6537aa3a45a19fb72f
parent054bb9f0612ab7bd5836c3817d105a9b0c9e6059 (diff)
support to set print black and white and specified the first printed page number
-rw-r--r--sheet.go56
-rw-r--r--sheet_test.go32
-rw-r--r--xmlWorksheet.go8
3 files changed, 76 insertions, 20 deletions
diff --git a/sheet.go b/sheet.go
index 9f71de4..4e56943 100644
--- a/sheet.go
+++ b/sheet.go
@@ -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
@@ -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)
@@ -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:
//
diff --git a/sheet_test.go b/sheet_test.go
index 701d824..d68327e 100644
--- a/sheet_test.go
+++ b/sheet_test.go
@@ -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),
@@ -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)
}
@@ -57,6 +61,8 @@ 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)
@@ -64,6 +70,8 @@ func ExampleFile_GetPageLayout() {
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
@@ -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)},
diff --git a/xmlWorksheet.go b/xmlWorksheet.go
index 1f680d2..72a470f 100644
--- a/xmlWorksheet.go
+++ b/xmlWorksheet.go
@@ -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"`
@@ -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