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.

185 lines
4.5KB

  1. package excelize_test
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/360EntSecGroup-Skylar/excelize/v2"
  7. )
  8. var _ = []excelize.SheetViewOption{
  9. excelize.DefaultGridColor(true),
  10. excelize.RightToLeft(false),
  11. excelize.ShowFormulas(false),
  12. excelize.ShowGridLines(true),
  13. excelize.ShowRowColHeaders(true),
  14. excelize.TopLeftCell("B2"),
  15. // SheetViewOptionPtr are also SheetViewOption
  16. new(excelize.DefaultGridColor),
  17. new(excelize.RightToLeft),
  18. new(excelize.ShowFormulas),
  19. new(excelize.ShowGridLines),
  20. new(excelize.ShowRowColHeaders),
  21. new(excelize.TopLeftCell),
  22. }
  23. var _ = []excelize.SheetViewOptionPtr{
  24. (*excelize.DefaultGridColor)(nil),
  25. (*excelize.RightToLeft)(nil),
  26. (*excelize.ShowFormulas)(nil),
  27. (*excelize.ShowGridLines)(nil),
  28. (*excelize.ShowRowColHeaders)(nil),
  29. (*excelize.TopLeftCell)(nil),
  30. }
  31. func ExampleFile_SetSheetViewOptions() {
  32. f := excelize.NewFile()
  33. const sheet = "Sheet1"
  34. if err := f.SetSheetViewOptions(sheet, 0,
  35. excelize.DefaultGridColor(false),
  36. excelize.RightToLeft(false),
  37. excelize.ShowFormulas(true),
  38. excelize.ShowGridLines(true),
  39. excelize.ShowRowColHeaders(true),
  40. excelize.ZoomScale(80),
  41. excelize.TopLeftCell("C3"),
  42. ); err != nil {
  43. panic(err)
  44. }
  45. var zoomScale excelize.ZoomScale
  46. fmt.Println("Default:")
  47. fmt.Println("- zoomScale: 80")
  48. if err := f.SetSheetViewOptions(sheet, 0, excelize.ZoomScale(500)); err != nil {
  49. panic(err)
  50. }
  51. if err := f.GetSheetViewOptions(sheet, 0, &zoomScale); err != nil {
  52. panic(err)
  53. }
  54. fmt.Println("Used out of range value:")
  55. fmt.Println("- zoomScale:", zoomScale)
  56. if err := f.SetSheetViewOptions(sheet, 0, excelize.ZoomScale(123)); err != nil {
  57. panic(err)
  58. }
  59. if err := f.GetSheetViewOptions(sheet, 0, &zoomScale); err != nil {
  60. panic(err)
  61. }
  62. fmt.Println("Used correct value:")
  63. fmt.Println("- zoomScale:", zoomScale)
  64. // Output:
  65. // Default:
  66. // - zoomScale: 80
  67. // Used out of range value:
  68. // - zoomScale: 80
  69. // Used correct value:
  70. // - zoomScale: 123
  71. }
  72. func ExampleFile_GetSheetViewOptions() {
  73. f := excelize.NewFile()
  74. const sheet = "Sheet1"
  75. var (
  76. defaultGridColor excelize.DefaultGridColor
  77. rightToLeft excelize.RightToLeft
  78. showFormulas excelize.ShowFormulas
  79. showGridLines excelize.ShowGridLines
  80. showZeros excelize.ShowZeros
  81. showRowColHeaders excelize.ShowRowColHeaders
  82. zoomScale excelize.ZoomScale
  83. topLeftCell excelize.TopLeftCell
  84. )
  85. if err := f.GetSheetViewOptions(sheet, 0,
  86. &defaultGridColor,
  87. &rightToLeft,
  88. &showFormulas,
  89. &showGridLines,
  90. &showZeros,
  91. &showRowColHeaders,
  92. &zoomScale,
  93. &topLeftCell,
  94. ); err != nil {
  95. panic(err)
  96. }
  97. fmt.Println("Default:")
  98. fmt.Println("- defaultGridColor:", defaultGridColor)
  99. fmt.Println("- rightToLeft:", rightToLeft)
  100. fmt.Println("- showFormulas:", showFormulas)
  101. fmt.Println("- showGridLines:", showGridLines)
  102. fmt.Println("- showZeros:", showZeros)
  103. fmt.Println("- showRowColHeaders:", showRowColHeaders)
  104. fmt.Println("- zoomScale:", zoomScale)
  105. fmt.Println("- topLeftCell:", `"`+topLeftCell+`"`)
  106. if err := f.SetSheetViewOptions(sheet, 0, excelize.TopLeftCell("B2")); err != nil {
  107. panic(err)
  108. }
  109. if err := f.GetSheetViewOptions(sheet, 0, &topLeftCell); err != nil {
  110. panic(err)
  111. }
  112. if err := f.SetSheetViewOptions(sheet, 0, excelize.ShowGridLines(false)); err != nil {
  113. panic(err)
  114. }
  115. if err := f.GetSheetViewOptions(sheet, 0, &showGridLines); err != nil {
  116. panic(err)
  117. }
  118. if err := f.SetSheetViewOptions(sheet, 0, excelize.ShowZeros(false)); err != nil {
  119. panic(err)
  120. }
  121. if err := f.GetSheetViewOptions(sheet, 0, &showZeros); err != nil {
  122. panic(err)
  123. }
  124. fmt.Println("After change:")
  125. fmt.Println("- showGridLines:", showGridLines)
  126. fmt.Println("- showZeros:", showZeros)
  127. fmt.Println("- topLeftCell:", topLeftCell)
  128. // Output:
  129. // Default:
  130. // - defaultGridColor: true
  131. // - rightToLeft: false
  132. // - showFormulas: false
  133. // - showGridLines: true
  134. // - showZeros: true
  135. // - showRowColHeaders: true
  136. // - zoomScale: 0
  137. // - topLeftCell: ""
  138. // After change:
  139. // - showGridLines: false
  140. // - showZeros: false
  141. // - topLeftCell: B2
  142. }
  143. func TestSheetViewOptionsErrors(t *testing.T) {
  144. f := excelize.NewFile()
  145. const sheet = "Sheet1"
  146. assert.NoError(t, f.GetSheetViewOptions(sheet, 0))
  147. assert.NoError(t, f.GetSheetViewOptions(sheet, -1))
  148. assert.Error(t, f.GetSheetViewOptions(sheet, 1))
  149. assert.Error(t, f.GetSheetViewOptions(sheet, -2))
  150. assert.NoError(t, f.SetSheetViewOptions(sheet, 0))
  151. assert.NoError(t, f.SetSheetViewOptions(sheet, -1))
  152. assert.Error(t, f.SetSheetViewOptions(sheet, 1))
  153. assert.Error(t, f.SetSheetViewOptions(sheet, -2))
  154. }