sheetview_test.go 4.1 KB

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