data_validation_test.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package xlsx
  2. import (
  3. "bytes"
  4. "fmt"
  5. "testing"
  6. qt "github.com/frankban/quicktest"
  7. )
  8. func TestDataValidation(t *testing.T) {
  9. var file *File
  10. var sheet *Sheet
  11. var row *Row
  12. var cell *Cell
  13. var err error
  14. var title = "cell"
  15. var msg = "cell msg"
  16. c := qt.New(t)
  17. file = NewFile()
  18. sheet, err = file.AddSheet("Sheet1")
  19. if err != nil {
  20. fmt.Printf(err.Error())
  21. }
  22. row = sheet.AddRow()
  23. cell = row.AddCell()
  24. cell.Value = "a1"
  25. dd := NewDataValidation(0, 0, 0, 0, true)
  26. err = dd.SetDropList([]string{"a1", "a2", "a3"})
  27. c.Assert(err, qt.IsNil)
  28. dd.SetInput(&title, &msg)
  29. cell.SetDataValidation(dd)
  30. dd = NewDataValidation(2, 0, 2, 0, true)
  31. err = dd.SetDropList([]string{"c1", "c2", "c3"})
  32. c.Assert(err, qt.IsNil)
  33. title = "col c"
  34. dd.SetInput(&title, &msg)
  35. sheet.AddDataValidation(dd)
  36. dd = NewDataValidation(3, 3, 3, 7, true)
  37. err = dd.SetDropList([]string{"d", "d1", "d2"})
  38. c.Assert(err, qt.IsNil)
  39. title = "col d range"
  40. dd.SetInput(&title, &msg)
  41. sheet.AddDataValidation(dd)
  42. dd = NewDataValidation(4, 1, 4, Excel2006MaxRowIndex, true)
  43. err = dd.SetDropList([]string{"e1", "e2", "e3"})
  44. c.Assert(err, qt.IsNil)
  45. title = "col e start 3"
  46. dd.SetInput(&title, &msg)
  47. sheet.AddDataValidation(dd)
  48. index := 5
  49. rowIndex := 1
  50. dd = NewDataValidation(rowIndex, index, rowIndex, index, true)
  51. err = dd.SetRange(15, 4, DataValidationTypeTextLeng, DataValidationOperatorBetween)
  52. c.Assert(err, qt.IsNil)
  53. sheet.AddDataValidation(dd)
  54. index++
  55. dd = NewDataValidation(rowIndex, index, rowIndex, index, true)
  56. err = dd.SetRange(10, 1, DataValidationTypeTextLeng, DataValidationOperatorEqual)
  57. c.Assert(err, qt.IsNil)
  58. sheet.AddDataValidation(dd)
  59. index++
  60. dd = NewDataValidation(rowIndex, index, rowIndex, index, true)
  61. err = dd.SetRange(10, 1, DataValidationTypeTextLeng, DataValidationOperatorGreaterThanOrEqual)
  62. c.Assert(err, qt.IsNil)
  63. sheet.AddDataValidation(dd)
  64. index++
  65. dd = NewDataValidation(rowIndex, index, rowIndex, index, true)
  66. err = dd.SetRange(10, 1, DataValidationTypeTextLeng, DataValidationOperatorGreaterThan)
  67. c.Assert(err, qt.IsNil)
  68. sheet.AddDataValidation(dd)
  69. index++
  70. dd = NewDataValidation(rowIndex, index, rowIndex, index, true)
  71. err = dd.SetRange(10, 1, DataValidationTypeTextLeng, DataValidationOperatorLessThan)
  72. c.Assert(err, qt.IsNil)
  73. sheet.AddDataValidation(dd)
  74. index++
  75. dd = NewDataValidation(rowIndex, index, rowIndex, index, true)
  76. err = dd.SetRange(10, 1, DataValidationTypeTextLeng, DataValidationOperatorLessThanOrEqual)
  77. c.Assert(err, qt.IsNil)
  78. sheet.AddDataValidation(dd)
  79. index++
  80. dd = NewDataValidation(rowIndex, index, rowIndex, index, true)
  81. err = dd.SetRange(10, 1, DataValidationTypeTextLeng, DataValidationOperatorNotEqual)
  82. c.Assert(err, qt.IsNil)
  83. sheet.AddDataValidation(dd)
  84. index++
  85. dd = NewDataValidation(rowIndex, index, rowIndex, index, true)
  86. err = dd.SetRange(10, 1, DataValidationTypeTextLeng, DataValidationOperatorNotBetween)
  87. c.Assert(err, qt.IsNil)
  88. sheet.AddDataValidation(dd)
  89. index++
  90. rowIndex++
  91. index = 5
  92. dd = NewDataValidation(rowIndex, index, rowIndex, index, true)
  93. err = dd.SetRange(4, 15, DataValidationTypeWhole, DataValidationOperatorBetween)
  94. c.Assert(err, qt.IsNil)
  95. sheet.AddDataValidation(dd)
  96. index++
  97. dd = NewDataValidation(rowIndex, index, rowIndex, index, true)
  98. err = dd.SetRange(10, 1, DataValidationTypeWhole, DataValidationOperatorEqual)
  99. c.Assert(err, qt.IsNil)
  100. sheet.AddDataValidation(dd)
  101. index++
  102. dd = NewDataValidation(rowIndex, index, rowIndex, index, true)
  103. err = dd.SetRange(10, 1, DataValidationTypeWhole, DataValidationOperatorGreaterThanOrEqual)
  104. c.Assert(err, qt.IsNil)
  105. sheet.AddDataValidation(dd)
  106. index++
  107. dd = NewDataValidation(rowIndex, index, rowIndex, index, true)
  108. err = dd.SetRange(10, 1, DataValidationTypeWhole, DataValidationOperatorGreaterThan)
  109. c.Assert(err, qt.IsNil)
  110. sheet.AddDataValidation(dd)
  111. index++
  112. dd = NewDataValidation(rowIndex, index, rowIndex, index, true)
  113. err = dd.SetRange(10, 1, DataValidationTypeWhole, DataValidationOperatorLessThan)
  114. c.Assert(err, qt.IsNil)
  115. sheet.AddDataValidation(dd)
  116. index++
  117. dd = NewDataValidation(rowIndex, index, rowIndex, index, true)
  118. err = dd.SetRange(10, 1, DataValidationTypeWhole, DataValidationOperatorLessThanOrEqual)
  119. c.Assert(err, qt.IsNil)
  120. sheet.AddDataValidation(dd)
  121. index++
  122. dd = NewDataValidation(rowIndex, index, rowIndex, index, true)
  123. err = dd.SetRange(10, 1, DataValidationTypeWhole, DataValidationOperatorNotEqual)
  124. c.Assert(err, qt.IsNil)
  125. sheet.AddDataValidation(dd)
  126. index++
  127. dd = NewDataValidation(rowIndex, index, rowIndex, index, true)
  128. err = dd.SetRange(10, 50, DataValidationTypeWhole, DataValidationOperatorNotBetween)
  129. if err != nil {
  130. t.Fatal(err)
  131. }
  132. sheet.AddDataValidation(dd)
  133. index++
  134. dd = NewDataValidation(12, 2, 12, 10, true)
  135. err = dd.SetDropList([]string{"1", "2", "4"})
  136. c.Assert(err, qt.IsNil)
  137. dd1 := NewDataValidation(12, 3, 12, 4, true)
  138. err = dd1.SetDropList([]string{"11", "22", "44"})
  139. c.Assert(err, qt.IsNil)
  140. dd2 := NewDataValidation(12, 5, 12, 7, true)
  141. err = dd2.SetDropList([]string{"111", "222", "444"})
  142. c.Assert(err, qt.IsNil)
  143. sheet.AddDataValidation(dd)
  144. sheet.AddDataValidation(dd1)
  145. sheet.AddDataValidation(dd2)
  146. dd = NewDataValidation(13, 2, 13, 10, true)
  147. err = dd.SetDropList([]string{"1", "2", "4"})
  148. c.Assert(err, qt.IsNil)
  149. dd1 = NewDataValidation(13, 1, 13, 2, true)
  150. err = dd1.SetDropList([]string{"11", "22", "44"})
  151. c.Assert(err, qt.IsNil)
  152. sheet.AddDataValidation(dd)
  153. sheet.AddDataValidation(dd1)
  154. dd = NewDataValidation(14, 2, 14, 10, true)
  155. err = dd.SetDropList([]string{"1", "2", "4"})
  156. c.Assert(err, qt.IsNil)
  157. dd1 = NewDataValidation(14, 1, 14, 5, true)
  158. err = dd1.SetDropList([]string{"11", "22", "44"})
  159. c.Assert(err, qt.IsNil)
  160. sheet.AddDataValidation(dd)
  161. sheet.AddDataValidation(dd1)
  162. dd = NewDataValidation(15, 2, 15, 10, true)
  163. err = dd.SetDropList([]string{"1", "2", "4"})
  164. if err != nil {
  165. t.Fatal(err)
  166. }
  167. dd1 = NewDataValidation(15, 1, 15, 10, true)
  168. err = dd1.SetDropList([]string{"11", "22", "44"})
  169. c.Assert(err, qt.IsNil)
  170. sheet.AddDataValidation(dd)
  171. sheet.AddDataValidation(dd1)
  172. dd = NewDataValidation(16, 10, 16, 20, true)
  173. err = dd.SetDropList([]string{"1", "2", "4"})
  174. c.Assert(err, qt.IsNil)
  175. dd1 = NewDataValidation(16, 2, 16, 4, true)
  176. err = dd1.SetDropList([]string{"11", "22", "44"})
  177. c.Assert(err, qt.IsNil)
  178. dd2 = NewDataValidation(16, 12, 16, 30, true)
  179. err = dd2.SetDropList([]string{"111", "222", "444"})
  180. c.Assert(err, qt.IsNil)
  181. sheet.AddDataValidation(dd)
  182. sheet.AddDataValidation(dd1)
  183. sheet.AddDataValidation(dd2)
  184. dd = NewDataValidation(3, 3, 3, Excel2006MaxRowIndex, true)
  185. err = dd.SetDropList([]string{"d", "d1", "d2"})
  186. c.Assert(err, qt.IsNil)
  187. title = "col d range"
  188. dd.SetInput(&title, &msg)
  189. sheet.AddDataValidation(dd)
  190. dd = NewDataValidation(3, 4, 3, Excel2006MaxRowIndex, true)
  191. err = dd.SetDropList([]string{"d", "d1", "d2"})
  192. c.Assert(err, qt.IsNil)
  193. title = "col d range"
  194. dd.SetInput(&title, &msg)
  195. sheet.AddDataValidation(dd)
  196. dest := &bytes.Buffer{}
  197. err = file.Write(dest)
  198. c.Assert(err, qt.IsNil)
  199. // Read and write the file that was just saved.
  200. file, err = OpenBinary(dest.Bytes())
  201. c.Assert(err, qt.IsNil)
  202. dest = &bytes.Buffer{}
  203. err = file.Write(dest)
  204. c.Assert(err, qt.IsNil)
  205. }
  206. func TestDataValidation2(t *testing.T) {
  207. c := qt.New(t)
  208. // Show error and show info start disabled, but automatically get enabled when setting a message
  209. dd := NewDataValidation(0, 0, 0, 0, true)
  210. c.Assert(dd.ShowErrorMessage, qt.Equals, false)
  211. c.Assert(dd.ShowInputMessage, qt.Equals, false)
  212. str := "you got an error"
  213. dd.SetError(StyleStop, &str, &str)
  214. c.Assert(dd.ShowErrorMessage, qt.Equals, true)
  215. c.Assert(dd.ShowInputMessage, qt.Equals, false)
  216. str = "hello"
  217. dd.SetInput(&str, &str)
  218. c.Assert(dd.ShowInputMessage, qt.Equals, true)
  219. // Check the formula created by this function
  220. // The sheet name needs single quotes, the single quote in the name gets escaped,
  221. // and all references are fixed.
  222. err := dd.SetInFileList("Sheet ' 2", 2, 1, 3, 10)
  223. c.Assert(err, qt.IsNil)
  224. expectedFormula := "'Sheet '' 2'!$C$2:$D$11"
  225. c.Assert(dd.Formula1, qt.Equals, expectedFormula)
  226. c.Assert(dd.Type, qt.Equals, "list")
  227. }