sheetview.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package excelize
  2. import "fmt"
  3. // SheetViewOption is an option of a view of a worksheet. See SetSheetViewOptions().
  4. type SheetViewOption interface {
  5. setSheetViewOption(view *xlsxSheetView)
  6. }
  7. // SheetViewOptionPtr is a writable SheetViewOption. See GetSheetViewOptions().
  8. type SheetViewOptionPtr interface {
  9. SheetViewOption
  10. getSheetViewOption(view *xlsxSheetView)
  11. }
  12. type (
  13. // DefaultGridColor is a SheetViewOption.
  14. DefaultGridColor bool
  15. // RightToLeft is a SheetViewOption.
  16. RightToLeft bool
  17. // ShowFormulas is a SheetViewOption.
  18. ShowFormulas bool
  19. // ShowGridLines is a SheetViewOption.
  20. ShowGridLines bool
  21. // ShowRowColHeaders is a SheetViewOption.
  22. ShowRowColHeaders bool
  23. // ZoomScale is a SheetViewOption.
  24. ZoomScale float64
  25. /* TODO
  26. // ShowWhiteSpace is a SheetViewOption.
  27. ShowWhiteSpace bool
  28. // ShowZeros is a SheetViewOption.
  29. ShowZeros bool
  30. // WindowProtection is a SheetViewOption.
  31. WindowProtection bool
  32. */
  33. )
  34. // Defaults for each option are described in XML schema for CT_SheetView
  35. func (o DefaultGridColor) setSheetViewOption(view *xlsxSheetView) {
  36. view.DefaultGridColor = boolPtr(bool(o))
  37. }
  38. func (o *DefaultGridColor) getSheetViewOption(view *xlsxSheetView) {
  39. *o = DefaultGridColor(defaultTrue(view.DefaultGridColor)) // Excel default: true
  40. }
  41. func (o RightToLeft) setSheetViewOption(view *xlsxSheetView) {
  42. view.RightToLeft = bool(o) // Excel default: false
  43. }
  44. func (o *RightToLeft) getSheetViewOption(view *xlsxSheetView) {
  45. *o = RightToLeft(view.RightToLeft)
  46. }
  47. func (o ShowFormulas) setSheetViewOption(view *xlsxSheetView) {
  48. view.ShowFormulas = bool(o) // Excel default: false
  49. }
  50. func (o *ShowFormulas) getSheetViewOption(view *xlsxSheetView) {
  51. *o = ShowFormulas(view.ShowFormulas) // Excel default: false
  52. }
  53. func (o ShowGridLines) setSheetViewOption(view *xlsxSheetView) {
  54. view.ShowGridLines = boolPtr(bool(o))
  55. }
  56. func (o *ShowGridLines) getSheetViewOption(view *xlsxSheetView) {
  57. *o = ShowGridLines(defaultTrue(view.ShowGridLines)) // Excel default: true
  58. }
  59. func (o ShowRowColHeaders) setSheetViewOption(view *xlsxSheetView) {
  60. view.ShowRowColHeaders = boolPtr(bool(o))
  61. }
  62. func (o *ShowRowColHeaders) getSheetViewOption(view *xlsxSheetView) {
  63. *o = ShowRowColHeaders(defaultTrue(view.ShowRowColHeaders)) // Excel default: true
  64. }
  65. func (o ZoomScale) setSheetViewOption(view *xlsxSheetView) {
  66. //This attribute is restricted to values ranging from 10 to 400.
  67. if float64(o) >= 10 && float64(o) <= 400 {
  68. view.ZoomScale = float64(o)
  69. }
  70. }
  71. func (o *ZoomScale) getSheetViewOption(view *xlsxSheetView) {
  72. *o = ZoomScale(view.ZoomScale)
  73. }
  74. // getSheetView returns the SheetView object
  75. func (f *File) getSheetView(sheetName string, viewIndex int) (*xlsxSheetView, error) {
  76. xlsx := f.workSheetReader(sheetName)
  77. if viewIndex < 0 {
  78. if viewIndex < -len(xlsx.SheetViews.SheetView) {
  79. return nil, fmt.Errorf("view index %d out of range", viewIndex)
  80. }
  81. viewIndex = len(xlsx.SheetViews.SheetView) + viewIndex
  82. } else if viewIndex >= len(xlsx.SheetViews.SheetView) {
  83. return nil, fmt.Errorf("view index %d out of range", viewIndex)
  84. }
  85. return &(xlsx.SheetViews.SheetView[viewIndex]), nil
  86. }
  87. // SetSheetViewOptions sets sheet view options.
  88. // The viewIndex may be negative and if so is counted backward (-1 is the last view).
  89. //
  90. // Available options:
  91. // DefaultGridColor(bool)
  92. // RightToLeft(bool)
  93. // ShowFormulas(bool)
  94. // ShowGridLines(bool)
  95. // ShowRowColHeaders(bool)
  96. // Example:
  97. // err = f.SetSheetViewOptions("Sheet1", -1, ShowGridLines(false))
  98. func (f *File) SetSheetViewOptions(name string, viewIndex int, opts ...SheetViewOption) error {
  99. view, err := f.getSheetView(name, viewIndex)
  100. if err != nil {
  101. return err
  102. }
  103. for _, opt := range opts {
  104. opt.setSheetViewOption(view)
  105. }
  106. return nil
  107. }
  108. // GetSheetViewOptions gets the value of sheet view options.
  109. // The viewIndex may be negative and if so is counted backward (-1 is the last view).
  110. //
  111. // Available options:
  112. // DefaultGridColor(bool)
  113. // RightToLeft(bool)
  114. // ShowFormulas(bool)
  115. // ShowGridLines(bool)
  116. // ShowRowColHeaders(bool)
  117. // Example:
  118. // var showGridLines excelize.ShowGridLines
  119. // err = f.GetSheetViewOptions("Sheet1", -1, &showGridLines)
  120. func (f *File) GetSheetViewOptions(name string, viewIndex int, opts ...SheetViewOptionPtr) error {
  121. view, err := f.getSheetView(name, viewIndex)
  122. if err != nil {
  123. return err
  124. }
  125. for _, opt := range opts {
  126. opt.getSheetViewOption(view)
  127. }
  128. return nil
  129. }