Skip to content

Commit

Permalink
Complete unit testing case for data validation
Browse files Browse the repository at this point in the history
  • Loading branch information
xuri committed Sep 13, 2018
1 parent b4a6e61 commit 4f47737
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ go:
- 1.8.x
- 1.9.x
- 1.10.x
- 1.11.x

os:
- linux
Expand Down
17 changes: 13 additions & 4 deletions datavalidation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down Expand Up @@ -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"
Expand Down
14 changes: 12 additions & 2 deletions datavalidation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down

0 comments on commit 4f47737

Please sign in to comment.