From b4a2a5010e95aed1d8394868dae40cd680f2edb6 Mon Sep 17 00:00:00 2001 From: eraydin Date: Tue, 19 Mar 2019 22:45:31 +0100 Subject: [PATCH] Fixed: 'Missing column name in exception message' https://github.com/eraydin/EPPlus.Core.Extensions/issues/19 --- common.props | 2 +- .../ExcelTableExtensions.cs | 14 ++++++------ .../ExcelWorksheetExtensions_Tests.cs | 22 +++++++++++++++++++ 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/common.props b/common.props index b11b401..96fc820 100644 --- a/common.props +++ b/common.props @@ -1,6 +1,6 @@ - 2.3.0 + 2.3.1 An extensions library for EPPlus package to generate and manipulate Excel files easily. $(NoWarn);CS1591 diff --git a/src/EPPlus.Core.Extensions/ExcelTableExtensions.cs b/src/EPPlus.Core.Extensions/ExcelTableExtensions.cs index 17e23b2..48dc313 100644 --- a/src/EPPlus.Core.Extensions/ExcelTableExtensions.cs +++ b/src/EPPlus.Core.Extensions/ExcelTableExtensions.cs @@ -171,10 +171,10 @@ private static IEnumerable> PrepareMappings(E List propertyInfoAndColumnAttributes = typeof(T).GetExcelTableColumnAttributesWithPropertyInfo(); // Build property-table column mapping - foreach (ExcelTableColumnAttributeAndPropertyInfo properyInfoAndColumnAttribute in propertyInfoAndColumnAttributes) + foreach (var propertyInfoAndColumnAttribute in propertyInfoAndColumnAttributes) { - PropertyInfo propertyInfo = properyInfoAndColumnAttribute.PropertyInfo; - ExcelTableColumnAttribute columnAttribute = properyInfoAndColumnAttribute.ColumnAttribute; + PropertyInfo propertyInfo = propertyInfoAndColumnAttribute.PropertyInfo; + ExcelTableColumnAttribute columnAttribute = propertyInfoAndColumnAttribute.ColumnAttribute; int col = -1; @@ -195,7 +195,7 @@ private static IEnumerable> PrepareMappings(E if (col == -1) { - throw new ExcelValidationException(string.Format(configuration.ColumnValidationExceptionMessage, columnAttribute.ColumnName)) + throw new ExcelValidationException(string.Format(configuration.ColumnValidationExceptionMessage, columnAttribute.ColumnName ?? propertyInfo.Name)) .WithArguments(new ExcelExceptionArgs { ColumnName = columnAttribute.ColumnName, @@ -271,7 +271,7 @@ private static void TrySetProperty(object item, PropertyInfo property, object ce BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty, null, item, - new object[] { Enum.Parse(type, cell.ToString(), true) }); + new [] { Enum.Parse(type, cell.ToString(), true) }); } else // ...and numeric cell value { @@ -282,7 +282,7 @@ private static void TrySetProperty(object item, PropertyInfo property, object ce BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty, null, item, - new object[] { Enum.ToObject(type, cell.ChangeType(underType)) }); + new [] { Enum.ToObject(type, cell.ChangeType(underType)) }); } } @@ -293,7 +293,7 @@ private static void TrySetProperty(object item, PropertyInfo property, object ce BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty, null, item, - new object[] { cell.ChangeType(type) }); + new [] { cell.ChangeType(type) }); } Validator.ValidateProperty(property.GetValue(item), new ValidationContext(item) { MemberName = property.Name }); diff --git a/test/EPPlus.Core.Extensions.Tests/ExcelWorksheetExtensions_Tests.cs b/test/EPPlus.Core.Extensions.Tests/ExcelWorksheetExtensions_Tests.cs index 615d182..98b431b 100644 --- a/test/EPPlus.Core.Extensions.Tests/ExcelWorksheetExtensions_Tests.cs +++ b/test/EPPlus.Core.Extensions.Tests/ExcelWorksheetExtensions_Tests.cs @@ -829,5 +829,27 @@ public void Should_check_columns_of_given_row_whether_it_contains_same_values_wi actionForEmptySheet1.Should().Throw().And.Message.Should().Be("'Barcode' column is not found on the worksheet."); actionForEmptySheet2.Should().Throw().And.Message.Should().Be("'LicensePlate' column is not found on the worksheet."); } + + [Fact] + public void Should_throw_exception_if_columnname_and_columnindex_not_defined_and_column_missing_on_Excel() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + ExcelWorksheet worksheet1 = excelPackage2.GetWorksheet("RandomOrderedColumns"); + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => + { + var result = worksheet1.ToList(); + }; + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().Throw().And.Message.Should().Be("'Name' column could not found on the worksheet."); + } } }