sheetview.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Copyright 2016 - 2019 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. /* TODO
  50. // ShowWhiteSpace is a SheetViewOption. It specifies a flag indicating
  51. // whether page layout view shall display margins. False means do not display
  52. // left, right, top (header), and bottom (footer) margins (even when there is
  53. // data in the header or footer).
  54. ShowWhiteSpace bool
  55. // ShowZeros is a SheetViewOption.
  56. ShowZeros bool
  57. // WindowProtection is a SheetViewOption.
  58. WindowProtection bool
  59. */
  60. )
  61. // Defaults for each option are described in XML schema for CT_SheetView
  62. func (o TopLeftCell) setSheetViewOption(view *xlsxSheetView) {
  63. view.TopLeftCell = string(o)
  64. }
  65. func (o *TopLeftCell) getSheetViewOption(view *xlsxSheetView) {
  66. *o = TopLeftCell(string(view.TopLeftCell))
  67. }
  68. func (o DefaultGridColor) setSheetViewOption(view *xlsxSheetView) {
  69. view.DefaultGridColor = boolPtr(bool(o))
  70. }
  71. func (o *DefaultGridColor) getSheetViewOption(view *xlsxSheetView) {
  72. *o = DefaultGridColor(defaultTrue(view.DefaultGridColor)) // Excel default: true
  73. }
  74. func (o RightToLeft) setSheetViewOption(view *xlsxSheetView) {
  75. view.RightToLeft = bool(o) // Excel default: false
  76. }
  77. func (o *RightToLeft) getSheetViewOption(view *xlsxSheetView) {
  78. *o = RightToLeft(view.RightToLeft)
  79. }
  80. func (o ShowFormulas) setSheetViewOption(view *xlsxSheetView) {
  81. view.ShowFormulas = bool(o) // Excel default: false
  82. }
  83. func (o *ShowFormulas) getSheetViewOption(view *xlsxSheetView) {
  84. *o = ShowFormulas(view.ShowFormulas) // Excel default: false
  85. }
  86. func (o ShowGridLines) setSheetViewOption(view *xlsxSheetView) {
  87. view.ShowGridLines = boolPtr(bool(o))
  88. }
  89. func (o *ShowGridLines) getSheetViewOption(view *xlsxSheetView) {
  90. *o = ShowGridLines(defaultTrue(view.ShowGridLines)) // Excel default: true
  91. }
  92. func (o ShowRowColHeaders) setSheetViewOption(view *xlsxSheetView) {
  93. view.ShowRowColHeaders = boolPtr(bool(o))
  94. }
  95. func (o *ShowRowColHeaders) getSheetViewOption(view *xlsxSheetView) {
  96. *o = ShowRowColHeaders(defaultTrue(view.ShowRowColHeaders)) // Excel default: true
  97. }
  98. func (o ZoomScale) setSheetViewOption(view *xlsxSheetView) {
  99. // This attribute is restricted to values ranging from 10 to 400.
  100. if float64(o) >= 10 && float64(o) <= 400 {
  101. view.ZoomScale = float64(o)
  102. }
  103. }
  104. func (o *ZoomScale) getSheetViewOption(view *xlsxSheetView) {
  105. *o = ZoomScale(view.ZoomScale)
  106. }
  107. // getSheetView returns the SheetView object
  108. func (f *File) getSheetView(sheetName string, viewIndex int) (*xlsxSheetView, error) {
  109. xlsx, err := f.workSheetReader(sheetName)
  110. if err != nil {
  111. return nil, err
  112. }
  113. if viewIndex < 0 {
  114. if viewIndex < -len(xlsx.SheetViews.SheetView) {
  115. return nil, fmt.Errorf("view index %d out of range", viewIndex)
  116. }
  117. viewIndex = len(xlsx.SheetViews.SheetView) + viewIndex
  118. } else if viewIndex >= len(xlsx.SheetViews.SheetView) {
  119. return nil, fmt.Errorf("view index %d out of range", viewIndex)
  120. }
  121. return &(xlsx.SheetViews.SheetView[viewIndex]), err
  122. }
  123. // SetSheetViewOptions sets sheet view options. The viewIndex may be negative
  124. // and if so is counted backward (-1 is the last view).
  125. //
  126. // Available options:
  127. //
  128. // DefaultGridColor(bool)
  129. // RightToLeft(bool)
  130. // ShowFormulas(bool)
  131. // ShowGridLines(bool)
  132. // ShowRowColHeaders(bool)
  133. // ZoomScale(float64)
  134. // TopLeftCell(string)
  135. //
  136. // Example:
  137. //
  138. // err = f.SetSheetViewOptions("Sheet1", -1, ShowGridLines(false))
  139. //
  140. func (f *File) SetSheetViewOptions(name string, viewIndex int, opts ...SheetViewOption) error {
  141. view, err := f.getSheetView(name, viewIndex)
  142. if err != nil {
  143. return err
  144. }
  145. for _, opt := range opts {
  146. opt.setSheetViewOption(view)
  147. }
  148. return nil
  149. }
  150. // GetSheetViewOptions gets the value of sheet view options. The viewIndex may
  151. // be negative and if so is counted backward (-1 is the last view).
  152. //
  153. // Available options:
  154. //
  155. // DefaultGridColor(bool)
  156. // RightToLeft(bool)
  157. // ShowFormulas(bool)
  158. // ShowGridLines(bool)
  159. // ShowRowColHeaders(bool)
  160. // ZoomScale(float64)
  161. // TopLeftCell(string)
  162. //
  163. // Example:
  164. //
  165. // var showGridLines excelize.ShowGridLines
  166. // err = f.GetSheetViewOptions("Sheet1", -1, &showGridLines)
  167. //
  168. func (f *File) GetSheetViewOptions(name string, viewIndex int, opts ...SheetViewOptionPtr) error {
  169. view, err := f.getSheetView(name, viewIndex)
  170. if err != nil {
  171. return err
  172. }
  173. for _, opt := range opts {
  174. opt.getSheetViewOption(view)
  175. }
  176. return nil
  177. }