Complete unit testing case for data validation

This commit is contained in:
xuri 2018-09-13 10:38:01 +08:00
parent b4a6e61ec3
commit 4f47737d64
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
3 changed files with 26 additions and 6 deletions

View File

@ -7,6 +7,7 @@ go:
- 1.8.x - 1.8.x
- 1.9.x - 1.9.x
- 1.10.x - 1.10.x
- 1.11.x
os: os:
- linux - linux

View File

@ -127,15 +127,24 @@ func (dd *DataValidation) SetRange(f1, f2 int, t DataValidationType, o DataValid
return nil 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 { func (dd *DataValidation) SetSqrefDropList(sqref string, isCurrentSheet bool) error {
if isCurrentSheet { if isCurrentSheet {
dd.Formula1 = sqref dd.Formula1 = sqref
dd.Type = convDataValidationType(typeList) dd.Type = convDataValidationType(typeList)
return nil return nil
} }
//isCurrentSheet = false Cross-sheet sqref cell use extLst xml node unrealized
return fmt.Errorf("cross-sheet sqref cell are not supported") return fmt.Errorf("cross-sheet sqref cell are not supported")
} }
@ -206,7 +215,7 @@ func convDataValidationOperatior(o DataValidationOperator) string {
// xlsx.AddDataValidation("Sheet1", dvRange) // xlsx.AddDataValidation("Sheet1", dvRange)
// //
// Example 3, set data validation on Sheet1!A5:B6 with validation criteria // 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 = excelize.NewDataValidation(true)
// dvRange.Sqref = "A5:B6" // dvRange.Sqref = "A5:B6"

View File

@ -19,6 +19,8 @@ func TestDataValidation(t *testing.T) {
dvRange.Sqref = "A1:B2" dvRange.Sqref = "A1:B2"
dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorBetween) dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorBetween)
dvRange.SetError(DataValidationErrorStyleStop, "error title", "error body") 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) xlsx.AddDataValidation("Sheet1", dvRange)
dvRange = NewDataValidation(true) dvRange = NewDataValidation(true)
@ -36,12 +38,20 @@ func TestDataValidation(t *testing.T) {
xlsx.SetCellStr("Sheet1", "E2", "E2") xlsx.SetCellStr("Sheet1", "E2", "E2")
xlsx.SetCellStr("Sheet1", "E3", "E3") xlsx.SetCellStr("Sheet1", "E3", "E3")
dvRange = NewDataValidation(true) dvRange = NewDataValidation(true)
dvRange.Sqref = "A7:B8" dvRange.SetSqref("A7:B8")
dvRange.SetSqref("A7:B8")
dvRange.SetSqrefDropList("$E$1:$E$3", true) dvRange.SetSqrefDropList("$E$1:$E$3", true)
err := dvRange.SetSqrefDropList("$E$1:$E$3", false)
t.Log(err)
xlsx.AddDataValidation("Sheet1", dvRange) 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. // 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 { if err != nil {
t.Error(err) t.Error(err)
} }