返回

Go-Excelize API源码阅读(十三)—— GetSheetVisible、SetSheetFormatPr

后端

前言

在前面的文章中,我们已经学习了如何使用Go-Excelize API来创建和修改工作表。在本文中,我们将继续学习如何使用GetSheetVisible和SetSheetFormatPr函数来操作工作表的显示属性和格式属性。

GetSheetVisible函数

GetSheetVisible函数用于获取工作表的显示属性。工作表的显示属性包括是否可见、是否隐藏和是否非常隐藏。

func (f *File) GetSheetVisible(sheet string) SheetVisibility
  • sheet: 工作表名称。

返回值:

  • SheetVisibility: 工作表的显示属性。

示例:

package main

import (
	"fmt"

	"github.com/360EntSecGroup-Skylar/excelize"
)

func main() {
	f, err := excelize.OpenFile("book1.xlsx")
	if err != nil {
		fmt.Println(err)
		return
	}

	visible := f.GetSheetVisible("Sheet1")
	fmt.Println(visible) // SheetVisibilityVisible
}

输出:

SheetVisibilityVisible

SetSheetFormatPr函数

SetSheetFormatPr函数用于设置工作表的格式属性。工作表的格式属性包括页面布局、页边距、打印标题和缩放比例等。

func (f *File) SetSheetFormatPr(sheet string, format *xl.SheetFormatPr)
  • sheet: 工作表名称。
  • format: 工作表的格式属性。

示例:

package main

import (
	"fmt"

	"github.com/360EntSecGroup-Skylar/excelize"
)

func main() {
	f, err := excelize.OpenFile("book1.xlsx")
	if err != nil {
		fmt.Println(err)
		return
	}

	format := &xl.SheetFormatPr{
		DefaultRowHeight:    15,
		DefaultColWidth:     10,
		ZeroHeight:           true,
		OutlineLevelCol:     0,
		OutlineLevelRow:     0,
		FitToPage:            true,
		Orientation:          xl.OrientationLandscape,
		UsePrinterDefaults:   false,
		NoOrientation:        false,
		NoSummaryBelow:      false,
		NoSummaryRight:      false,
		NoHeaders:            false,
		AlignColWithMargins:  true,
		AlignRowWithMargins:  true,
		PrintErrors:          true,
		ErrorStyle:           xl.PrintErrorDisplayAsBlank,
		AutoPageBreaks:      true,
		HCenter:             true,
		VCenter:             true,
		PrintArea:            "",
		ShowRowColHeaders:   true,
		ShowZeros:            true,
		PageOrder:            xl.PageOrderDownThenOver,
		PageBreakZoom:        100,
		Selected:             false,
		DisplayRightToLeft:   false,
		ActivePane:           "",
		ProtectObjects:       false,
		ProtectScenarios:     false,
		ShowFormulas:         false,
		ShowOutlineSymbols:  false,
		NoSummary:            false,
		Draft:                false,
		PrintComments:        xl.PrintCommentAtEnd,
		ShowCellComments:     true,
		ShowPrintComments:   true,
		ShowPivotChartFilter: true,
		AllLevelsCollapsed:   false,
		ShowDropDowns:        true,
	}

	f.SetSheetFormatPr("Sheet1", format)

	if err := f.Save(); err != nil {
		fmt.Println(err)
	}
}

总结

在本文中,我们学习了如何使用GetSheetVisible和SetSheetFormatPr函数来操作工作表的显示属性和格式属性。这些函数可以帮助我们更好地控制工作表的外观和打印效果。