This closes #1254, `DeleteDataValidation` support delete all data validations in the worksheet

This commit is contained in:
z.hua 2022-06-15 17:28:59 +08:00 committed by GitHub
parent 7f570c74f8
commit 5beeeef570
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 3 deletions

View File

@ -262,8 +262,9 @@ func (f *File) AddDataValidation(sheet string, dv *DataValidation) error {
}
// DeleteDataValidation delete data validation by given worksheet name and
// reference sequence.
func (f *File) DeleteDataValidation(sheet, sqref string) error {
// reference sequence. All data validations in the worksheet will be deleted
// if not specify reference sequence parameter.
func (f *File) DeleteDataValidation(sheet string, sqref ...string) error {
ws, err := f.workSheetReader(sheet)
if err != nil {
return err
@ -271,7 +272,11 @@ func (f *File) DeleteDataValidation(sheet, sqref string) error {
if ws.DataValidations == nil {
return nil
}
delCells, err := f.flatSqref(sqref)
if sqref == nil {
ws.DataValidations = nil
return nil
}
delCells, err := f.flatSqref(sqref[0])
if err != nil {
return err
}

View File

@ -171,4 +171,8 @@ func TestDeleteDataValidation(t *testing.T) {
// Test delete data validation on no exists worksheet.
assert.EqualError(t, f.DeleteDataValidation("SheetN", "A1:B2"), "sheet SheetN is not exist")
// Test delete all data validations in the worksheet
assert.NoError(t, f.DeleteDataValidation("Sheet1"))
assert.Nil(t, ws.(*xlsxWorksheet).DataValidations)
}