sheetpr.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Copyright 2016 - 2023 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 and
  6. // read from XLAM / XLSM / XLSX / XLTM / XLTX files. Supports reading and
  7. // writing spreadsheet documents generated by Microsoft Excel™ 2007 and later.
  8. // Supports complex components by high compatibility, and provided streaming
  9. // API for generating or reading data from a worksheet with huge amounts of
  10. // data. This library needs Go version 1.16 or later.
  11. package excelize
  12. import "reflect"
  13. // SetPageMargins provides a function to set worksheet page margins.
  14. func (f *File) SetPageMargins(sheet string, opts *PageLayoutMarginsOptions) error {
  15. ws, err := f.workSheetReader(sheet)
  16. if err != nil {
  17. return err
  18. }
  19. if opts == nil {
  20. return err
  21. }
  22. preparePageMargins := func(ws *xlsxWorksheet) {
  23. if ws.PageMargins == nil {
  24. ws.PageMargins = new(xlsxPageMargins)
  25. }
  26. }
  27. preparePrintOptions := func(ws *xlsxWorksheet) {
  28. if ws.PrintOptions == nil {
  29. ws.PrintOptions = new(xlsxPrintOptions)
  30. }
  31. }
  32. s := reflect.ValueOf(opts).Elem()
  33. for i := 0; i < 6; i++ {
  34. if !s.Field(i).IsNil() {
  35. preparePageMargins(ws)
  36. name := s.Type().Field(i).Name
  37. reflect.ValueOf(ws.PageMargins).Elem().FieldByName(name).Set(s.Field(i).Elem())
  38. }
  39. }
  40. if opts.Horizontally != nil {
  41. preparePrintOptions(ws)
  42. ws.PrintOptions.HorizontalCentered = *opts.Horizontally
  43. }
  44. if opts.Vertically != nil {
  45. preparePrintOptions(ws)
  46. ws.PrintOptions.VerticalCentered = *opts.Vertically
  47. }
  48. return err
  49. }
  50. // GetPageMargins provides a function to get worksheet page margins.
  51. func (f *File) GetPageMargins(sheet string) (PageLayoutMarginsOptions, error) {
  52. opts := PageLayoutMarginsOptions{
  53. Bottom: float64Ptr(0.75),
  54. Footer: float64Ptr(0.3),
  55. Header: float64Ptr(0.3),
  56. Left: float64Ptr(0.7),
  57. Right: float64Ptr(0.7),
  58. Top: float64Ptr(0.75),
  59. }
  60. ws, err := f.workSheetReader(sheet)
  61. if err != nil {
  62. return opts, err
  63. }
  64. if ws.PageMargins != nil {
  65. opts.Bottom = float64Ptr(ws.PageMargins.Bottom)
  66. opts.Footer = float64Ptr(ws.PageMargins.Footer)
  67. opts.Header = float64Ptr(ws.PageMargins.Header)
  68. opts.Left = float64Ptr(ws.PageMargins.Left)
  69. opts.Right = float64Ptr(ws.PageMargins.Right)
  70. opts.Top = float64Ptr(ws.PageMargins.Top)
  71. }
  72. if ws.PrintOptions != nil {
  73. opts.Horizontally = boolPtr(ws.PrintOptions.HorizontalCentered)
  74. opts.Vertically = boolPtr(ws.PrintOptions.VerticalCentered)
  75. }
  76. return opts, err
  77. }
  78. // prepareSheetPr create sheetPr element which not exist.
  79. func (ws *xlsxWorksheet) prepareSheetPr() {
  80. if ws.SheetPr == nil {
  81. ws.SheetPr = new(xlsxSheetPr)
  82. }
  83. }
  84. // setSheetOutlinePr set worksheet outline properties by given options.
  85. func (ws *xlsxWorksheet) setSheetOutlineProps(opts *SheetPropsOptions) {
  86. prepareOutlinePr := func(ws *xlsxWorksheet) {
  87. ws.prepareSheetPr()
  88. if ws.SheetPr.OutlinePr == nil {
  89. ws.SheetPr.OutlinePr = new(xlsxOutlinePr)
  90. }
  91. }
  92. if opts.OutlineSummaryBelow != nil {
  93. prepareOutlinePr(ws)
  94. ws.SheetPr.OutlinePr.SummaryBelow = opts.OutlineSummaryBelow
  95. }
  96. if opts.OutlineSummaryRight != nil {
  97. prepareOutlinePr(ws)
  98. ws.SheetPr.OutlinePr.SummaryRight = opts.OutlineSummaryRight
  99. }
  100. }
  101. // setSheetProps set worksheet format properties by given options.
  102. func (ws *xlsxWorksheet) setSheetProps(opts *SheetPropsOptions) {
  103. preparePageSetUpPr := func(ws *xlsxWorksheet) {
  104. ws.prepareSheetPr()
  105. if ws.SheetPr.PageSetUpPr == nil {
  106. ws.SheetPr.PageSetUpPr = new(xlsxPageSetUpPr)
  107. }
  108. }
  109. prepareTabColor := func(ws *xlsxWorksheet) {
  110. ws.prepareSheetPr()
  111. if ws.SheetPr.TabColor == nil {
  112. ws.SheetPr.TabColor = new(xlsxTabColor)
  113. }
  114. }
  115. if opts.CodeName != nil {
  116. ws.prepareSheetPr()
  117. ws.SheetPr.CodeName = *opts.CodeName
  118. }
  119. if opts.EnableFormatConditionsCalculation != nil {
  120. ws.prepareSheetPr()
  121. ws.SheetPr.EnableFormatConditionsCalculation = opts.EnableFormatConditionsCalculation
  122. }
  123. if opts.Published != nil {
  124. ws.prepareSheetPr()
  125. ws.SheetPr.Published = opts.Published
  126. }
  127. if opts.AutoPageBreaks != nil {
  128. preparePageSetUpPr(ws)
  129. ws.SheetPr.PageSetUpPr.AutoPageBreaks = *opts.AutoPageBreaks
  130. }
  131. if opts.FitToPage != nil {
  132. preparePageSetUpPr(ws)
  133. ws.SheetPr.PageSetUpPr.FitToPage = *opts.FitToPage
  134. }
  135. ws.setSheetOutlineProps(opts)
  136. s := reflect.ValueOf(opts).Elem()
  137. for i := 5; i < 9; i++ {
  138. if !s.Field(i).IsNil() {
  139. prepareTabColor(ws)
  140. name := s.Type().Field(i).Name
  141. reflect.ValueOf(ws.SheetPr.TabColor).Elem().FieldByName(name[8:]).Set(s.Field(i).Elem())
  142. }
  143. }
  144. }
  145. // SetSheetProps provides a function to set worksheet properties.
  146. func (f *File) SetSheetProps(sheet string, opts *SheetPropsOptions) error {
  147. ws, err := f.workSheetReader(sheet)
  148. if err != nil {
  149. return err
  150. }
  151. if opts == nil {
  152. return err
  153. }
  154. ws.setSheetProps(opts)
  155. if ws.SheetFormatPr == nil {
  156. ws.SheetFormatPr = &xlsxSheetFormatPr{DefaultRowHeight: defaultRowHeight}
  157. }
  158. s := reflect.ValueOf(opts).Elem()
  159. for i := 11; i < 18; i++ {
  160. if !s.Field(i).IsNil() {
  161. name := s.Type().Field(i).Name
  162. reflect.ValueOf(ws.SheetFormatPr).Elem().FieldByName(name).Set(s.Field(i).Elem())
  163. }
  164. }
  165. return err
  166. }
  167. // GetSheetProps provides a function to get worksheet properties.
  168. func (f *File) GetSheetProps(sheet string) (SheetPropsOptions, error) {
  169. baseColWidth := uint8(8)
  170. opts := SheetPropsOptions{
  171. EnableFormatConditionsCalculation: boolPtr(true),
  172. Published: boolPtr(true),
  173. AutoPageBreaks: boolPtr(true),
  174. OutlineSummaryBelow: boolPtr(true),
  175. BaseColWidth: &baseColWidth,
  176. }
  177. ws, err := f.workSheetReader(sheet)
  178. if err != nil {
  179. return opts, err
  180. }
  181. if ws.SheetPr != nil {
  182. opts.CodeName = stringPtr(ws.SheetPr.CodeName)
  183. if ws.SheetPr.EnableFormatConditionsCalculation != nil {
  184. opts.EnableFormatConditionsCalculation = ws.SheetPr.EnableFormatConditionsCalculation
  185. }
  186. if ws.SheetPr.Published != nil {
  187. opts.Published = ws.SheetPr.Published
  188. }
  189. if ws.SheetPr.PageSetUpPr != nil {
  190. opts.AutoPageBreaks = boolPtr(ws.SheetPr.PageSetUpPr.AutoPageBreaks)
  191. opts.FitToPage = boolPtr(ws.SheetPr.PageSetUpPr.FitToPage)
  192. }
  193. if ws.SheetPr.OutlinePr != nil {
  194. opts.OutlineSummaryBelow = ws.SheetPr.OutlinePr.SummaryBelow
  195. opts.OutlineSummaryRight = ws.SheetPr.OutlinePr.SummaryRight
  196. }
  197. if ws.SheetPr.TabColor != nil {
  198. opts.TabColorIndexed = intPtr(ws.SheetPr.TabColor.Indexed)
  199. opts.TabColorRGB = stringPtr(ws.SheetPr.TabColor.RGB)
  200. opts.TabColorTheme = intPtr(ws.SheetPr.TabColor.Theme)
  201. opts.TabColorTint = float64Ptr(ws.SheetPr.TabColor.Tint)
  202. }
  203. }
  204. if ws.SheetFormatPr != nil {
  205. opts.BaseColWidth = &ws.SheetFormatPr.BaseColWidth
  206. opts.DefaultColWidth = float64Ptr(ws.SheetFormatPr.DefaultColWidth)
  207. opts.DefaultRowHeight = float64Ptr(ws.SheetFormatPr.DefaultRowHeight)
  208. opts.CustomHeight = boolPtr(ws.SheetFormatPr.CustomHeight)
  209. opts.ZeroHeight = boolPtr(ws.SheetFormatPr.ZeroHeight)
  210. opts.ThickTop = boolPtr(ws.SheetFormatPr.ThickTop)
  211. opts.ThickBottom = boolPtr(ws.SheetFormatPr.ThickBottom)
  212. }
  213. return opts, err
  214. }