datavalidation_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Package excelize providing a set of functions that allow you to write to
  2. // and read from XLSX files. Support reads and writes XLSX file generated by
  3. // Microsoft Excel™ 2007 and later. Support save file without losing original
  4. // charts of XLSX. This library needs Go version 1.8 or later.
  5. //
  6. // Copyright 2016 - 2018 The excelize Authors. All rights reserved. Use of
  7. // this source code is governed by a BSD-style license that can be found in
  8. // the LICENSE file.
  9. package excelize
  10. import (
  11. "testing"
  12. )
  13. func TestDataValidation(t *testing.T) {
  14. xlsx := NewFile()
  15. dvRange := NewDataValidation(true)
  16. dvRange.Sqref = "A1:B2"
  17. dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorBetween)
  18. dvRange.SetError(DataValidationErrorStyleStop, "error title", "error body")
  19. xlsx.AddDataValidation("Sheet1", dvRange)
  20. dvRange = NewDataValidation(true)
  21. dvRange.Sqref = "A3:B4"
  22. dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan)
  23. dvRange.SetInput("input title", "input body")
  24. xlsx.AddDataValidation("Sheet1", dvRange)
  25. dvRange = NewDataValidation(true)
  26. dvRange.Sqref = "A5:B6"
  27. dvRange.SetDropList([]string{"1", "2", "3"})
  28. xlsx.AddDataValidation("Sheet1", dvRange)
  29. xlsx.SetCellStr("Sheet1", "E1", "E1")
  30. xlsx.SetCellStr("Sheet1", "E2", "E2")
  31. xlsx.SetCellStr("Sheet1", "E3", "E3")
  32. dvRange = NewDataValidation(true)
  33. dvRange.Sqref = "A7:B8"
  34. dvRange.SetSqrefDropList("$E$1:$E$3", true)
  35. xlsx.AddDataValidation("Sheet1", dvRange)
  36. // Test write file to given path.
  37. err := xlsx.SaveAs("./test/Book_data_validation.xlsx")
  38. if err != nil {
  39. t.Error(err)
  40. }
  41. }