获取工作表视图属性

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

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

可选视图参数类型
DefaultGridColorbool
RightToLeftbool
ShowFormulasbool
ShowGridLinesbool
ShowRowColHeadersbool
ZoomScalefloat64
TopLeftCellstring
ShowZerosbool
  • 例1,获取名为 Sheet1 的工作表上最后一个视图的网格线属性设置:
  1. var showGridLines excelize.ShowGridLines
  2. err = f.GetSheetViewOptions("Sheet1", -1, &showGridLines)
  • 例2:
  1. f := excelize.NewFile()
  2. const sheet = "Sheet1"
  3. var (
  4. defaultGridColor excelize.DefaultGridColor
  5. rightToLeft excelize.RightToLeft
  6. showFormulas excelize.ShowFormulas
  7. showGridLines excelize.ShowGridLines
  8. showZeros excelize.ShowZeros
  9. showRowColHeaders excelize.ShowRowColHeaders
  10. zoomScale excelize.ZoomScale
  11. topLeftCell excelize.TopLeftCell
  12. )
  13. if err := f.GetSheetViewOptions(sheet, 0,
  14. &defaultGridColor,
  15. &rightToLeft,
  16. &showFormulas,
  17. &showGridLines,
  18. &showZeros,
  19. &showRowColHeaders,
  20. &zoomScale,
  21. &topLeftCell,
  22. ); err != nil {
  23. fmt.Println(err)
  24. }
  25. fmt.Println("Default:")
  26. fmt.Println("- defaultGridColor:", defaultGridColor)
  27. fmt.Println("- rightToLeft:", rightToLeft)
  28. fmt.Println("- showFormulas:", showFormulas)
  29. fmt.Println("- showGridLines:", showGridLines)
  30. fmt.Println("- showZeros:", showZeros)
  31. fmt.Println("- showRowColHeaders:", showRowColHeaders)
  32. fmt.Println("- zoomScale:", zoomScale)
  33. fmt.Println("- topLeftCell:", `"`+topLeftCell+`"`)
  34. if err := f.SetSheetViewOptions(sheet, 0, excelize.TopLeftCell("B2")); err != nil {
  35. fmt.Println(err)
  36. }
  37. if err := f.GetSheetViewOptions(sheet, 0, &topLeftCell); err != nil {
  38. fmt.Println(err)
  39. }
  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. fmt.Println("After change:")
  53. fmt.Println("- showGridLines:", showGridLines)
  54. fmt.Println("- showZeros:", showZeros)
  55. fmt.Println("- topLeftCell:", topLeftCell)

得到输出:

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