sheetview.go 6.9 KB

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