sheetview.go 6.2 KB

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