sheetview.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Copyright 2016 - 2021 The excelize Authors. All rights reserved. Use of
  2. // this source code is governed by a BSD-style license that can be found in
  3. // the LICENSE file.
  4. //
  5. // Package excelize providing a set of functions that allow you to write to
  6. // and read from XLSX / XLSM / XLTM files. Supports reading and writing
  7. // spreadsheet documents generated by Microsoft Excel™ 2007 and later. Supports
  8. // complex components by high compatibility, and provided streaming API for
  9. // generating or reading data from a worksheet with huge amounts of data. This
  10. // library needs Go version 1.15 or later.
  11. package excelize
  12. import "fmt"
  13. // SheetViewOption is an option of a view of a worksheet. See
  14. // SetSheetViewOptions().
  15. type SheetViewOption interface {
  16. setSheetViewOption(view *xlsxSheetView)
  17. }
  18. // SheetViewOptionPtr is a writable SheetViewOption. See
  19. // GetSheetViewOptions().
  20. type SheetViewOptionPtr interface {
  21. SheetViewOption
  22. getSheetViewOption(view *xlsxSheetView)
  23. }
  24. type (
  25. // DefaultGridColor is a SheetViewOption. It specifies a flag indicating that
  26. // the consuming application should use the default grid lines color (system
  27. // dependent). Overrides any color specified in colorId.
  28. DefaultGridColor bool
  29. // RightToLeft is a SheetViewOption. It specifies a flag indicating whether
  30. // the sheet is in 'right to left' display mode. When in this mode, Column A
  31. // is on the far right, Column B ;is one column left of Column A, and so on.
  32. // Also, information in cells is displayed in the Right to Left format.
  33. RightToLeft bool
  34. // ShowFormulas is a SheetViewOption. It specifies a flag indicating whether
  35. // this sheet should display formulas.
  36. ShowFormulas bool
  37. // ShowGridLines is a SheetViewOption. It specifies a flag indicating whether
  38. // this sheet should display gridlines.
  39. ShowGridLines bool
  40. // ShowRowColHeaders is a SheetViewOption. It specifies a flag indicating
  41. // whether the sheet should display row and column headings.
  42. ShowRowColHeaders bool
  43. // ZoomScale is a SheetViewOption. It specifies a window zoom magnification
  44. // for current view representing percent values. This attribute is restricted
  45. // to values ranging from 10 to 400. Horizontal & Vertical scale together.
  46. ZoomScale float64
  47. // TopLeftCell is a SheetViewOption. It specifies a location of the top left
  48. // visible cell Location of the top left visible cell in the bottom right
  49. // pane (when in Left-to-Right mode).
  50. TopLeftCell string
  51. // ShowZeros is a SheetViewOption. It specifies a flag indicating
  52. // whether to "show a zero in cells that have zero value".
  53. // When using a formula to reference another cell which is empty, the referenced value becomes 0
  54. // when the flag is true. (Default setting is true.)
  55. ShowZeros bool
  56. /* TODO
  57. // ShowWhiteSpace is a SheetViewOption. It specifies a flag indicating
  58. // whether page layout view shall display margins. False means do not display
  59. // left, right, top (header), and bottom (footer) margins (even when there is
  60. // data in the header or footer).
  61. ShowWhiteSpace bool
  62. // WindowProtection is a SheetViewOption.
  63. WindowProtection bool
  64. */
  65. )
  66. // Defaults for each option are described in XML schema for CT_SheetView
  67. func (o TopLeftCell) setSheetViewOption(view *xlsxSheetView) {
  68. view.TopLeftCell = string(o)
  69. }
  70. func (o *TopLeftCell) getSheetViewOption(view *xlsxSheetView) {
  71. *o = TopLeftCell(string(view.TopLeftCell))
  72. }
  73. func (o DefaultGridColor) setSheetViewOption(view *xlsxSheetView) {
  74. view.DefaultGridColor = boolPtr(bool(o))
  75. }
  76. func (o *DefaultGridColor) getSheetViewOption(view *xlsxSheetView) {
  77. *o = DefaultGridColor(defaultTrue(view.DefaultGridColor)) // Excel default: true
  78. }
  79. func (o RightToLeft) setSheetViewOption(view *xlsxSheetView) {
  80. view.RightToLeft = bool(o) // Excel default: false
  81. }
  82. func (o *RightToLeft) getSheetViewOption(view *xlsxSheetView) {
  83. *o = RightToLeft(view.RightToLeft)
  84. }
  85. func (o ShowFormulas) setSheetViewOption(view *xlsxSheetView) {
  86. view.ShowFormulas = bool(o) // Excel default: false
  87. }
  88. func (o *ShowFormulas) getSheetViewOption(view *xlsxSheetView) {
  89. *o = ShowFormulas(view.ShowFormulas) // Excel default: false
  90. }
  91. func (o ShowGridLines) setSheetViewOption(view *xlsxSheetView) {
  92. view.ShowGridLines = boolPtr(bool(o))
  93. }
  94. func (o *ShowGridLines) getSheetViewOption(view *xlsxSheetView) {
  95. *o = ShowGridLines(defaultTrue(view.ShowGridLines)) // Excel default: true
  96. }
  97. func (o ShowZeros) setSheetViewOption(view *xlsxSheetView) {
  98. view.ShowZeros = boolPtr(bool(o))
  99. }
  100. func (o *ShowZeros) getSheetViewOption(view *xlsxSheetView) {
  101. *o = ShowZeros(defaultTrue(view.ShowZeros)) // Excel default: true
  102. }
  103. func (o ShowRowColHeaders) setSheetViewOption(view *xlsxSheetView) {
  104. view.ShowRowColHeaders = boolPtr(bool(o))
  105. }
  106. func (o *ShowRowColHeaders) getSheetViewOption(view *xlsxSheetView) {
  107. *o = ShowRowColHeaders(defaultTrue(view.ShowRowColHeaders)) // Excel default: true
  108. }
  109. func (o ZoomScale) setSheetViewOption(view *xlsxSheetView) {
  110. // This attribute is restricted to values ranging from 10 to 400.
  111. if float64(o) >= 10 && float64(o) <= 400 {
  112. view.ZoomScale = float64(o)
  113. }
  114. }
  115. func (o *ZoomScale) getSheetViewOption(view *xlsxSheetView) {
  116. *o = ZoomScale(view.ZoomScale)
  117. }
  118. // getSheetView returns the SheetView object
  119. func (f *File) getSheetView(sheet string, viewIndex int) (*xlsxSheetView, error) {
  120. ws, err := f.workSheetReader(sheet)
  121. if err != nil {
  122. return nil, err
  123. }
  124. if viewIndex < 0 {
  125. if viewIndex < -len(ws.SheetViews.SheetView) {
  126. return nil, fmt.Errorf("view index %d out of range", viewIndex)
  127. }
  128. viewIndex = len(ws.SheetViews.SheetView) + viewIndex
  129. } else if viewIndex >= len(ws.SheetViews.SheetView) {
  130. return nil, fmt.Errorf("view index %d out of range", viewIndex)
  131. }
  132. return &(ws.SheetViews.SheetView[viewIndex]), err
  133. }
  134. // SetSheetViewOptions sets sheet view options. The viewIndex may be negative
  135. // and if so is counted backward (-1 is the last view).
  136. //
  137. // Available options:
  138. //
  139. // DefaultGridColor(bool)
  140. // RightToLeft(bool)
  141. // ShowFormulas(bool)
  142. // ShowGridLines(bool)
  143. // ShowRowColHeaders(bool)
  144. // ZoomScale(float64)
  145. // TopLeftCell(string)
  146. // ShowZeros(bool)
  147. //
  148. // Example:
  149. //
  150. // err = f.SetSheetViewOptions("Sheet1", -1, ShowGridLines(false))
  151. //
  152. func (f *File) SetSheetViewOptions(name string, viewIndex int, opts ...SheetViewOption) error {
  153. view, err := f.getSheetView(name, viewIndex)
  154. if err != nil {
  155. return err
  156. }
  157. for _, opt := range opts {
  158. opt.setSheetViewOption(view)
  159. }
  160. return nil
  161. }
  162. // GetSheetViewOptions gets the value of sheet view options. The viewIndex may
  163. // be negative and if so is counted backward (-1 is the last view).
  164. //
  165. // Available options:
  166. //
  167. // DefaultGridColor(bool)
  168. // RightToLeft(bool)
  169. // ShowFormulas(bool)
  170. // ShowGridLines(bool)
  171. // ShowRowColHeaders(bool)
  172. // ZoomScale(float64)
  173. // TopLeftCell(string)
  174. // ShowZeros(bool)
  175. //
  176. // Example:
  177. //
  178. // var showGridLines excelize.ShowGridLines
  179. // err = f.GetSheetViewOptions("Sheet1", -1, &showGridLines)
  180. //
  181. func (f *File) GetSheetViewOptions(name string, viewIndex int, opts ...SheetViewOptionPtr) error {
  182. view, err := f.getSheetView(name, viewIndex)
  183. if err != nil {
  184. return err
  185. }
  186. for _, opt := range opts {
  187. opt.getSheetViewOption(view)
  188. }
  189. return nil
  190. }