sheetpr.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package excelize
  2. // SheetPrOption is an option of a view of a worksheet. See SetSheetPrOptions().
  3. type SheetPrOption interface {
  4. setSheetPrOption(view *xlsxSheetPr)
  5. }
  6. // SheetPrOptionPtr is a writable SheetPrOption. See GetSheetPrOptions().
  7. type SheetPrOptionPtr interface {
  8. SheetPrOption
  9. getSheetPrOption(view *xlsxSheetPr)
  10. }
  11. type (
  12. // CodeName is a SheetPrOption
  13. CodeName string
  14. // EnableFormatConditionsCalculation is a SheetPrOption
  15. EnableFormatConditionsCalculation bool
  16. // Published is a SheetPrOption
  17. Published bool
  18. // FitToPage is a SheetPrOption
  19. FitToPage bool
  20. // AutoPageBreaks is a SheetPrOption
  21. AutoPageBreaks bool
  22. )
  23. func (o CodeName) setSheetPrOption(pr *xlsxSheetPr) {
  24. pr.CodeName = string(o)
  25. }
  26. func (o *CodeName) getSheetPrOption(pr *xlsxSheetPr) {
  27. if pr == nil {
  28. *o = ""
  29. return
  30. }
  31. *o = CodeName(pr.CodeName)
  32. }
  33. func (o EnableFormatConditionsCalculation) setSheetPrOption(pr *xlsxSheetPr) {
  34. pr.EnableFormatConditionsCalculation = boolPtr(bool(o))
  35. }
  36. func (o *EnableFormatConditionsCalculation) getSheetPrOption(pr *xlsxSheetPr) {
  37. if pr == nil {
  38. *o = true
  39. return
  40. }
  41. *o = EnableFormatConditionsCalculation(defaultTrue(pr.EnableFormatConditionsCalculation))
  42. }
  43. func (o Published) setSheetPrOption(pr *xlsxSheetPr) {
  44. pr.Published = boolPtr(bool(o))
  45. }
  46. func (o *Published) getSheetPrOption(pr *xlsxSheetPr) {
  47. if pr == nil {
  48. *o = true
  49. return
  50. }
  51. *o = Published(defaultTrue(pr.Published))
  52. }
  53. func (o FitToPage) setSheetPrOption(pr *xlsxSheetPr) {
  54. if pr.PageSetUpPr == nil {
  55. if !o {
  56. return
  57. }
  58. pr.PageSetUpPr = new(xlsxPageSetUpPr)
  59. }
  60. pr.PageSetUpPr.FitToPage = bool(o)
  61. }
  62. func (o *FitToPage) getSheetPrOption(pr *xlsxSheetPr) {
  63. // Excel default: false
  64. if pr == nil || pr.PageSetUpPr == nil {
  65. *o = false
  66. return
  67. }
  68. *o = FitToPage(pr.PageSetUpPr.FitToPage)
  69. }
  70. func (o AutoPageBreaks) setSheetPrOption(pr *xlsxSheetPr) {
  71. if pr.PageSetUpPr == nil {
  72. if !o {
  73. return
  74. }
  75. pr.PageSetUpPr = new(xlsxPageSetUpPr)
  76. }
  77. pr.PageSetUpPr.AutoPageBreaks = bool(o)
  78. }
  79. func (o *AutoPageBreaks) getSheetPrOption(pr *xlsxSheetPr) {
  80. // Excel default: false
  81. if pr == nil || pr.PageSetUpPr == nil {
  82. *o = false
  83. return
  84. }
  85. *o = AutoPageBreaks(pr.PageSetUpPr.AutoPageBreaks)
  86. }
  87. // SetSheetPrOptions provides a function to sets worksheet properties.
  88. //
  89. // Available options:
  90. // CodeName(string)
  91. // EnableFormatConditionsCalculation(bool)
  92. // Published(bool)
  93. // FitToPage(bool)
  94. // AutoPageBreaks(bool)
  95. func (f *File) SetSheetPrOptions(name string, opts ...SheetPrOption) error {
  96. sheet := f.workSheetReader(name)
  97. pr := sheet.SheetPr
  98. if pr == nil {
  99. pr = new(xlsxSheetPr)
  100. sheet.SheetPr = pr
  101. }
  102. for _, opt := range opts {
  103. opt.setSheetPrOption(pr)
  104. }
  105. return nil
  106. }
  107. // GetSheetPrOptions provides a function to gets worksheet properties.
  108. //
  109. // Available options:
  110. // CodeName(string)
  111. // EnableFormatConditionsCalculation(bool)
  112. // Published(bool)
  113. // FitToPage(bool)
  114. // AutoPageBreaks(bool)
  115. func (f *File) GetSheetPrOptions(name string, opts ...SheetPrOptionPtr) error {
  116. sheet := f.workSheetReader(name)
  117. pr := sheet.SheetPr
  118. for _, opt := range opts {
  119. opt.getSheetPrOption(pr)
  120. }
  121. return nil
  122. }