data_validation_test.go 8.0 KB

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