sheetpr_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. package excelize
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/mohae/deepcopy"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. var _ = []SheetPrOption{
  9. CodeName("hello"),
  10. EnableFormatConditionsCalculation(false),
  11. Published(false),
  12. FitToPage(true),
  13. TabColor("#FFFF00"),
  14. AutoPageBreaks(true),
  15. OutlineSummaryBelow(true),
  16. }
  17. var _ = []SheetPrOptionPtr{
  18. (*CodeName)(nil),
  19. (*EnableFormatConditionsCalculation)(nil),
  20. (*Published)(nil),
  21. (*FitToPage)(nil),
  22. (*TabColor)(nil),
  23. (*AutoPageBreaks)(nil),
  24. (*OutlineSummaryBelow)(nil),
  25. }
  26. func ExampleFile_SetSheetPrOptions() {
  27. f := NewFile()
  28. const sheet = "Sheet1"
  29. if err := f.SetSheetPrOptions(sheet,
  30. CodeName("code"),
  31. EnableFormatConditionsCalculation(false),
  32. Published(false),
  33. FitToPage(true),
  34. TabColor("#FFFF00"),
  35. AutoPageBreaks(true),
  36. OutlineSummaryBelow(false),
  37. ); err != nil {
  38. fmt.Println(err)
  39. }
  40. // Output:
  41. }
  42. func ExampleFile_GetSheetPrOptions() {
  43. f := NewFile()
  44. const sheet = "Sheet1"
  45. var (
  46. codeName CodeName
  47. enableFormatConditionsCalculation EnableFormatConditionsCalculation
  48. published Published
  49. fitToPage FitToPage
  50. tabColor TabColor
  51. autoPageBreaks AutoPageBreaks
  52. outlineSummaryBelow OutlineSummaryBelow
  53. )
  54. if err := f.GetSheetPrOptions(sheet,
  55. &codeName,
  56. &enableFormatConditionsCalculation,
  57. &published,
  58. &fitToPage,
  59. &tabColor,
  60. &autoPageBreaks,
  61. &outlineSummaryBelow,
  62. ); err != nil {
  63. fmt.Println(err)
  64. }
  65. fmt.Println("Defaults:")
  66. fmt.Printf("- codeName: %q\n", codeName)
  67. fmt.Println("- enableFormatConditionsCalculation:", enableFormatConditionsCalculation)
  68. fmt.Println("- published:", published)
  69. fmt.Println("- fitToPage:", fitToPage)
  70. fmt.Printf("- tabColor: %q\n", tabColor)
  71. fmt.Println("- autoPageBreaks:", autoPageBreaks)
  72. fmt.Println("- outlineSummaryBelow:", outlineSummaryBelow)
  73. // Output:
  74. // Defaults:
  75. // - codeName: ""
  76. // - enableFormatConditionsCalculation: true
  77. // - published: true
  78. // - fitToPage: false
  79. // - tabColor: ""
  80. // - autoPageBreaks: false
  81. // - outlineSummaryBelow: true
  82. }
  83. func TestSheetPrOptions(t *testing.T) {
  84. const sheet = "Sheet1"
  85. testData := []struct {
  86. container SheetPrOptionPtr
  87. nonDefault SheetPrOption
  88. }{
  89. {new(CodeName), CodeName("xx")},
  90. {new(EnableFormatConditionsCalculation), EnableFormatConditionsCalculation(false)},
  91. {new(Published), Published(false)},
  92. {new(FitToPage), FitToPage(true)},
  93. {new(TabColor), TabColor("FFFF00")},
  94. {new(AutoPageBreaks), AutoPageBreaks(true)},
  95. {new(OutlineSummaryBelow), OutlineSummaryBelow(false)},
  96. }
  97. for i, test := range testData {
  98. t.Run(fmt.Sprintf("TestData%d", i), func(t *testing.T) {
  99. opt := test.nonDefault
  100. t.Logf("option %T", opt)
  101. def := deepcopy.Copy(test.container).(SheetPrOptionPtr)
  102. val1 := deepcopy.Copy(def).(SheetPrOptionPtr)
  103. val2 := deepcopy.Copy(def).(SheetPrOptionPtr)
  104. f := NewFile()
  105. // Get the default value
  106. assert.NoError(t, f.GetSheetPrOptions(sheet, def), opt)
  107. // Get again and check
  108. assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
  109. if !assert.Equal(t, val1, def, opt) {
  110. t.FailNow()
  111. }
  112. // Set the same value
  113. assert.NoError(t, f.SetSheetPrOptions(sheet, val1), opt)
  114. // Get again and check
  115. assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
  116. if !assert.Equal(t, val1, def, "%T: value should not have changed", opt) {
  117. t.FailNow()
  118. }
  119. // Set a different value
  120. assert.NoError(t, f.SetSheetPrOptions(sheet, test.nonDefault), opt)
  121. assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
  122. // Get again and compare
  123. assert.NoError(t, f.GetSheetPrOptions(sheet, val2), opt)
  124. if !assert.Equal(t, val1, val2, "%T: value should not have changed", opt) {
  125. t.FailNow()
  126. }
  127. // Value should not be the same as the default
  128. if !assert.NotEqual(t, def, val1, "%T: value should have changed from default", opt) {
  129. t.FailNow()
  130. }
  131. // Restore the default value
  132. assert.NoError(t, f.SetSheetPrOptions(sheet, def), opt)
  133. assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
  134. if !assert.Equal(t, def, val1) {
  135. t.FailNow()
  136. }
  137. })
  138. }
  139. }
  140. func TestSetSheetrOptions(t *testing.T) {
  141. f := NewFile()
  142. assert.NoError(t, f.SetSheetPrOptions("Sheet1", TabColor("")))
  143. // Test SetSheetrOptions on not exists worksheet.
  144. assert.EqualError(t, f.SetSheetPrOptions("SheetN"), "sheet SheetN is not exist")
  145. }
  146. func TestGetSheetPrOptions(t *testing.T) {
  147. f := NewFile()
  148. // Test GetSheetPrOptions on not exists worksheet.
  149. assert.EqualError(t, f.GetSheetPrOptions("SheetN"), "sheet SheetN is not exist")
  150. }
  151. var _ = []PageMarginsOptions{
  152. PageMarginBottom(1.0),
  153. PageMarginFooter(1.0),
  154. PageMarginHeader(1.0),
  155. PageMarginLeft(1.0),
  156. PageMarginRight(1.0),
  157. PageMarginTop(1.0),
  158. }
  159. var _ = []PageMarginsOptionsPtr{
  160. (*PageMarginBottom)(nil),
  161. (*PageMarginFooter)(nil),
  162. (*PageMarginHeader)(nil),
  163. (*PageMarginLeft)(nil),
  164. (*PageMarginRight)(nil),
  165. (*PageMarginTop)(nil),
  166. }
  167. func ExampleFile_SetPageMargins() {
  168. f := NewFile()
  169. const sheet = "Sheet1"
  170. if err := f.SetPageMargins(sheet,
  171. PageMarginBottom(1.0),
  172. PageMarginFooter(1.0),
  173. PageMarginHeader(1.0),
  174. PageMarginLeft(1.0),
  175. PageMarginRight(1.0),
  176. PageMarginTop(1.0),
  177. ); err != nil {
  178. fmt.Println(err)
  179. }
  180. // Output:
  181. }
  182. func ExampleFile_GetPageMargins() {
  183. f := NewFile()
  184. const sheet = "Sheet1"
  185. var (
  186. marginBottom PageMarginBottom
  187. marginFooter PageMarginFooter
  188. marginHeader PageMarginHeader
  189. marginLeft PageMarginLeft
  190. marginRight PageMarginRight
  191. marginTop PageMarginTop
  192. )
  193. if err := f.GetPageMargins(sheet,
  194. &marginBottom,
  195. &marginFooter,
  196. &marginHeader,
  197. &marginLeft,
  198. &marginRight,
  199. &marginTop,
  200. ); err != nil {
  201. fmt.Println(err)
  202. }
  203. fmt.Println("Defaults:")
  204. fmt.Println("- marginBottom:", marginBottom)
  205. fmt.Println("- marginFooter:", marginFooter)
  206. fmt.Println("- marginHeader:", marginHeader)
  207. fmt.Println("- marginLeft:", marginLeft)
  208. fmt.Println("- marginRight:", marginRight)
  209. fmt.Println("- marginTop:", marginTop)
  210. // Output:
  211. // Defaults:
  212. // - marginBottom: 0.75
  213. // - marginFooter: 0.3
  214. // - marginHeader: 0.3
  215. // - marginLeft: 0.7
  216. // - marginRight: 0.7
  217. // - marginTop: 0.75
  218. }
  219. func TestPageMarginsOption(t *testing.T) {
  220. const sheet = "Sheet1"
  221. testData := []struct {
  222. container PageMarginsOptionsPtr
  223. nonDefault PageMarginsOptions
  224. }{
  225. {new(PageMarginTop), PageMarginTop(1.0)},
  226. {new(PageMarginBottom), PageMarginBottom(1.0)},
  227. {new(PageMarginLeft), PageMarginLeft(1.0)},
  228. {new(PageMarginRight), PageMarginRight(1.0)},
  229. {new(PageMarginHeader), PageMarginHeader(1.0)},
  230. {new(PageMarginFooter), PageMarginFooter(1.0)},
  231. }
  232. for i, test := range testData {
  233. t.Run(fmt.Sprintf("TestData%d", i), func(t *testing.T) {
  234. opt := test.nonDefault
  235. t.Logf("option %T", opt)
  236. def := deepcopy.Copy(test.container).(PageMarginsOptionsPtr)
  237. val1 := deepcopy.Copy(def).(PageMarginsOptionsPtr)
  238. val2 := deepcopy.Copy(def).(PageMarginsOptionsPtr)
  239. f := NewFile()
  240. // Get the default value
  241. assert.NoError(t, f.GetPageMargins(sheet, def), opt)
  242. // Get again and check
  243. assert.NoError(t, f.GetPageMargins(sheet, val1), opt)
  244. if !assert.Equal(t, val1, def, opt) {
  245. t.FailNow()
  246. }
  247. // Set the same value
  248. assert.NoError(t, f.SetPageMargins(sheet, val1), opt)
  249. // Get again and check
  250. assert.NoError(t, f.GetPageMargins(sheet, val1), opt)
  251. if !assert.Equal(t, val1, def, "%T: value should not have changed", opt) {
  252. t.FailNow()
  253. }
  254. // Set a different value
  255. assert.NoError(t, f.SetPageMargins(sheet, test.nonDefault), opt)
  256. assert.NoError(t, f.GetPageMargins(sheet, val1), opt)
  257. // Get again and compare
  258. assert.NoError(t, f.GetPageMargins(sheet, val2), opt)
  259. if !assert.Equal(t, val1, val2, "%T: value should not have changed", opt) {
  260. t.FailNow()
  261. }
  262. // Value should not be the same as the default
  263. if !assert.NotEqual(t, def, val1, "%T: value should have changed from default", opt) {
  264. t.FailNow()
  265. }
  266. // Restore the default value
  267. assert.NoError(t, f.SetPageMargins(sheet, def), opt)
  268. assert.NoError(t, f.GetPageMargins(sheet, val1), opt)
  269. if !assert.Equal(t, def, val1) {
  270. t.FailNow()
  271. }
  272. })
  273. }
  274. }
  275. func TestSetPageMargins(t *testing.T) {
  276. f := NewFile()
  277. // Test set page margins on not exists worksheet.
  278. assert.EqualError(t, f.SetPageMargins("SheetN"), "sheet SheetN is not exist")
  279. }
  280. func TestGetPageMargins(t *testing.T) {
  281. f := NewFile()
  282. // Test get page margins on not exists worksheet.
  283. assert.EqualError(t, f.GetPageMargins("SheetN"), "sheet SheetN is not exist")
  284. }
  285. func ExampleFile_SetSheetFormatPr() {
  286. f := NewFile()
  287. const sheet = "Sheet1"
  288. if err := f.SetSheetFormatPr(sheet,
  289. BaseColWidth(1.0),
  290. DefaultColWidth(1.0),
  291. DefaultRowHeight(1.0),
  292. CustomHeight(true),
  293. ZeroHeight(true),
  294. ThickTop(true),
  295. ThickBottom(true),
  296. ); err != nil {
  297. fmt.Println(err)
  298. }
  299. // Output:
  300. }
  301. func ExampleFile_GetSheetFormatPr() {
  302. f := NewFile()
  303. const sheet = "Sheet1"
  304. var (
  305. baseColWidth BaseColWidth
  306. defaultColWidth DefaultColWidth
  307. defaultRowHeight DefaultRowHeight
  308. customHeight CustomHeight
  309. zeroHeight ZeroHeight
  310. thickTop ThickTop
  311. thickBottom ThickBottom
  312. )
  313. if err := f.GetSheetFormatPr(sheet,
  314. &baseColWidth,
  315. &defaultColWidth,
  316. &defaultRowHeight,
  317. &customHeight,
  318. &zeroHeight,
  319. &thickTop,
  320. &thickBottom,
  321. ); err != nil {
  322. fmt.Println(err)
  323. }
  324. fmt.Println("Defaults:")
  325. fmt.Println("- baseColWidth:", baseColWidth)
  326. fmt.Println("- defaultColWidth:", defaultColWidth)
  327. fmt.Println("- defaultRowHeight:", defaultRowHeight)
  328. fmt.Println("- customHeight:", customHeight)
  329. fmt.Println("- zeroHeight:", zeroHeight)
  330. fmt.Println("- thickTop:", thickTop)
  331. fmt.Println("- thickBottom:", thickBottom)
  332. // Output:
  333. // Defaults:
  334. // - baseColWidth: 0
  335. // - defaultColWidth: 0
  336. // - defaultRowHeight: 15
  337. // - customHeight: false
  338. // - zeroHeight: false
  339. // - thickTop: false
  340. // - thickBottom: false
  341. }
  342. func TestSheetFormatPrOptions(t *testing.T) {
  343. const sheet = "Sheet1"
  344. testData := []struct {
  345. container SheetFormatPrOptionsPtr
  346. nonDefault SheetFormatPrOptions
  347. }{
  348. {new(BaseColWidth), BaseColWidth(1.0)},
  349. {new(DefaultColWidth), DefaultColWidth(1.0)},
  350. {new(DefaultRowHeight), DefaultRowHeight(1.0)},
  351. {new(CustomHeight), CustomHeight(true)},
  352. {new(ZeroHeight), ZeroHeight(true)},
  353. {new(ThickTop), ThickTop(true)},
  354. {new(ThickBottom), ThickBottom(true)},
  355. }
  356. for i, test := range testData {
  357. t.Run(fmt.Sprintf("TestData%d", i), func(t *testing.T) {
  358. opt := test.nonDefault
  359. t.Logf("option %T", opt)
  360. def := deepcopy.Copy(test.container).(SheetFormatPrOptionsPtr)
  361. val1 := deepcopy.Copy(def).(SheetFormatPrOptionsPtr)
  362. val2 := deepcopy.Copy(def).(SheetFormatPrOptionsPtr)
  363. f := NewFile()
  364. // Get the default value
  365. assert.NoError(t, f.GetSheetFormatPr(sheet, def), opt)
  366. // Get again and check
  367. assert.NoError(t, f.GetSheetFormatPr(sheet, val1), opt)
  368. if !assert.Equal(t, val1, def, opt) {
  369. t.FailNow()
  370. }
  371. // Set the same value
  372. assert.NoError(t, f.SetSheetFormatPr(sheet, val1), opt)
  373. // Get again and check
  374. assert.NoError(t, f.GetSheetFormatPr(sheet, val1), opt)
  375. if !assert.Equal(t, val1, def, "%T: value should not have changed", opt) {
  376. t.FailNow()
  377. }
  378. // Set a different value
  379. assert.NoError(t, f.SetSheetFormatPr(sheet, test.nonDefault), opt)
  380. assert.NoError(t, f.GetSheetFormatPr(sheet, val1), opt)
  381. // Get again and compare
  382. assert.NoError(t, f.GetSheetFormatPr(sheet, val2), opt)
  383. if !assert.Equal(t, val1, val2, "%T: value should not have changed", opt) {
  384. t.FailNow()
  385. }
  386. // Value should not be the same as the default
  387. if !assert.NotEqual(t, def, val1, "%T: value should have changed from default", opt) {
  388. t.FailNow()
  389. }
  390. // Restore the default value
  391. assert.NoError(t, f.SetSheetFormatPr(sheet, def), opt)
  392. assert.NoError(t, f.GetSheetFormatPr(sheet, val1), opt)
  393. if !assert.Equal(t, def, val1) {
  394. t.FailNow()
  395. }
  396. })
  397. }
  398. }
  399. func TestSetSheetFormatPr(t *testing.T) {
  400. f := NewFile()
  401. assert.NoError(t, f.GetSheetFormatPr("Sheet1"))
  402. f.Sheet["xl/worksheets/sheet1.xml"].SheetFormatPr = nil
  403. assert.NoError(t, f.SetSheetFormatPr("Sheet1", BaseColWidth(1.0)))
  404. // Test set formatting properties on not exists worksheet.
  405. assert.EqualError(t, f.SetSheetFormatPr("SheetN"), "sheet SheetN is not exist")
  406. }
  407. func TestGetSheetFormatPr(t *testing.T) {
  408. f := NewFile()
  409. assert.NoError(t, f.GetSheetFormatPr("Sheet1"))
  410. f.Sheet["xl/worksheets/sheet1.xml"].SheetFormatPr = nil
  411. var (
  412. baseColWidth BaseColWidth
  413. defaultColWidth DefaultColWidth
  414. defaultRowHeight DefaultRowHeight
  415. customHeight CustomHeight
  416. zeroHeight ZeroHeight
  417. thickTop ThickTop
  418. thickBottom ThickBottom
  419. )
  420. assert.NoError(t, f.GetSheetFormatPr("Sheet1",
  421. &baseColWidth,
  422. &defaultColWidth,
  423. &defaultRowHeight,
  424. &customHeight,
  425. &zeroHeight,
  426. &thickTop,
  427. &thickBottom,
  428. ))
  429. // Test get formatting properties on not exists worksheet.
  430. assert.EqualError(t, f.GetSheetFormatPr("SheetN"), "sheet SheetN is not exist")
  431. }