You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

310 lines
9.0KB

  1. package excelize_test
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/mohae/deepcopy"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/360EntSecGroup-Skylar/excelize/v2"
  8. )
  9. var _ = []excelize.SheetPrOption{
  10. excelize.CodeName("hello"),
  11. excelize.EnableFormatConditionsCalculation(false),
  12. excelize.Published(false),
  13. excelize.FitToPage(true),
  14. excelize.AutoPageBreaks(true),
  15. excelize.OutlineSummaryBelow(true),
  16. }
  17. var _ = []excelize.SheetPrOptionPtr{
  18. (*excelize.CodeName)(nil),
  19. (*excelize.EnableFormatConditionsCalculation)(nil),
  20. (*excelize.Published)(nil),
  21. (*excelize.FitToPage)(nil),
  22. (*excelize.AutoPageBreaks)(nil),
  23. (*excelize.OutlineSummaryBelow)(nil),
  24. }
  25. func ExampleFile_SetSheetPrOptions() {
  26. f := excelize.NewFile()
  27. const sheet = "Sheet1"
  28. if err := f.SetSheetPrOptions(sheet,
  29. excelize.CodeName("code"),
  30. excelize.EnableFormatConditionsCalculation(false),
  31. excelize.Published(false),
  32. excelize.FitToPage(true),
  33. excelize.AutoPageBreaks(true),
  34. excelize.OutlineSummaryBelow(false),
  35. ); err != nil {
  36. panic(err)
  37. }
  38. // Output:
  39. }
  40. func ExampleFile_GetSheetPrOptions() {
  41. f := excelize.NewFile()
  42. const sheet = "Sheet1"
  43. var (
  44. codeName excelize.CodeName
  45. enableFormatConditionsCalculation excelize.EnableFormatConditionsCalculation
  46. published excelize.Published
  47. fitToPage excelize.FitToPage
  48. autoPageBreaks excelize.AutoPageBreaks
  49. outlineSummaryBelow excelize.OutlineSummaryBelow
  50. )
  51. if err := f.GetSheetPrOptions(sheet,
  52. &codeName,
  53. &enableFormatConditionsCalculation,
  54. &published,
  55. &fitToPage,
  56. &autoPageBreaks,
  57. &outlineSummaryBelow,
  58. ); err != nil {
  59. panic(err)
  60. }
  61. fmt.Println("Defaults:")
  62. fmt.Printf("- codeName: %q\n", codeName)
  63. fmt.Println("- enableFormatConditionsCalculation:", enableFormatConditionsCalculation)
  64. fmt.Println("- published:", published)
  65. fmt.Println("- fitToPage:", fitToPage)
  66. fmt.Println("- autoPageBreaks:", autoPageBreaks)
  67. fmt.Println("- outlineSummaryBelow:", outlineSummaryBelow)
  68. // Output:
  69. // Defaults:
  70. // - codeName: ""
  71. // - enableFormatConditionsCalculation: true
  72. // - published: true
  73. // - fitToPage: false
  74. // - autoPageBreaks: false
  75. // - outlineSummaryBelow: true
  76. }
  77. func TestSheetPrOptions(t *testing.T) {
  78. const sheet = "Sheet1"
  79. testData := []struct {
  80. container excelize.SheetPrOptionPtr
  81. nonDefault excelize.SheetPrOption
  82. }{
  83. {new(excelize.CodeName), excelize.CodeName("xx")},
  84. {new(excelize.EnableFormatConditionsCalculation), excelize.EnableFormatConditionsCalculation(false)},
  85. {new(excelize.Published), excelize.Published(false)},
  86. {new(excelize.FitToPage), excelize.FitToPage(true)},
  87. {new(excelize.AutoPageBreaks), excelize.AutoPageBreaks(true)},
  88. {new(excelize.OutlineSummaryBelow), excelize.OutlineSummaryBelow(false)},
  89. }
  90. for i, test := range testData {
  91. t.Run(fmt.Sprintf("TestData%d", i), func(t *testing.T) {
  92. opt := test.nonDefault
  93. t.Logf("option %T", opt)
  94. def := deepcopy.Copy(test.container).(excelize.SheetPrOptionPtr)
  95. val1 := deepcopy.Copy(def).(excelize.SheetPrOptionPtr)
  96. val2 := deepcopy.Copy(def).(excelize.SheetPrOptionPtr)
  97. f := excelize.NewFile()
  98. // Get the default value
  99. assert.NoError(t, f.GetSheetPrOptions(sheet, def), opt)
  100. // Get again and check
  101. assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
  102. if !assert.Equal(t, val1, def, opt) {
  103. t.FailNow()
  104. }
  105. // Set the same value
  106. assert.NoError(t, f.SetSheetPrOptions(sheet, val1), opt)
  107. // Get again and check
  108. assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
  109. if !assert.Equal(t, val1, def, "%T: value should not have changed", opt) {
  110. t.FailNow()
  111. }
  112. // Set a different value
  113. assert.NoError(t, f.SetSheetPrOptions(sheet, test.nonDefault), opt)
  114. assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
  115. // Get again and compare
  116. assert.NoError(t, f.GetSheetPrOptions(sheet, val2), opt)
  117. if !assert.Equal(t, val1, val2, "%T: value should not have changed", opt) {
  118. t.FailNow()
  119. }
  120. // Value should not be the same as the default
  121. if !assert.NotEqual(t, def, val1, "%T: value should have changed from default", opt) {
  122. t.FailNow()
  123. }
  124. // Restore the default value
  125. assert.NoError(t, f.SetSheetPrOptions(sheet, def), opt)
  126. assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
  127. if !assert.Equal(t, def, val1) {
  128. t.FailNow()
  129. }
  130. })
  131. }
  132. }
  133. func TestSetSheetrOptions(t *testing.T) {
  134. f := excelize.NewFile()
  135. // Test SetSheetrOptions on not exists worksheet.
  136. assert.EqualError(t, f.SetSheetPrOptions("SheetN"), "sheet SheetN is not exist")
  137. }
  138. func TestGetSheetPrOptions(t *testing.T) {
  139. f := excelize.NewFile()
  140. // Test GetSheetPrOptions on not exists worksheet.
  141. assert.EqualError(t, f.GetSheetPrOptions("SheetN"), "sheet SheetN is not exist")
  142. }
  143. var _ = []excelize.PageMarginsOptions{
  144. excelize.PageMarginBottom(1.0),
  145. excelize.PageMarginFooter(1.0),
  146. excelize.PageMarginHeader(1.0),
  147. excelize.PageMarginLeft(1.0),
  148. excelize.PageMarginRight(1.0),
  149. excelize.PageMarginTop(1.0),
  150. }
  151. var _ = []excelize.PageMarginsOptionsPtr{
  152. (*excelize.PageMarginBottom)(nil),
  153. (*excelize.PageMarginFooter)(nil),
  154. (*excelize.PageMarginHeader)(nil),
  155. (*excelize.PageMarginLeft)(nil),
  156. (*excelize.PageMarginRight)(nil),
  157. (*excelize.PageMarginTop)(nil),
  158. }
  159. func ExampleFile_SetPageMargins() {
  160. f := excelize.NewFile()
  161. const sheet = "Sheet1"
  162. if err := f.SetPageMargins(sheet,
  163. excelize.PageMarginBottom(1.0),
  164. excelize.PageMarginFooter(1.0),
  165. excelize.PageMarginHeader(1.0),
  166. excelize.PageMarginLeft(1.0),
  167. excelize.PageMarginRight(1.0),
  168. excelize.PageMarginTop(1.0),
  169. ); err != nil {
  170. panic(err)
  171. }
  172. // Output:
  173. }
  174. func ExampleFile_GetPageMargins() {
  175. f := excelize.NewFile()
  176. const sheet = "Sheet1"
  177. var (
  178. marginBottom excelize.PageMarginBottom
  179. marginFooter excelize.PageMarginFooter
  180. marginHeader excelize.PageMarginHeader
  181. marginLeft excelize.PageMarginLeft
  182. marginRight excelize.PageMarginRight
  183. marginTop excelize.PageMarginTop
  184. )
  185. if err := f.GetPageMargins(sheet,
  186. &marginBottom,
  187. &marginFooter,
  188. &marginHeader,
  189. &marginLeft,
  190. &marginRight,
  191. &marginTop,
  192. ); err != nil {
  193. panic(err)
  194. }
  195. fmt.Println("Defaults:")
  196. fmt.Println("- marginBottom:", marginBottom)
  197. fmt.Println("- marginFooter:", marginFooter)
  198. fmt.Println("- marginHeader:", marginHeader)
  199. fmt.Println("- marginLeft:", marginLeft)
  200. fmt.Println("- marginRight:", marginRight)
  201. fmt.Println("- marginTop:", marginTop)
  202. // Output:
  203. // Defaults:
  204. // - marginBottom: 0.75
  205. // - marginFooter: 0.3
  206. // - marginHeader: 0.3
  207. // - marginLeft: 0.7
  208. // - marginRight: 0.7
  209. // - marginTop: 0.75
  210. }
  211. func TestPageMarginsOption(t *testing.T) {
  212. const sheet = "Sheet1"
  213. testData := []struct {
  214. container excelize.PageMarginsOptionsPtr
  215. nonDefault excelize.PageMarginsOptions
  216. }{
  217. {new(excelize.PageMarginTop), excelize.PageMarginTop(1.0)},
  218. {new(excelize.PageMarginBottom), excelize.PageMarginBottom(1.0)},
  219. {new(excelize.PageMarginLeft), excelize.PageMarginLeft(1.0)},
  220. {new(excelize.PageMarginRight), excelize.PageMarginRight(1.0)},
  221. {new(excelize.PageMarginHeader), excelize.PageMarginHeader(1.0)},
  222. {new(excelize.PageMarginFooter), excelize.PageMarginFooter(1.0)},
  223. }
  224. for i, test := range testData {
  225. t.Run(fmt.Sprintf("TestData%d", i), func(t *testing.T) {
  226. opt := test.nonDefault
  227. t.Logf("option %T", opt)
  228. def := deepcopy.Copy(test.container).(excelize.PageMarginsOptionsPtr)
  229. val1 := deepcopy.Copy(def).(excelize.PageMarginsOptionsPtr)
  230. val2 := deepcopy.Copy(def).(excelize.PageMarginsOptionsPtr)
  231. f := excelize.NewFile()
  232. // Get the default value
  233. assert.NoError(t, f.GetPageMargins(sheet, def), opt)
  234. // Get again and check
  235. assert.NoError(t, f.GetPageMargins(sheet, val1), opt)
  236. if !assert.Equal(t, val1, def, opt) {
  237. t.FailNow()
  238. }
  239. // Set the same value
  240. assert.NoError(t, f.SetPageMargins(sheet, val1), opt)
  241. // Get again and check
  242. assert.NoError(t, f.GetPageMargins(sheet, val1), opt)
  243. if !assert.Equal(t, val1, def, "%T: value should not have changed", opt) {
  244. t.FailNow()
  245. }
  246. // Set a different value
  247. assert.NoError(t, f.SetPageMargins(sheet, test.nonDefault), opt)
  248. assert.NoError(t, f.GetPageMargins(sheet, val1), opt)
  249. // Get again and compare
  250. assert.NoError(t, f.GetPageMargins(sheet, val2), opt)
  251. if !assert.Equal(t, val1, val2, "%T: value should not have changed", opt) {
  252. t.FailNow()
  253. }
  254. // Value should not be the same as the default
  255. if !assert.NotEqual(t, def, val1, "%T: value should have changed from default", opt) {
  256. t.FailNow()
  257. }
  258. // Restore the default value
  259. assert.NoError(t, f.SetPageMargins(sheet, def), opt)
  260. assert.NoError(t, f.GetPageMargins(sheet, val1), opt)
  261. if !assert.Equal(t, def, val1) {
  262. t.FailNow()
  263. }
  264. })
  265. }
  266. }
  267. func TestSetPageMargins(t *testing.T) {
  268. f := excelize.NewFile()
  269. // Test set page margins on not exists worksheet.
  270. assert.EqualError(t, f.SetPageMargins("SheetN"), "sheet SheetN is not exist")
  271. }
  272. func TestGetPageMargins(t *testing.T) {
  273. f := excelize.NewFile()
  274. // Test get page margins on not exists worksheet.
  275. assert.EqualError(t, f.GetPageMargins("SheetN"), "sheet SheetN is not exist")
  276. }