sheetpr.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. // 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. func (o OutlineSummaryBelow) setSheetPrOption(pr *xlsxSheetPr) {
  34. if pr.OutlinePr == nil {
  35. pr.OutlinePr = new(xlsxOutlinePr)
  36. }
  37. pr.OutlinePr.SummaryBelow = bool(o)
  38. }
  39. func (o *OutlineSummaryBelow) getSheetPrOption(pr *xlsxSheetPr) {
  40. // Excel default: true
  41. if pr == nil || pr.OutlinePr == nil {
  42. *o = true
  43. return
  44. }
  45. *o = OutlineSummaryBelow(defaultTrue(&pr.OutlinePr.SummaryBelow))
  46. }
  47. func (o CodeName) setSheetPrOption(pr *xlsxSheetPr) {
  48. pr.CodeName = string(o)
  49. }
  50. func (o *CodeName) getSheetPrOption(pr *xlsxSheetPr) {
  51. if pr == nil {
  52. *o = ""
  53. return
  54. }
  55. *o = CodeName(pr.CodeName)
  56. }
  57. func (o EnableFormatConditionsCalculation) setSheetPrOption(pr *xlsxSheetPr) {
  58. pr.EnableFormatConditionsCalculation = boolPtr(bool(o))
  59. }
  60. func (o *EnableFormatConditionsCalculation) getSheetPrOption(pr *xlsxSheetPr) {
  61. if pr == nil {
  62. *o = true
  63. return
  64. }
  65. *o = EnableFormatConditionsCalculation(defaultTrue(pr.EnableFormatConditionsCalculation))
  66. }
  67. func (o Published) setSheetPrOption(pr *xlsxSheetPr) {
  68. pr.Published = boolPtr(bool(o))
  69. }
  70. func (o *Published) getSheetPrOption(pr *xlsxSheetPr) {
  71. if pr == nil {
  72. *o = true
  73. return
  74. }
  75. *o = Published(defaultTrue(pr.Published))
  76. }
  77. func (o FitToPage) setSheetPrOption(pr *xlsxSheetPr) {
  78. if pr.PageSetUpPr == nil {
  79. if !o {
  80. return
  81. }
  82. pr.PageSetUpPr = new(xlsxPageSetUpPr)
  83. }
  84. pr.PageSetUpPr.FitToPage = bool(o)
  85. }
  86. func (o *FitToPage) getSheetPrOption(pr *xlsxSheetPr) {
  87. // Excel default: false
  88. if pr == nil || pr.PageSetUpPr == nil {
  89. *o = false
  90. return
  91. }
  92. *o = FitToPage(pr.PageSetUpPr.FitToPage)
  93. }
  94. func (o AutoPageBreaks) setSheetPrOption(pr *xlsxSheetPr) {
  95. if pr.PageSetUpPr == nil {
  96. if !o {
  97. return
  98. }
  99. pr.PageSetUpPr = new(xlsxPageSetUpPr)
  100. }
  101. pr.PageSetUpPr.AutoPageBreaks = bool(o)
  102. }
  103. func (o *AutoPageBreaks) getSheetPrOption(pr *xlsxSheetPr) {
  104. // Excel default: false
  105. if pr == nil || pr.PageSetUpPr == nil {
  106. *o = false
  107. return
  108. }
  109. *o = AutoPageBreaks(pr.PageSetUpPr.AutoPageBreaks)
  110. }
  111. // SetSheetPrOptions provides a function to sets worksheet properties.
  112. //
  113. // Available options:
  114. // CodeName(string)
  115. // EnableFormatConditionsCalculation(bool)
  116. // Published(bool)
  117. // FitToPage(bool)
  118. // AutoPageBreaks(bool)
  119. // OutlineSummaryBelow(bool)
  120. func (f *File) SetSheetPrOptions(name string, opts ...SheetPrOption) error {
  121. sheet := f.workSheetReader(name)
  122. pr := sheet.SheetPr
  123. if pr == nil {
  124. pr = new(xlsxSheetPr)
  125. sheet.SheetPr = pr
  126. }
  127. for _, opt := range opts {
  128. opt.setSheetPrOption(pr)
  129. }
  130. return nil
  131. }
  132. // GetSheetPrOptions provides a function to gets worksheet properties.
  133. //
  134. // Available options:
  135. // CodeName(string)
  136. // EnableFormatConditionsCalculation(bool)
  137. // Published(bool)
  138. // FitToPage(bool)
  139. // AutoPageBreaks(bool)
  140. // OutlineSummaryBelow(bool)
  141. func (f *File) GetSheetPrOptions(name string, opts ...SheetPrOptionPtr) error {
  142. sheet := f.workSheetReader(name)
  143. pr := sheet.SheetPr
  144. for _, opt := range opts {
  145. opt.getSheetPrOption(pr)
  146. }
  147. return nil
  148. }