sheetpr.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. // SheetPrOption is an option of a view of a worksheet. See SetSheetPrOptions().
  11. type SheetPrOption interface {
  12. setSheetPrOption(view *xlsxSheetPr)
  13. }
  14. // SheetPrOptionPtr is a writable SheetPrOption. See GetSheetPrOptions().
  15. type SheetPrOptionPtr interface {
  16. SheetPrOption
  17. getSheetPrOption(view *xlsxSheetPr)
  18. }
  19. type (
  20. // CodeName is a SheetPrOption
  21. CodeName string
  22. // EnableFormatConditionsCalculation is a SheetPrOption
  23. EnableFormatConditionsCalculation bool
  24. // Published is a SheetPrOption
  25. Published bool
  26. // FitToPage is a SheetPrOption
  27. FitToPage bool
  28. // AutoPageBreaks is a SheetPrOption
  29. AutoPageBreaks bool
  30. // OutlineSummaryBelow is an outlinePr, within SheetPr option
  31. OutlineSummaryBelow bool
  32. )
  33. // setSheetPrOption implements the SheetPrOption interface.
  34. func (o OutlineSummaryBelow) setSheetPrOption(pr *xlsxSheetPr) {
  35. if pr.OutlinePr == nil {
  36. pr.OutlinePr = new(xlsxOutlinePr)
  37. }
  38. pr.OutlinePr.SummaryBelow = bool(o)
  39. }
  40. // getSheetPrOption implements the SheetPrOptionPtr interface.
  41. func (o *OutlineSummaryBelow) getSheetPrOption(pr *xlsxSheetPr) {
  42. // Excel default: true
  43. if pr == nil || pr.OutlinePr == nil {
  44. *o = true
  45. return
  46. }
  47. *o = OutlineSummaryBelow(defaultTrue(&pr.OutlinePr.SummaryBelow))
  48. }
  49. // setSheetPrOption implements the SheetPrOption interface and specifies a
  50. // stable name of the sheet.
  51. func (o CodeName) setSheetPrOption(pr *xlsxSheetPr) {
  52. pr.CodeName = string(o)
  53. }
  54. // getSheetPrOption implements the SheetPrOptionPtr interface and get the
  55. // stable name of the sheet.
  56. func (o *CodeName) getSheetPrOption(pr *xlsxSheetPr) {
  57. if pr == nil {
  58. *o = ""
  59. return
  60. }
  61. *o = CodeName(pr.CodeName)
  62. }
  63. // setSheetPrOption implements the SheetPrOption interface and flag indicating
  64. // whether the conditional formatting calculations shall be evaluated.
  65. func (o EnableFormatConditionsCalculation) setSheetPrOption(pr *xlsxSheetPr) {
  66. pr.EnableFormatConditionsCalculation = boolPtr(bool(o))
  67. }
  68. // getSheetPrOption implements the SheetPrOptionPtr interface and get the
  69. // settings of whether the conditional formatting calculations shall be
  70. // evaluated.
  71. func (o *EnableFormatConditionsCalculation) getSheetPrOption(pr *xlsxSheetPr) {
  72. if pr == nil {
  73. *o = true
  74. return
  75. }
  76. *o = EnableFormatConditionsCalculation(defaultTrue(pr.EnableFormatConditionsCalculation))
  77. }
  78. // setSheetPrOption implements the SheetPrOption interface and flag indicating
  79. // whether the worksheet is published.
  80. func (o Published) setSheetPrOption(pr *xlsxSheetPr) {
  81. pr.Published = boolPtr(bool(o))
  82. }
  83. // getSheetPrOption implements the SheetPrOptionPtr interface and get the
  84. // settings of whether the worksheet is published.
  85. func (o *Published) getSheetPrOption(pr *xlsxSheetPr) {
  86. if pr == nil {
  87. *o = true
  88. return
  89. }
  90. *o = Published(defaultTrue(pr.Published))
  91. }
  92. // setSheetPrOption implements the SheetPrOption interface.
  93. func (o FitToPage) setSheetPrOption(pr *xlsxSheetPr) {
  94. if pr.PageSetUpPr == nil {
  95. if !o {
  96. return
  97. }
  98. pr.PageSetUpPr = new(xlsxPageSetUpPr)
  99. }
  100. pr.PageSetUpPr.FitToPage = bool(o)
  101. }
  102. // getSheetPrOption implements the SheetPrOptionPtr interface.
  103. func (o *FitToPage) getSheetPrOption(pr *xlsxSheetPr) {
  104. // Excel default: false
  105. if pr == nil || pr.PageSetUpPr == nil {
  106. *o = false
  107. return
  108. }
  109. *o = FitToPage(pr.PageSetUpPr.FitToPage)
  110. }
  111. // setSheetPrOption implements the SheetPrOption interface.
  112. func (o AutoPageBreaks) setSheetPrOption(pr *xlsxSheetPr) {
  113. if pr.PageSetUpPr == nil {
  114. if !o {
  115. return
  116. }
  117. pr.PageSetUpPr = new(xlsxPageSetUpPr)
  118. }
  119. pr.PageSetUpPr.AutoPageBreaks = bool(o)
  120. }
  121. // getSheetPrOption implements the SheetPrOptionPtr interface.
  122. func (o *AutoPageBreaks) getSheetPrOption(pr *xlsxSheetPr) {
  123. // Excel default: false
  124. if pr == nil || pr.PageSetUpPr == nil {
  125. *o = false
  126. return
  127. }
  128. *o = AutoPageBreaks(pr.PageSetUpPr.AutoPageBreaks)
  129. }
  130. // SetSheetPrOptions provides a function to sets worksheet properties.
  131. //
  132. // Available options:
  133. // CodeName(string)
  134. // EnableFormatConditionsCalculation(bool)
  135. // Published(bool)
  136. // FitToPage(bool)
  137. // AutoPageBreaks(bool)
  138. // OutlineSummaryBelow(bool)
  139. func (f *File) SetSheetPrOptions(name string, opts ...SheetPrOption) error {
  140. sheet, err := f.workSheetReader(name)
  141. if err != nil {
  142. return err
  143. }
  144. pr := sheet.SheetPr
  145. if pr == nil {
  146. pr = new(xlsxSheetPr)
  147. sheet.SheetPr = pr
  148. }
  149. for _, opt := range opts {
  150. opt.setSheetPrOption(pr)
  151. }
  152. return err
  153. }
  154. // GetSheetPrOptions provides a function to gets worksheet properties.
  155. //
  156. // Available options:
  157. // CodeName(string)
  158. // EnableFormatConditionsCalculation(bool)
  159. // Published(bool)
  160. // FitToPage(bool)
  161. // AutoPageBreaks(bool)
  162. // OutlineSummaryBelow(bool)
  163. func (f *File) GetSheetPrOptions(name string, opts ...SheetPrOptionPtr) error {
  164. sheet, err := f.workSheetReader(name)
  165. if err != nil {
  166. return err
  167. }
  168. pr := sheet.SheetPr
  169. for _, opt := range opts {
  170. opt.getSheetPrOption(pr)
  171. }
  172. return err
  173. }