sheetpr.go 3.6 KB

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