Skip to content
This repository has been archived by the owner on Feb 20, 2021. It is now read-only.

Commit

Permalink
Fixed: 'Missing column name in exception message' #19
Browse files Browse the repository at this point in the history
  • Loading branch information
eraydin committed Mar 19, 2019
1 parent 1c55471 commit b4a2a50
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
2 changes: 1 addition & 1 deletion common.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<VersionPrefix>2.3.0</VersionPrefix>
<VersionPrefix>2.3.1</VersionPrefix>
<Description>An extensions library for EPPlus package to generate and manipulate Excel files easily.</Description>

<NoWarn>$(NoWarn);CS1591</NoWarn>
Expand Down
14 changes: 7 additions & 7 deletions src/EPPlus.Core.Extensions/ExcelTableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ private static IEnumerable<KeyValuePair<int, PropertyInfo>> PrepareMappings<T>(E
List<ExcelTableColumnAttributeAndPropertyInfo> 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;

Expand All @@ -195,7 +195,7 @@ private static IEnumerable<KeyValuePair<int, PropertyInfo>> PrepareMappings<T>(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,
Expand Down Expand Up @@ -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
{
Expand All @@ -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)) });
}
}

Expand All @@ -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 });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -829,5 +829,27 @@ public void Should_check_columns_of_given_row_whether_it_contains_same_values_wi
actionForEmptySheet1.Should().Throw<ExcelValidationException>().And.Message.Should().Be("'Barcode' column is not found on the worksheet.");
actionForEmptySheet2.Should().Throw<ExcelValidationException>().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<DefaultMap>();
};

//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<ExcelValidationException>().And.Message.Should().Be("'Name' column could not found on the worksheet.");
}
}
}

0 comments on commit b4a2a50

Please sign in to comment.