sheetpr.go 16 KB

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