2019-01-01 13:20:14 +08:00
|
|
|
// Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of
|
2018-09-14 00:44:23 +08:00
|
|
|
// this source code is governed by a BSD-style license that can be found in
|
|
|
|
// the LICENSE file.
|
|
|
|
//
|
|
|
|
// Package excelize providing a set of functions that allow you to write to
|
|
|
|
// and read from XLSX files. Support reads and writes XLSX file generated by
|
|
|
|
// Microsoft Excel™ 2007 and later. Support save file without losing original
|
2019-08-11 00:36:14 +08:00
|
|
|
// charts of XLSX. This library needs Go version 1.10 or later.
|
2018-09-14 00:58:48 +08:00
|
|
|
|
2018-09-01 19:38:30 +08:00
|
|
|
package excelize
|
|
|
|
|
2018-12-27 18:51:44 +08:00
|
|
|
import (
|
2018-12-27 22:28:28 +08:00
|
|
|
"path/filepath"
|
2018-12-27 18:51:44 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
2018-09-01 19:38:30 +08:00
|
|
|
|
|
|
|
func TestDataValidation(t *testing.T) {
|
2018-12-27 22:28:28 +08:00
|
|
|
resultFile := filepath.Join("test", "TestDataValidation.xlsx")
|
2018-12-27 18:51:44 +08:00
|
|
|
|
2019-05-05 16:25:57 +08:00
|
|
|
f := NewFile()
|
2018-09-01 19:38:30 +08:00
|
|
|
|
|
|
|
dvRange := NewDataValidation(true)
|
|
|
|
dvRange.Sqref = "A1:B2"
|
|
|
|
dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorBetween)
|
|
|
|
dvRange.SetError(DataValidationErrorStyleStop, "error title", "error body")
|
2018-09-13 10:38:01 +08:00
|
|
|
dvRange.SetError(DataValidationErrorStyleWarning, "error title", "error body")
|
|
|
|
dvRange.SetError(DataValidationErrorStyleInformation, "error title", "error body")
|
2019-05-05 16:25:57 +08:00
|
|
|
f.AddDataValidation("Sheet1", dvRange)
|
|
|
|
if !assert.NoError(t, f.SaveAs(resultFile)) {
|
2018-12-27 18:51:44 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
2018-09-01 19:38:30 +08:00
|
|
|
|
|
|
|
dvRange = NewDataValidation(true)
|
|
|
|
dvRange.Sqref = "A3:B4"
|
|
|
|
dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan)
|
|
|
|
dvRange.SetInput("input title", "input body")
|
2019-05-05 16:25:57 +08:00
|
|
|
f.AddDataValidation("Sheet1", dvRange)
|
|
|
|
if !assert.NoError(t, f.SaveAs(resultFile)) {
|
2018-12-27 18:51:44 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
2018-09-01 19:38:30 +08:00
|
|
|
|
|
|
|
dvRange = NewDataValidation(true)
|
|
|
|
dvRange.Sqref = "A5:B6"
|
|
|
|
dvRange.SetDropList([]string{"1", "2", "3"})
|
2019-05-05 16:25:57 +08:00
|
|
|
f.AddDataValidation("Sheet1", dvRange)
|
|
|
|
if !assert.NoError(t, f.SaveAs(resultFile)) {
|
2018-12-27 18:51:44 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
2018-09-01 19:38:30 +08:00
|
|
|
|
2018-12-27 18:51:44 +08:00
|
|
|
func TestDataValidationError(t *testing.T) {
|
2018-12-27 22:28:28 +08:00
|
|
|
resultFile := filepath.Join("test", "TestDataValidationError.xlsx")
|
2018-12-27 18:51:44 +08:00
|
|
|
|
2019-05-05 16:25:57 +08:00
|
|
|
f := NewFile()
|
|
|
|
f.SetCellStr("Sheet1", "E1", "E1")
|
|
|
|
f.SetCellStr("Sheet1", "E2", "E2")
|
|
|
|
f.SetCellStr("Sheet1", "E3", "E3")
|
2018-12-27 18:51:44 +08:00
|
|
|
|
|
|
|
dvRange := NewDataValidation(true)
|
2018-09-13 10:38:01 +08:00
|
|
|
dvRange.SetSqref("A7:B8")
|
|
|
|
dvRange.SetSqref("A7:B8")
|
2018-09-04 13:40:53 +08:00
|
|
|
dvRange.SetSqrefDropList("$E$1:$E$3", true)
|
2018-12-27 18:51:44 +08:00
|
|
|
|
2018-09-13 10:38:01 +08:00
|
|
|
err := dvRange.SetSqrefDropList("$E$1:$E$3", false)
|
2018-12-27 18:51:44 +08:00
|
|
|
assert.EqualError(t, err, "cross-sheet sqref cell are not supported")
|
|
|
|
|
2019-05-05 16:25:57 +08:00
|
|
|
f.AddDataValidation("Sheet1", dvRange)
|
|
|
|
if !assert.NoError(t, f.SaveAs(resultFile)) {
|
2018-12-27 18:51:44 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
2018-09-04 13:40:53 +08:00
|
|
|
|
2018-09-13 10:38:01 +08:00
|
|
|
dvRange = NewDataValidation(true)
|
2019-01-23 22:07:11 +08:00
|
|
|
err = dvRange.SetDropList(make([]string, 258))
|
|
|
|
if dvRange.Formula1 != "" {
|
|
|
|
t.Errorf("data validation error. Formula1 must be empty!")
|
|
|
|
return
|
|
|
|
}
|
2018-12-27 18:51:44 +08:00
|
|
|
assert.EqualError(t, err, "data validation must be 0-255 characters")
|
2019-01-23 22:07:11 +08:00
|
|
|
dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan)
|
|
|
|
dvRange.SetSqref("A9:B10")
|
2018-09-13 10:38:01 +08:00
|
|
|
|
2019-05-05 16:25:57 +08:00
|
|
|
f.AddDataValidation("Sheet1", dvRange)
|
|
|
|
if !assert.NoError(t, f.SaveAs(resultFile)) {
|
2018-12-27 18:51:44 +08:00
|
|
|
t.FailNow()
|
2018-09-01 19:38:30 +08:00
|
|
|
}
|
|
|
|
}
|