datavalidation_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of
  2. // this source code is governed by a BSD-style license that can be found in
  3. // the LICENSE file.
  4. //
  5. // Package excelize providing a set of functions that allow you to write to
  6. // and read from XLSX files. Support reads and writes XLSX file generated by
  7. // Microsoft Excel™ 2007 and later. Support save file without losing original
  8. // charts of XLSX. This library needs Go version 1.8 or later.
  9. package excelize
  10. import (
  11. "path/filepath"
  12. "testing"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func TestDataValidation(t *testing.T) {
  16. resultFile := filepath.Join("test", "TestDataValidation.xlsx")
  17. xlsx := NewFile()
  18. dvRange := NewDataValidation(true)
  19. dvRange.Sqref = "A1:B2"
  20. dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorBetween)
  21. dvRange.SetError(DataValidationErrorStyleStop, "error title", "error body")
  22. dvRange.SetError(DataValidationErrorStyleWarning, "error title", "error body")
  23. dvRange.SetError(DataValidationErrorStyleInformation, "error title", "error body")
  24. xlsx.AddDataValidation("Sheet1", dvRange)
  25. if !assert.NoError(t, xlsx.SaveAs(resultFile)) {
  26. t.FailNow()
  27. }
  28. dvRange = NewDataValidation(true)
  29. dvRange.Sqref = "A3:B4"
  30. dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan)
  31. dvRange.SetInput("input title", "input body")
  32. xlsx.AddDataValidation("Sheet1", dvRange)
  33. if !assert.NoError(t, xlsx.SaveAs(resultFile)) {
  34. t.FailNow()
  35. }
  36. dvRange = NewDataValidation(true)
  37. dvRange.Sqref = "A5:B6"
  38. dvRange.SetDropList([]string{"1", "2", "3"})
  39. xlsx.AddDataValidation("Sheet1", dvRange)
  40. if !assert.NoError(t, xlsx.SaveAs(resultFile)) {
  41. t.FailNow()
  42. }
  43. }
  44. func TestDataValidationError(t *testing.T) {
  45. resultFile := filepath.Join("test", "TestDataValidationError.xlsx")
  46. xlsx := NewFile()
  47. xlsx.SetCellStr("Sheet1", "E1", "E1")
  48. xlsx.SetCellStr("Sheet1", "E2", "E2")
  49. xlsx.SetCellStr("Sheet1", "E3", "E3")
  50. dvRange := NewDataValidation(true)
  51. dvRange.SetSqref("A7:B8")
  52. dvRange.SetSqref("A7:B8")
  53. dvRange.SetSqrefDropList("$E$1:$E$3", true)
  54. err := dvRange.SetSqrefDropList("$E$1:$E$3", false)
  55. assert.EqualError(t, err, "cross-sheet sqref cell are not supported")
  56. xlsx.AddDataValidation("Sheet1", dvRange)
  57. if !assert.NoError(t, xlsx.SaveAs(resultFile)) {
  58. t.FailNow()
  59. }
  60. dvRange = NewDataValidation(true)
  61. dvRange.SetDropList(make([]string, 258))
  62. err = dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan)
  63. assert.EqualError(t, err, "data validation must be 0-255 characters")
  64. xlsx.AddDataValidation("Sheet1", dvRange)
  65. if !assert.NoError(t, xlsx.SaveAs(resultFile)) {
  66. t.FailNow()
  67. }
  68. }