当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 0 Fork 162

lurker8/excelize
暂停

forked from xuri/excelize 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
datavalidation_test.go 2.71 KB
一键复制 编辑 原始数据 按行查看 历史
xuri 提交于 2019-08-11 00:36 . Documentation updated, Go 1.10+ required
// Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of
// 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
// charts of XLSX. This library needs Go version 1.10 or later.
package excelize
import (
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDataValidation(t *testing.T) {
resultFile := filepath.Join("test", "TestDataValidation.xlsx")
f := NewFile()
dvRange := NewDataValidation(true)
dvRange.Sqref = "A1:B2"
dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorBetween)
dvRange.SetError(DataValidationErrorStyleStop, "error title", "error body")
dvRange.SetError(DataValidationErrorStyleWarning, "error title", "error body")
dvRange.SetError(DataValidationErrorStyleInformation, "error title", "error body")
f.AddDataValidation("Sheet1", dvRange)
if !assert.NoError(t, f.SaveAs(resultFile)) {
t.FailNow()
}
dvRange = NewDataValidation(true)
dvRange.Sqref = "A3:B4"
dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan)
dvRange.SetInput("input title", "input body")
f.AddDataValidation("Sheet1", dvRange)
if !assert.NoError(t, f.SaveAs(resultFile)) {
t.FailNow()
}
dvRange = NewDataValidation(true)
dvRange.Sqref = "A5:B6"
dvRange.SetDropList([]string{"1", "2", "3"})
f.AddDataValidation("Sheet1", dvRange)
if !assert.NoError(t, f.SaveAs(resultFile)) {
t.FailNow()
}
}
func TestDataValidationError(t *testing.T) {
resultFile := filepath.Join("test", "TestDataValidationError.xlsx")
f := NewFile()
f.SetCellStr("Sheet1", "E1", "E1")
f.SetCellStr("Sheet1", "E2", "E2")
f.SetCellStr("Sheet1", "E3", "E3")
dvRange := NewDataValidation(true)
dvRange.SetSqref("A7:B8")
dvRange.SetSqref("A7:B8")
dvRange.SetSqrefDropList("$E$1:$E$3", true)
err := dvRange.SetSqrefDropList("$E$1:$E$3", false)
assert.EqualError(t, err, "cross-sheet sqref cell are not supported")
f.AddDataValidation("Sheet1", dvRange)
if !assert.NoError(t, f.SaveAs(resultFile)) {
t.FailNow()
}
dvRange = NewDataValidation(true)
err = dvRange.SetDropList(make([]string, 258))
if dvRange.Formula1 != "" {
t.Errorf("data validation error. Formula1 must be empty!")
return
}
assert.EqualError(t, err, "data validation must be 0-255 characters")
dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan)
dvRange.SetSqref("A9:B10")
f.AddDataValidation("Sheet1", dvRange)
if !assert.NoError(t, f.SaveAs(resultFile)) {
t.FailNow()
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/lurker8/excelize.git
git@gitee.com:lurker8/excelize.git
lurker8
excelize
excelize
master

搜索帮助