sheetpr.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. Package excelize providing a set of functions that allow you to write to
  3. and read from XLSX files. Support reads and writes XLSX file generated by
  4. Microsoft Excel™ 2007 and later. Support save file without losing original
  5. charts of XLSX. This library needs Go version 1.8 or later.
  6. Copyright 2016 - 2018 The excelize Authors. All rights reserved. Use of
  7. this source code is governed by a BSD-style license that can be found in
  8. the LICENSE file.
  9. */
  10. package excelize
  11. // SheetPrOption is an option of a view of a worksheet. See SetSheetPrOptions().
  12. type SheetPrOption interface {
  13. setSheetPrOption(view *xlsxSheetPr)
  14. }
  15. // SheetPrOptionPtr is a writable SheetPrOption. See GetSheetPrOptions().
  16. type SheetPrOptionPtr interface {
  17. SheetPrOption
  18. getSheetPrOption(view *xlsxSheetPr)
  19. }
  20. type (
  21. // CodeName is a SheetPrOption
  22. CodeName string
  23. // EnableFormatConditionsCalculation is a SheetPrOption
  24. EnableFormatConditionsCalculation bool
  25. // Published is a SheetPrOption
  26. Published bool
  27. // FitToPage is a SheetPrOption
  28. FitToPage bool
  29. // AutoPageBreaks is a SheetPrOption
  30. AutoPageBreaks bool
  31. )
  32. func (o CodeName) setSheetPrOption(pr *xlsxSheetPr) {
  33. pr.CodeName = string(o)
  34. }
  35. func (o *CodeName) getSheetPrOption(pr *xlsxSheetPr) {
  36. if pr == nil {
  37. *o = ""
  38. return
  39. }
  40. *o = CodeName(pr.CodeName)
  41. }
  42. func (o EnableFormatConditionsCalculation) setSheetPrOption(pr *xlsxSheetPr) {
  43. pr.EnableFormatConditionsCalculation = boolPtr(bool(o))
  44. }
  45. func (o *EnableFormatConditionsCalculation) getSheetPrOption(pr *xlsxSheetPr) {
  46. if pr == nil {
  47. *o = true
  48. return
  49. }
  50. *o = EnableFormatConditionsCalculation(defaultTrue(pr.EnableFormatConditionsCalculation))
  51. }
  52. func (o Published) setSheetPrOption(pr *xlsxSheetPr) {
  53. pr.Published = boolPtr(bool(o))
  54. }
  55. func (o *Published) getSheetPrOption(pr *xlsxSheetPr) {
  56. if pr == nil {
  57. *o = true
  58. return
  59. }
  60. *o = Published(defaultTrue(pr.Published))
  61. }
  62. func (o FitToPage) setSheetPrOption(pr *xlsxSheetPr) {
  63. if pr.PageSetUpPr == nil {
  64. if !o {
  65. return
  66. }
  67. pr.PageSetUpPr = new(xlsxPageSetUpPr)
  68. }
  69. pr.PageSetUpPr.FitToPage = bool(o)
  70. }
  71. func (o *FitToPage) getSheetPrOption(pr *xlsxSheetPr) {
  72. // Excel default: false
  73. if pr == nil || pr.PageSetUpPr == nil {
  74. *o = false
  75. return
  76. }
  77. *o = FitToPage(pr.PageSetUpPr.FitToPage)
  78. }
  79. func (o AutoPageBreaks) setSheetPrOption(pr *xlsxSheetPr) {
  80. if pr.PageSetUpPr == nil {
  81. if !o {
  82. return
  83. }
  84. pr.PageSetUpPr = new(xlsxPageSetUpPr)
  85. }
  86. pr.PageSetUpPr.AutoPageBreaks = bool(o)
  87. }
  88. func (o *AutoPageBreaks) getSheetPrOption(pr *xlsxSheetPr) {
  89. // Excel default: false
  90. if pr == nil || pr.PageSetUpPr == nil {
  91. *o = false
  92. return
  93. }
  94. *o = AutoPageBreaks(pr.PageSetUpPr.AutoPageBreaks)
  95. }
  96. // SetSheetPrOptions provides a function to sets worksheet properties.
  97. //
  98. // Available options:
  99. // CodeName(string)
  100. // EnableFormatConditionsCalculation(bool)
  101. // Published(bool)
  102. // FitToPage(bool)
  103. // AutoPageBreaks(bool)
  104. func (f *File) SetSheetPrOptions(name string, opts ...SheetPrOption) error {
  105. sheet := f.workSheetReader(name)
  106. pr := sheet.SheetPr
  107. if pr == nil {
  108. pr = new(xlsxSheetPr)
  109. sheet.SheetPr = pr
  110. }
  111. for _, opt := range opts {
  112. opt.setSheetPrOption(pr)
  113. }
  114. return nil
  115. }
  116. // GetSheetPrOptions provides a function to gets worksheet properties.
  117. //
  118. // Available options:
  119. // CodeName(string)
  120. // EnableFormatConditionsCalculation(bool)
  121. // Published(bool)
  122. // FitToPage(bool)
  123. // AutoPageBreaks(bool)
  124. func (f *File) GetSheetPrOptions(name string, opts ...SheetPrOptionPtr) error {
  125. sheet := f.workSheetReader(name)
  126. pr := sheet.SheetPr
  127. for _, opt := range opts {
  128. opt.getSheetPrOption(pr)
  129. }
  130. return nil
  131. }