support to set print black and white and specified the first printed page number

This commit is contained in:
xuri 2021-01-17 01:06:08 +08:00
parent 054bb9f061
commit b260485f29
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
3 changed files with 76 additions and 20 deletions

View File

@ -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:
//

View File

@ -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)},

View File

@ -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