浏览代码

Support to adjust print scaling of the worksheet

xuri 4 年之前
父节点
当前提交
054bb9f061
共有 3 个文件被更改,包括 29 次插入2 次删除
  1. 20 0
      sheet.go
  2. 8 1
      sheet_test.go
  3. 1 1
      xmlWorksheet.go

+ 20 - 0
sheet.go

@@ -1144,6 +1144,10 @@ type (
 	FitToHeight int
 	// 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
+	// overridden when fitToWidth and/or fitToHeight are in use.
+	PageLayoutScale uint
 )
 
 const (
@@ -1215,6 +1219,22 @@ func (p *FitToWidth) getPageLayout(ps *xlsxPageSetUp) {
 	*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.
 //
 // Available options:

+ 8 - 1
sheet_test.go

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

+ 1 - 1
xmlWorksheet.go

@@ -121,7 +121,7 @@ type xlsxPageSetUp struct {
 	PaperHeight        string   `xml:"paperHeight,attr,omitempty"`
 	PaperSize          int      `xml:"paperSize,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"`
 	UsePrinterDefaults bool     `xml:"usePrinterDefaults,attr,omitempty"`
 	VerticalDPI        int      `xml:"verticalDpi,attr,omitempty"`