获取工作表视图属性

  1. func (f *File) GetSheetViewOptions(sheet string, viewIndex int, opts ...SheetViewOptionPtr) error

根据给定的工作表名称、视图索引和视图参数获取工作表视图属性,viewIndex 可以是负数,如果是这样,则向后计数(-1 代表最后一个视图)。

可选视图参数类型
DefaultGridColorbool
ShowFormulasbool
ShowGridLinesbool
ShowRowColHeadersbool
ShowZerosbool
RightToLeftbool
ShowRulerbool
Viewstring
TopLeftCellstring
ZoomScalefloat64
  • 例1,获取名为 Sheet1 的工作表上最后一个视图的网格线属性设置:
  1. var showGridLines excelize.ShowGridLines
  2. err = f.GetSheetViewOptions("Sheet1", -1, &showGridLines)
  • 例2:
  1. f := NewFile()
  2. const sheet = "Sheet1"
  3. var (
  4. defaultGridColor excelize.DefaultGridColor
  5. showFormulas excelize.ShowFormulas
  6. showGridLines excelize.ShowGridLines
  7. showRowColHeaders excelize.ShowRowColHeaders
  8. showZeros excelize.ShowZeros
  9. rightToLeft excelize.RightToLeft
  10. showRuler excelize.ShowRuler
  11. view excelize.View
  12. topLeftCell excelize.TopLeftCell
  13. zoomScale excelize.ZoomScale
  14. )
  15. if err := f.GetSheetViewOptions(sheet, 0,
  16. &defaultGridColor,
  17. &showFormulas,
  18. &showGridLines,
  19. &showRowColHeaders,
  20. &showZeros,
  21. &rightToLeft,
  22. &showRuler,
  23. &view,
  24. &topLeftCell,
  25. &zoomScale,
  26. ); err != nil {
  27. fmt.Println(err)
  28. }
  29. fmt.Println("Default:")
  30. fmt.Println("- defaultGridColor:", defaultGridColor)
  31. fmt.Println("- showFormulas:", showFormulas)
  32. fmt.Println("- showGridLines:", showGridLines)
  33. fmt.Println("- showRowColHeaders:", showRowColHeaders)
  34. fmt.Println("- showZeros:", showZeros)
  35. fmt.Println("- rightToLeft:", rightToLeft)
  36. fmt.Println("- showRuler:", showRuler)
  37. fmt.Println("- view:", view)
  38. fmt.Println("- topLeftCell:", `"`+topLeftCell+`"`)
  39. fmt.Println("- zoomScale:", zoomScale)
  40. if err := f.SetSheetViewOptions(sheet, 0, excelize.ShowGridLines(false)); err != nil {
  41. fmt.Println(err)
  42. }
  43. if err := f.GetSheetViewOptions(sheet, 0, &showGridLines); err != nil {
  44. fmt.Println(err)
  45. }
  46. if err := f.SetSheetViewOptions(sheet, 0, excelize.ShowZeros(false)); err != nil {
  47. fmt.Println(err)
  48. }
  49. if err := f.GetSheetViewOptions(sheet, 0, &showZeros); err != nil {
  50. fmt.Println(err)
  51. }
  52. if err := f.SetSheetViewOptions(sheet, 0, excelize.View("pageLayout")); err != nil {
  53. fmt.Println(err)
  54. }
  55. if err := f.GetSheetViewOptions(sheet, 0, &view); err != nil {
  56. fmt.Println(err)
  57. }
  58. if err := f.SetSheetViewOptions(sheet, 0, excelize.TopLeftCell("B2")); err != nil {
  59. fmt.Println(err)
  60. }
  61. if err := f.GetSheetViewOptions(sheet, 0, &topLeftCell); err != nil {
  62. fmt.Println(err)
  63. }
  64. fmt.Println("After change:")
  65. fmt.Println("- showGridLines:", showGridLines)
  66. fmt.Println("- showZeros:", showZeros)
  67. fmt.Println("- view:", view)
  68. fmt.Println("- topLeftCell:", topLeftCell)

得到输出:

  1. Default:
  2. - defaultGridColor: true
  3. - showFormulas: false
  4. - showGridLines: true
  5. - showRowColHeaders: true
  6. - showZeros: true
  7. - rightToLeft: false
  8. - showRuler: true
  9. - view: normal
  10. - topLeftCell: ""
  11. - zoomScale: 0
  12. After change:
  13. - showGridLines: false
  14. - showZeros: false
  15. - view: pageLayout
  16. - topLeftCell: B2