datavalidation_test.go 2.0 KB
Newer Older
xurime's avatar
xurime 已提交
1 2 3 4 5 6 7 8
// 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.8 or later.
//
// Copyright 2016 - 2018 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.
9 10 11 12 13 14 15 16 17 18 19 20 21
package excelize

import (
	"testing"
)

func TestDataValidation(t *testing.T) {
	xlsx := NewFile()

	dvRange := NewDataValidation(true)
	dvRange.Sqref = "A1:B2"
	dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorBetween)
	dvRange.SetError(DataValidationErrorStyleStop, "error title", "error body")
22 23
	dvRange.SetError(DataValidationErrorStyleWarning, "error title", "error body")
	dvRange.SetError(DataValidationErrorStyleInformation, "error title", "error body")
24 25 26 27 28 29 30 31 32 33 34 35 36
	xlsx.AddDataValidation("Sheet1", dvRange)

	dvRange = NewDataValidation(true)
	dvRange.Sqref = "A3:B4"
	dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan)
	dvRange.SetInput("input title", "input body")
	xlsx.AddDataValidation("Sheet1", dvRange)

	dvRange = NewDataValidation(true)
	dvRange.Sqref = "A5:B6"
	dvRange.SetDropList([]string{"1", "2", "3"})
	xlsx.AddDataValidation("Sheet1", dvRange)

37 38 39 40
	xlsx.SetCellStr("Sheet1", "E1", "E1")
	xlsx.SetCellStr("Sheet1", "E2", "E2")
	xlsx.SetCellStr("Sheet1", "E3", "E3")
	dvRange = NewDataValidation(true)
41 42
	dvRange.SetSqref("A7:B8")
	dvRange.SetSqref("A7:B8")
43
	dvRange.SetSqrefDropList("$E$1:$E$3", true)
44 45
	err := dvRange.SetSqrefDropList("$E$1:$E$3", false)
	t.Log(err)
46 47
	xlsx.AddDataValidation("Sheet1", dvRange)

48 49 50 51 52
	dvRange = NewDataValidation(true)
	dvRange.SetDropList(make([]string, 258))
	err = dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan)
	t.Log(err)

53
	// Test write file to given path.
54
	err = xlsx.SaveAs("./test/Book_data_validation.xlsx")
55 56 57 58
	if err != nil {
		t.Error(err)
	}
}