From 85c1fb77eca14f2b0c0c530cad67805ecd6db644 Mon Sep 17 00:00:00 2001 From: Mats Alm <897655+swmal@users.noreply.github.com> Date: Tue, 6 Jul 2021 11:16:39 +0200 Subject: [PATCH] #427 - Validation of datavalidations throw an exception when Formula1 is empty even if AllowBlank was set to true --- src/EPPlus/DataValidation/ExcelDataValidation.cs | 2 +- .../DataValidation/DataValidationTests.cs | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/EPPlus/DataValidation/ExcelDataValidation.cs b/src/EPPlus/DataValidation/ExcelDataValidation.cs index 5593a9d23..b2beab8ab 100644 --- a/src/EPPlus/DataValidation/ExcelDataValidation.cs +++ b/src/EPPlus/DataValidation/ExcelDataValidation.cs @@ -253,7 +253,7 @@ public virtual void Validate() { var address = Address.Address; // validate Formula1 - if (string.IsNullOrEmpty(Formula1Internal)) + if (string.IsNullOrEmpty(Formula1Internal) && !(AllowBlank ?? false)) { throw new InvalidOperationException("Validation of " + address + " failed: Formula1 cannot be empty"); } diff --git a/src/EPPlusTest/DataValidation/DataValidationTests.cs b/src/EPPlusTest/DataValidation/DataValidationTests.cs index 72fa92944..7d100cf3e 100644 --- a/src/EPPlusTest/DataValidation/DataValidationTests.cs +++ b/src/EPPlusTest/DataValidation/DataValidationTests.cs @@ -154,6 +154,21 @@ public void DataValidations_ShouldAcceptOneItemOnly() validation.Validate(); } + [TestMethod] + public void DataValidations_ShouldNotThrowIfAllowBlankIsSet() + { + var validation = _sheet.DataValidations.AddListValidation("A1"); + validation.AllowBlank = true; + validation.Validate(); + } + + [TestMethod, ExpectedException(typeof(InvalidOperationException))] + public void DataValidations_ShouldThrowIfAllowBlankIsNotSet() + { + var validation = _sheet.DataValidations.AddListValidation("A1"); + validation.Validate(); + } + [TestMethod] public void ExcelDataValidation_ShouldReplaceLastPartInWholeColumnRangeWithMaxNumberOfRowsOneColumn() {