You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.7KB

  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.10 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. f := 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. f.AddDataValidation("Sheet1", dvRange)
  25. if !assert.NoError(t, f.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. f.AddDataValidation("Sheet1", dvRange)
  33. if !assert.NoError(t, f.SaveAs(resultFile)) {
  34. t.FailNow()
  35. }
  36. dvRange = NewDataValidation(true)
  37. dvRange.Sqref = "A5:B6"
  38. dvRange.SetDropList([]string{"1", "2", "3"})
  39. f.AddDataValidation("Sheet1", dvRange)
  40. if !assert.NoError(t, f.SaveAs(resultFile)) {
  41. t.FailNow()
  42. }
  43. }
  44. func TestDataValidationError(t *testing.T) {
  45. resultFile := filepath.Join("test", "TestDataValidationError.xlsx")
  46. f := NewFile()
  47. f.SetCellStr("Sheet1", "E1", "E1")
  48. f.SetCellStr("Sheet1", "E2", "E2")
  49. f.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. f.AddDataValidation("Sheet1", dvRange)
  57. if !assert.NoError(t, f.SaveAs(resultFile)) {
  58. t.FailNow()
  59. }
  60. dvRange = NewDataValidation(true)
  61. err = dvRange.SetDropList(make([]string, 258))
  62. if dvRange.Formula1 != "" {
  63. t.Errorf("data validation error. Formula1 must be empty!")
  64. return
  65. }
  66. assert.EqualError(t, err, "data validation must be 0-255 characters")
  67. dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan)
  68. dvRange.SetSqref("A9:B10")
  69. f.AddDataValidation("Sheet1", dvRange)
  70. if !assert.NoError(t, f.SaveAs(resultFile)) {
  71. t.FailNow()
  72. }
  73. }