Support to adjust print scaling of the worksheet

formula
xuri 4 years ago
parent a26675517e
commit 054bb9f061
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7

@ -1144,6 +1144,10 @@ type (
FitToHeight int FitToHeight int
// FitToWidth specified number of horizontal pages to fit on // FitToWidth specified number of horizontal pages to fit on
FitToWidth int FitToWidth int
// PageLayoutScale defines the print scaling. This attribute is restricted
// to values ranging from 10 (10%) to 400 (400%). This setting is
// overridden when fitToWidth and/or fitToHeight are in use.
PageLayoutScale uint
) )
const ( const (
@ -1215,6 +1219,22 @@ func (p *FitToWidth) getPageLayout(ps *xlsxPageSetUp) {
*p = FitToWidth(ps.FitToWidth) *p = FitToWidth(ps.FitToWidth)
} }
// setPageLayout provides a method to set the scale for the worksheet.
func (p PageLayoutScale) setPageLayout(ps *xlsxPageSetUp) {
if 10 <= uint(p) && uint(p) <= 400 {
ps.Scale = uint(p)
}
}
// getPageLayout provides a method to get the scale for the worksheet.
func (p *PageLayoutScale) getPageLayout(ps *xlsxPageSetUp) {
if ps == nil || ps.Scale < 10 || ps.Scale > 400 {
*p = 100
return
}
*p = PageLayoutScale(ps.Scale)
}
// SetPageLayout provides a function to sets worksheet page layout. // SetPageLayout provides a function to sets worksheet page layout.
// //
// Available options: // Available options:

@ -25,6 +25,7 @@ func ExampleFile_SetPageLayout() {
PageLayoutPaperSize(10), PageLayoutPaperSize(10),
FitToHeight(2), FitToHeight(2),
FitToWidth(2), FitToWidth(2),
PageLayoutScale(50),
); err != nil { ); err != nil {
fmt.Println(err) fmt.Println(err)
} }
@ -38,6 +39,7 @@ func ExampleFile_GetPageLayout() {
paperSize PageLayoutPaperSize paperSize PageLayoutPaperSize
fitToHeight FitToHeight fitToHeight FitToHeight
fitToWidth FitToWidth fitToWidth FitToWidth
scale PageLayoutScale
) )
if err := f.GetPageLayout("Sheet1", &orientation); err != nil { if err := f.GetPageLayout("Sheet1", &orientation); err != nil {
fmt.Println(err) fmt.Println(err)
@ -48,21 +50,25 @@ func ExampleFile_GetPageLayout() {
if err := f.GetPageLayout("Sheet1", &fitToHeight); err != nil { if err := f.GetPageLayout("Sheet1", &fitToHeight); err != nil {
fmt.Println(err) fmt.Println(err)
} }
if err := f.GetPageLayout("Sheet1", &fitToWidth); err != nil { if err := f.GetPageLayout("Sheet1", &fitToWidth); err != nil {
fmt.Println(err) fmt.Println(err)
} }
if err := f.GetPageLayout("Sheet1", &scale); err != nil {
fmt.Println(err)
}
fmt.Println("Defaults:") fmt.Println("Defaults:")
fmt.Printf("- orientation: %q\n", orientation) fmt.Printf("- orientation: %q\n", orientation)
fmt.Printf("- paper size: %d\n", paperSize) fmt.Printf("- paper size: %d\n", paperSize)
fmt.Printf("- fit to height: %d\n", fitToHeight) fmt.Printf("- fit to height: %d\n", fitToHeight)
fmt.Printf("- fit to width: %d\n", fitToWidth) fmt.Printf("- fit to width: %d\n", fitToWidth)
fmt.Printf("- scale: %d\n", scale)
// Output: // Output:
// Defaults: // Defaults:
// - orientation: "portrait" // - orientation: "portrait"
// - paper size: 1 // - paper size: 1
// - fit to height: 1 // - fit to height: 1
// - fit to width: 1 // - fit to width: 1
// - scale: 100
} }
func TestNewSheet(t *testing.T) { func TestNewSheet(t *testing.T) {
@ -101,6 +107,7 @@ func TestPageLayoutOption(t *testing.T) {
{new(PageLayoutPaperSize), PageLayoutPaperSize(10)}, {new(PageLayoutPaperSize), PageLayoutPaperSize(10)},
{new(FitToHeight), FitToHeight(2)}, {new(FitToHeight), FitToHeight(2)},
{new(FitToWidth), FitToWidth(2)}, {new(FitToWidth), FitToWidth(2)},
{new(PageLayoutScale), PageLayoutScale(50)},
} }
for i, test := range testData { for i, test := range testData {

@ -121,7 +121,7 @@ type xlsxPageSetUp struct {
PaperHeight string `xml:"paperHeight,attr,omitempty"` PaperHeight string `xml:"paperHeight,attr,omitempty"`
PaperSize int `xml:"paperSize,attr,omitempty"` PaperSize int `xml:"paperSize,attr,omitempty"`
PaperWidth string `xml:"paperWidth,attr,omitempty"` PaperWidth string `xml:"paperWidth,attr,omitempty"`
Scale int `xml:"scale,attr,omitempty"` Scale uint `xml:"scale,attr,omitempty"`
UseFirstPageNumber bool `xml:"useFirstPageNumber,attr,omitempty"` UseFirstPageNumber bool `xml:"useFirstPageNumber,attr,omitempty"`
UsePrinterDefaults bool `xml:"usePrinterDefaults,attr,omitempty"` UsePrinterDefaults bool `xml:"usePrinterDefaults,attr,omitempty"`
VerticalDPI int `xml:"verticalDpi,attr,omitempty"` VerticalDPI int `xml:"verticalDpi,attr,omitempty"`

Loading…
Cancel
Save