sheetpr.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. // Copyright 2016 - 2020 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 / XLSM / XLTM files. Supports reading and writing
  7. // spreadsheet documents generated by Microsoft Exce™ 2007 and later. Supports
  8. // complex components by high compatibility, and provided streaming API for
  9. // generating or reading data from a worksheet with huge amounts of data. This
  10. // library needs Go version 1.10 or later.
  11. package excelize
  12. // SheetPrOption is an option of a view of a worksheet. See SetSheetPrOptions().
  13. type SheetPrOption interface {
  14. setSheetPrOption(view *xlsxSheetPr)
  15. }
  16. // SheetPrOptionPtr is a writable SheetPrOption. See GetSheetPrOptions().
  17. type SheetPrOptionPtr interface {
  18. SheetPrOption
  19. getSheetPrOption(view *xlsxSheetPr)
  20. }
  21. type (
  22. // CodeName is a SheetPrOption
  23. CodeName string
  24. // EnableFormatConditionsCalculation is a SheetPrOption
  25. EnableFormatConditionsCalculation bool
  26. // Published is a SheetPrOption
  27. Published bool
  28. // FitToPage is a SheetPrOption
  29. FitToPage bool
  30. // AutoPageBreaks is a SheetPrOption
  31. AutoPageBreaks bool
  32. // OutlineSummaryBelow is an outlinePr, within SheetPr option
  33. OutlineSummaryBelow bool
  34. )
  35. // setSheetPrOption implements the SheetPrOption interface.
  36. func (o OutlineSummaryBelow) setSheetPrOption(pr *xlsxSheetPr) {
  37. if pr.OutlinePr == nil {
  38. pr.OutlinePr = new(xlsxOutlinePr)
  39. }
  40. pr.OutlinePr.SummaryBelow = bool(o)
  41. }
  42. // getSheetPrOption implements the SheetPrOptionPtr interface.
  43. func (o *OutlineSummaryBelow) getSheetPrOption(pr *xlsxSheetPr) {
  44. // Excel default: true
  45. if pr == nil || pr.OutlinePr == nil {
  46. *o = true
  47. return
  48. }
  49. *o = OutlineSummaryBelow(defaultTrue(&pr.OutlinePr.SummaryBelow))
  50. }
  51. // setSheetPrOption implements the SheetPrOption interface and specifies a
  52. // stable name of the sheet.
  53. func (o CodeName) setSheetPrOption(pr *xlsxSheetPr) {
  54. pr.CodeName = string(o)
  55. }
  56. // getSheetPrOption implements the SheetPrOptionPtr interface and get the
  57. // stable name of the sheet.
  58. func (o *CodeName) getSheetPrOption(pr *xlsxSheetPr) {
  59. if pr == nil {
  60. *o = ""
  61. return
  62. }
  63. *o = CodeName(pr.CodeName)
  64. }
  65. // setSheetPrOption implements the SheetPrOption interface and flag indicating
  66. // whether the conditional formatting calculations shall be evaluated.
  67. func (o EnableFormatConditionsCalculation) setSheetPrOption(pr *xlsxSheetPr) {
  68. pr.EnableFormatConditionsCalculation = boolPtr(bool(o))
  69. }
  70. // getSheetPrOption implements the SheetPrOptionPtr interface and get the
  71. // settings of whether the conditional formatting calculations shall be
  72. // evaluated.
  73. func (o *EnableFormatConditionsCalculation) getSheetPrOption(pr *xlsxSheetPr) {
  74. if pr == nil {
  75. *o = true
  76. return
  77. }
  78. *o = EnableFormatConditionsCalculation(defaultTrue(pr.EnableFormatConditionsCalculation))
  79. }
  80. // setSheetPrOption implements the SheetPrOption interface and flag indicating
  81. // whether the worksheet is published.
  82. func (o Published) setSheetPrOption(pr *xlsxSheetPr) {
  83. pr.Published = boolPtr(bool(o))
  84. }
  85. // getSheetPrOption implements the SheetPrOptionPtr interface and get the
  86. // settings of whether the worksheet is published.
  87. func (o *Published) getSheetPrOption(pr *xlsxSheetPr) {
  88. if pr == nil {
  89. *o = true
  90. return
  91. }
  92. *o = Published(defaultTrue(pr.Published))
  93. }
  94. // setSheetPrOption implements the SheetPrOption interface.
  95. func (o FitToPage) setSheetPrOption(pr *xlsxSheetPr) {
  96. if pr.PageSetUpPr == nil {
  97. if !o {
  98. return
  99. }
  100. pr.PageSetUpPr = new(xlsxPageSetUpPr)
  101. }
  102. pr.PageSetUpPr.FitToPage = bool(o)
  103. }
  104. // getSheetPrOption implements the SheetPrOptionPtr interface.
  105. func (o *FitToPage) getSheetPrOption(pr *xlsxSheetPr) {
  106. // Excel default: false
  107. if pr == nil || pr.PageSetUpPr == nil {
  108. *o = false
  109. return
  110. }
  111. *o = FitToPage(pr.PageSetUpPr.FitToPage)
  112. }
  113. // setSheetPrOption implements the SheetPrOption interface.
  114. func (o AutoPageBreaks) setSheetPrOption(pr *xlsxSheetPr) {
  115. if pr.PageSetUpPr == nil {
  116. if !o {
  117. return
  118. }
  119. pr.PageSetUpPr = new(xlsxPageSetUpPr)
  120. }
  121. pr.PageSetUpPr.AutoPageBreaks = bool(o)
  122. }
  123. // getSheetPrOption implements the SheetPrOptionPtr interface.
  124. func (o *AutoPageBreaks) getSheetPrOption(pr *xlsxSheetPr) {
  125. // Excel default: false
  126. if pr == nil || pr.PageSetUpPr == nil {
  127. *o = false
  128. return
  129. }
  130. *o = AutoPageBreaks(pr.PageSetUpPr.AutoPageBreaks)
  131. }
  132. // SetSheetPrOptions provides a function to sets 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) SetSheetPrOptions(name string, opts ...SheetPrOption) error {
  142. sheet, err := f.workSheetReader(name)
  143. if err != nil {
  144. return err
  145. }
  146. pr := sheet.SheetPr
  147. if pr == nil {
  148. pr = new(xlsxSheetPr)
  149. sheet.SheetPr = pr
  150. }
  151. for _, opt := range opts {
  152. opt.setSheetPrOption(pr)
  153. }
  154. return err
  155. }
  156. // GetSheetPrOptions provides a function to gets worksheet properties.
  157. //
  158. // Available options:
  159. // CodeName(string)
  160. // EnableFormatConditionsCalculation(bool)
  161. // Published(bool)
  162. // FitToPage(bool)
  163. // AutoPageBreaks(bool)
  164. // OutlineSummaryBelow(bool)
  165. func (f *File) GetSheetPrOptions(name string, opts ...SheetPrOptionPtr) error {
  166. sheet, err := f.workSheetReader(name)
  167. if err != nil {
  168. return err
  169. }
  170. pr := sheet.SheetPr
  171. for _, opt := range opts {
  172. opt.getSheetPrOption(pr)
  173. }
  174. return err
  175. }
  176. type (
  177. // PageMarginBottom specifies the bottom margin for the page.
  178. PageMarginBottom float64
  179. // PageMarginFooter specifies the footer margin for the page.
  180. PageMarginFooter float64
  181. // PageMarginHeader specifies the header margin for the page.
  182. PageMarginHeader float64
  183. // PageMarginLeft specifies the left margin for the page.
  184. PageMarginLeft float64
  185. // PageMarginRight specifies the right margin for the page.
  186. PageMarginRight float64
  187. // PageMarginTop specifies the top margin for the page.
  188. PageMarginTop float64
  189. )
  190. // setPageMargins provides a method to set the bottom margin for the worksheet.
  191. func (p PageMarginBottom) setPageMargins(pm *xlsxPageMargins) {
  192. pm.Bottom = float64(p)
  193. }
  194. // setPageMargins provides a method to get the bottom margin for the worksheet.
  195. func (p *PageMarginBottom) getPageMargins(pm *xlsxPageMargins) {
  196. // Excel default: 0.75
  197. if pm == nil || pm.Bottom == 0 {
  198. *p = 0.75
  199. return
  200. }
  201. *p = PageMarginBottom(pm.Bottom)
  202. }
  203. // setPageMargins provides a method to set the footer margin for the worksheet.
  204. func (p PageMarginFooter) setPageMargins(pm *xlsxPageMargins) {
  205. pm.Footer = float64(p)
  206. }
  207. // setPageMargins provides a method to get the footer margin for the worksheet.
  208. func (p *PageMarginFooter) getPageMargins(pm *xlsxPageMargins) {
  209. // Excel default: 0.3
  210. if pm == nil || pm.Footer == 0 {
  211. *p = 0.3
  212. return
  213. }
  214. *p = PageMarginFooter(pm.Footer)
  215. }
  216. // setPageMargins provides a method to set the header margin for the worksheet.
  217. func (p PageMarginHeader) setPageMargins(pm *xlsxPageMargins) {
  218. pm.Header = float64(p)
  219. }
  220. // setPageMargins provides a method to get the header margin for the worksheet.
  221. func (p *PageMarginHeader) getPageMargins(pm *xlsxPageMargins) {
  222. // Excel default: 0.3
  223. if pm == nil || pm.Header == 0 {
  224. *p = 0.3
  225. return
  226. }
  227. *p = PageMarginHeader(pm.Header)
  228. }
  229. // setPageMargins provides a method to set the left margin for the worksheet.
  230. func (p PageMarginLeft) setPageMargins(pm *xlsxPageMargins) {
  231. pm.Left = float64(p)
  232. }
  233. // setPageMargins provides a method to get the left margin for the worksheet.
  234. func (p *PageMarginLeft) getPageMargins(pm *xlsxPageMargins) {
  235. // Excel default: 0.7
  236. if pm == nil || pm.Left == 0 {
  237. *p = 0.7
  238. return
  239. }
  240. *p = PageMarginLeft(pm.Left)
  241. }
  242. // setPageMargins provides a method to set the right margin for the worksheet.
  243. func (p PageMarginRight) setPageMargins(pm *xlsxPageMargins) {
  244. pm.Right = float64(p)
  245. }
  246. // setPageMargins provides a method to get the right margin for the worksheet.
  247. func (p *PageMarginRight) getPageMargins(pm *xlsxPageMargins) {
  248. // Excel default: 0.7
  249. if pm == nil || pm.Right == 0 {
  250. *p = 0.7
  251. return
  252. }
  253. *p = PageMarginRight(pm.Right)
  254. }
  255. // setPageMargins provides a method to set the top margin for the worksheet.
  256. func (p PageMarginTop) setPageMargins(pm *xlsxPageMargins) {
  257. pm.Top = float64(p)
  258. }
  259. // setPageMargins provides a method to get the top margin for the worksheet.
  260. func (p *PageMarginTop) getPageMargins(pm *xlsxPageMargins) {
  261. // Excel default: 0.75
  262. if pm == nil || pm.Top == 0 {
  263. *p = 0.75
  264. return
  265. }
  266. *p = PageMarginTop(pm.Top)
  267. }
  268. // PageMarginsOptions is an option of a page margin of a worksheet. See
  269. // SetPageMargins().
  270. type PageMarginsOptions interface {
  271. setPageMargins(layout *xlsxPageMargins)
  272. }
  273. // PageMarginsOptionsPtr is a writable PageMarginsOptions. See
  274. // GetPageMargins().
  275. type PageMarginsOptionsPtr interface {
  276. PageMarginsOptions
  277. getPageMargins(layout *xlsxPageMargins)
  278. }
  279. // SetPageMargins provides a function to set worksheet page margins.
  280. //
  281. // Available options:
  282. // PageMarginBottom(float64)
  283. // PageMarginFooter(float64)
  284. // PageMarginHeader(float64)
  285. // PageMarginLeft(float64)
  286. // PageMarginRight(float64)
  287. // PageMarginTop(float64)
  288. func (f *File) SetPageMargins(sheet string, opts ...PageMarginsOptions) error {
  289. s, err := f.workSheetReader(sheet)
  290. if err != nil {
  291. return err
  292. }
  293. pm := s.PageMargins
  294. if pm == nil {
  295. pm = new(xlsxPageMargins)
  296. s.PageMargins = pm
  297. }
  298. for _, opt := range opts {
  299. opt.setPageMargins(pm)
  300. }
  301. return err
  302. }
  303. // GetPageMargins provides a function to get worksheet page margins.
  304. //
  305. // Available options:
  306. // PageMarginBottom(float64)
  307. // PageMarginFooter(float64)
  308. // PageMarginHeader(float64)
  309. // PageMarginLeft(float64)
  310. // PageMarginRight(float64)
  311. // PageMarginTop(float64)
  312. func (f *File) GetPageMargins(sheet string, opts ...PageMarginsOptionsPtr) error {
  313. s, err := f.workSheetReader(sheet)
  314. if err != nil {
  315. return err
  316. }
  317. pm := s.PageMargins
  318. for _, opt := range opts {
  319. opt.getPageMargins(pm)
  320. }
  321. return err
  322. }
  323. // SheetFormatPrOptions is an option of the formatting properties of a
  324. // worksheet. See SetSheetFormatPr().
  325. type SheetFormatPrOptions interface {
  326. setSheetFormatPr(formatPr *xlsxSheetFormatPr)
  327. }
  328. // SheetFormatPrOptionsPtr is a writable SheetFormatPrOptions. See
  329. // GetSheetFormatPr().
  330. type SheetFormatPrOptionsPtr interface {
  331. SheetFormatPrOptions
  332. getSheetFormatPr(formatPr *xlsxSheetFormatPr)
  333. }
  334. type (
  335. // BaseColWidth specifies the number of characters of the maximum digit width
  336. // of the normal style's font. This value does not include margin padding or
  337. // extra padding for gridlines. It is only the number of characters.
  338. BaseColWidth uint8
  339. // DefaultColWidth specifies the default column width measured as the number
  340. // of characters of the maximum digit width of the normal style's font.
  341. DefaultColWidth float64
  342. // DefaultRowHeight specifies the default row height measured in point size.
  343. // Optimization so we don't have to write the height on all rows. This can be
  344. // written out if most rows have custom height, to achieve the optimization.
  345. DefaultRowHeight float64
  346. // CustomHeight specifies the custom height.
  347. CustomHeight bool
  348. // ZeroHeight specifies if rows are hidden.
  349. ZeroHeight bool
  350. // ThickTop specifies if rows have a thick top border by default.
  351. ThickTop bool
  352. // ThickBottom specifies if rows have a thick bottom border by default.
  353. ThickBottom bool
  354. )
  355. // setSheetFormatPr provides a method to set the number of characters of the
  356. // maximum digit width of the normal style's font.
  357. func (p BaseColWidth) setSheetFormatPr(fp *xlsxSheetFormatPr) {
  358. fp.BaseColWidth = uint8(p)
  359. }
  360. // setSheetFormatPr provides a method to set the number of characters of the
  361. // maximum digit width of the normal style's font.
  362. func (p *BaseColWidth) getSheetFormatPr(fp *xlsxSheetFormatPr) {
  363. if fp == nil {
  364. *p = 0
  365. return
  366. }
  367. *p = BaseColWidth(fp.BaseColWidth)
  368. }
  369. // setSheetFormatPr provides a method to set the default column width measured
  370. // as the number of characters of the maximum digit width of the normal
  371. // style's font.
  372. func (p DefaultColWidth) setSheetFormatPr(fp *xlsxSheetFormatPr) {
  373. fp.DefaultColWidth = float64(p)
  374. }
  375. // getSheetFormatPr provides a method to get the default column width measured
  376. // as the number of characters of the maximum digit width of the normal
  377. // style's font.
  378. func (p *DefaultColWidth) getSheetFormatPr(fp *xlsxSheetFormatPr) {
  379. if fp == nil {
  380. *p = 0
  381. return
  382. }
  383. *p = DefaultColWidth(fp.DefaultColWidth)
  384. }
  385. // setSheetFormatPr provides a method to set the default row height measured
  386. // in point size.
  387. func (p DefaultRowHeight) setSheetFormatPr(fp *xlsxSheetFormatPr) {
  388. fp.DefaultRowHeight = float64(p)
  389. }
  390. // getSheetFormatPr provides a method to get the default row height measured
  391. // in point size.
  392. func (p *DefaultRowHeight) getSheetFormatPr(fp *xlsxSheetFormatPr) {
  393. if fp == nil {
  394. *p = 15
  395. return
  396. }
  397. *p = DefaultRowHeight(fp.DefaultRowHeight)
  398. }
  399. // setSheetFormatPr provides a method to set the custom height.
  400. func (p CustomHeight) setSheetFormatPr(fp *xlsxSheetFormatPr) {
  401. fp.CustomHeight = bool(p)
  402. }
  403. // getSheetFormatPr provides a method to get the custom height.
  404. func (p *CustomHeight) getSheetFormatPr(fp *xlsxSheetFormatPr) {
  405. if fp == nil {
  406. *p = false
  407. return
  408. }
  409. *p = CustomHeight(fp.CustomHeight)
  410. }
  411. // setSheetFormatPr provides a method to set if rows are hidden.
  412. func (p ZeroHeight) setSheetFormatPr(fp *xlsxSheetFormatPr) {
  413. fp.ZeroHeight = bool(p)
  414. }
  415. // getSheetFormatPr provides a method to get if rows are hidden.
  416. func (p *ZeroHeight) getSheetFormatPr(fp *xlsxSheetFormatPr) {
  417. if fp == nil {
  418. *p = false
  419. return
  420. }
  421. *p = ZeroHeight(fp.ZeroHeight)
  422. }
  423. // setSheetFormatPr provides a method to set if rows have a thick top border
  424. // by default.
  425. func (p ThickTop) setSheetFormatPr(fp *xlsxSheetFormatPr) {
  426. fp.ThickTop = bool(p)
  427. }
  428. // getSheetFormatPr provides a method to get if rows have a thick top border
  429. // by default.
  430. func (p *ThickTop) getSheetFormatPr(fp *xlsxSheetFormatPr) {
  431. if fp == nil {
  432. *p = false
  433. return
  434. }
  435. *p = ThickTop(fp.ThickTop)
  436. }
  437. // setSheetFormatPr provides a method to set if rows have a thick bottom
  438. // border by default.
  439. func (p ThickBottom) setSheetFormatPr(fp *xlsxSheetFormatPr) {
  440. fp.ThickBottom = bool(p)
  441. }
  442. // setSheetFormatPr provides a method to set if rows have a thick bottom
  443. // border by default.
  444. func (p *ThickBottom) getSheetFormatPr(fp *xlsxSheetFormatPr) {
  445. if fp == nil {
  446. *p = false
  447. return
  448. }
  449. *p = ThickBottom(fp.ThickBottom)
  450. }
  451. // SetSheetFormatPr provides a function to set worksheet formatting properties.
  452. //
  453. // Available options:
  454. // BaseColWidth(uint8)
  455. // DefaultColWidth(float64)
  456. // DefaultRowHeight(float64)
  457. // CustomHeight(bool)
  458. // ZeroHeight(bool)
  459. // ThickTop(bool)
  460. // ThickBottom(bool)
  461. func (f *File) SetSheetFormatPr(sheet string, opts ...SheetFormatPrOptions) error {
  462. s, err := f.workSheetReader(sheet)
  463. if err != nil {
  464. return err
  465. }
  466. fp := s.SheetFormatPr
  467. if fp == nil {
  468. fp = new(xlsxSheetFormatPr)
  469. s.SheetFormatPr = fp
  470. }
  471. for _, opt := range opts {
  472. opt.setSheetFormatPr(fp)
  473. }
  474. return err
  475. }
  476. // GetSheetFormatPr provides a function to get worksheet formatting properties.
  477. //
  478. // Available options:
  479. // BaseColWidth(uint8)
  480. // DefaultColWidth(float64)
  481. // DefaultRowHeight(float64)
  482. // CustomHeight(bool)
  483. // ZeroHeight(bool)
  484. // ThickTop(bool)
  485. // ThickBottom(bool)
  486. func (f *File) GetSheetFormatPr(sheet string, opts ...SheetFormatPrOptionsPtr) error {
  487. s, err := f.workSheetReader(sheet)
  488. if err != nil {
  489. return err
  490. }
  491. fp := s.SheetFormatPr
  492. for _, opt := range opts {
  493. opt.getSheetFormatPr(fp)
  494. }
  495. return err
  496. }