From b0285abc11f48626b8be38131155fc98d413dea1 Mon Sep 17 00:00:00 2001 From: "z.hua" <276675879@qq.com> Date: Wed, 15 Jun 2022 17:28:59 +0800 Subject: [PATCH] This closes #1254, `DeleteDataValidation` support delete all data validations in the worksheet --- datavalidation.go | 11 ++++++++--- datavalidation_test.go | 4 ++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/datavalidation.go b/datavalidation.go index 4df2c505ae7..1b06b6a7e21 100644 --- a/datavalidation.go +++ b/datavalidation.go @@ -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 } diff --git a/datavalidation_test.go b/datavalidation_test.go index 9ef11dcd38e..80cbf595478 100644 --- a/datavalidation_test.go +++ b/datavalidation_test.go @@ -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) }