From daf32a37f948f08cd585b14a935b49efdec9ff96 Mon Sep 17 00:00:00 2001 From: rentiansheng Date: Wed, 23 Jan 2019 22:07:11 +0800 Subject: [PATCH] fix: datavalidation list error, formula > 255 issue #339 --- datavalidation.go | 6 +++++- datavalidation_test.go | 10 +++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/datavalidation.go b/datavalidation.go index 0a95251..3035bb2 100644 --- a/datavalidation.go +++ b/datavalidation.go @@ -108,7 +108,11 @@ func (dd *DataValidation) SetInput(title, msg string) { // SetDropList data validation list. func (dd *DataValidation) SetDropList(keys []string) error { - dd.Formula1 = "\"" + strings.Join(keys, ",") + "\"" + formula := "\"" + strings.Join(keys, ",") + "\"" + if dataValidationFormulaStrLen < len(formula) { + return fmt.Errorf(dataValidationFormulaStrLenErr) + } + dd.Formula1 = formula dd.Type = convDataValidationType(typeList) return nil } diff --git a/datavalidation_test.go b/datavalidation_test.go index fad50c2..afb659c 100644 --- a/datavalidation_test.go +++ b/datavalidation_test.go @@ -72,10 +72,14 @@ func TestDataValidationError(t *testing.T) { } dvRange = NewDataValidation(true) - dvRange.SetDropList(make([]string, 258)) - - err = dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan) + 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") xlsx.AddDataValidation("Sheet1", dvRange) if !assert.NoError(t, xlsx.SaveAs(resultFile)) {