From 4f47737d64fc9d9108675cbc1e73ae93c2d723a9 Mon Sep 17 00:00:00 2001 From: xuri Date: Thu, 13 Sep 2018 10:38:01 +0800 Subject: [PATCH] Complete unit testing case for data validation --- .travis.yml | 1 + datavalidation.go | 17 +++++++++++++---- datavalidation_test.go | 14 ++++++++++++-- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 26ca7b299c..c2f0f90590 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,7 @@ go: - 1.8.x - 1.9.x - 1.10.x + - 1.11.x os: - linux diff --git a/datavalidation.go b/datavalidation.go index be3caabe92..c0f006f35f 100644 --- a/datavalidation.go +++ b/datavalidation.go @@ -127,15 +127,24 @@ func (dd *DataValidation) SetRange(f1, f2 int, t DataValidationType, o DataValid return nil } -// SetSqrefDropList data validation list with current sheet cell rang +// SetSqrefDropList provides set data validation on a range with source +// reference range of the worksheet by given data validation object and +// worksheet name. The data validation object can be created by +// NewDataValidation function. For example, set data validation on +// Sheet1!A7:B8 with validation criteria source Sheet1!E1:E3 settings, create +// in-cell dropdown by allowing list source: +// +// dvRange := excelize.NewDataValidation(true) +// dvRange.Sqref = "A7:B8" +// dvRange.SetSqrefDropList("E1:E3", true) +// xlsx.AddDataValidation("Sheet1", dvRange) +// func (dd *DataValidation) SetSqrefDropList(sqref string, isCurrentSheet bool) error { if isCurrentSheet { dd.Formula1 = sqref dd.Type = convDataValidationType(typeList) return nil } - - //isCurrentSheet = false Cross-sheet sqref cell use extLst xml node unrealized return fmt.Errorf("cross-sheet sqref cell are not supported") } @@ -206,7 +215,7 @@ func convDataValidationOperatior(o DataValidationOperator) string { // xlsx.AddDataValidation("Sheet1", dvRange) // // Example 3, set data validation on Sheet1!A5:B6 with validation criteria -// settings, create in-cell dropdown by allow list source: +// settings, create in-cell dropdown by allowing list source: // // dvRange = excelize.NewDataValidation(true) // dvRange.Sqref = "A5:B6" diff --git a/datavalidation_test.go b/datavalidation_test.go index 8134b721a5..98993342d8 100644 --- a/datavalidation_test.go +++ b/datavalidation_test.go @@ -19,6 +19,8 @@ func TestDataValidation(t *testing.T) { 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") xlsx.AddDataValidation("Sheet1", dvRange) dvRange = NewDataValidation(true) @@ -36,12 +38,20 @@ func TestDataValidation(t *testing.T) { xlsx.SetCellStr("Sheet1", "E2", "E2") xlsx.SetCellStr("Sheet1", "E3", "E3") dvRange = NewDataValidation(true) - dvRange.Sqref = "A7:B8" + dvRange.SetSqref("A7:B8") + dvRange.SetSqref("A7:B8") dvRange.SetSqrefDropList("$E$1:$E$3", true) + err := dvRange.SetSqrefDropList("$E$1:$E$3", false) + t.Log(err) xlsx.AddDataValidation("Sheet1", dvRange) + dvRange = NewDataValidation(true) + dvRange.SetDropList(make([]string, 258)) + err = dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan) + t.Log(err) + // Test write file to given path. - err := xlsx.SaveAs("./test/Book_data_validation.xlsx") + err = xlsx.SaveAs("./test/Book_data_validation.xlsx") if err != nil { t.Error(err) }