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

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
eraydin committed Feb 11, 2018
2 parents adf034c + 54388ce commit 74018d9
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 59 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>1.2.5</VersionPrefix>
<VersionPrefix>1.2.6</VersionPrefix>
<Description>An extensions library for both EPPlus and EPPlus.Core packages to generate and manipulate Excel files easily.</Description>

<NoWarn>$(NoWarn);CS1591</NoWarn>
Expand Down
10 changes: 6 additions & 4 deletions src/EPPlus.Core.Extensions/ExcelPackageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@ public static DataSet ToDataSet(this ExcelPackage package, bool hasHeaderRow = t
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="package"></param>
/// <param name="onCaught">Gets created list item and current row index</param>
/// <param name="configurationAction"></param>
/// <param name="worksheetIndex"></param>
/// <returns></returns>
public static IEnumerable<T> AsEnumerable<T>(this ExcelPackage package, int worksheetIndex = 1, Action<IExcelConfiguration<T>> configurationAction = null) where T : class, new()
public static IEnumerable<T> AsEnumerable<T>(this ExcelPackage package, int worksheetIndex = 1, OnCaught<T> onCaught = null, Action<IExcelConfiguration<T>> configurationAction = null) where T : class, new()
{
return package.Workbook.Worksheets[worksheetIndex].AsEnumerable(configurationAction);
return package.Workbook.Worksheets[worksheetIndex].AsEnumerable(onCaught, configurationAction);
}

/// <summary>
Expand All @@ -91,11 +92,12 @@ public static DataSet ToDataSet(this ExcelPackage package, bool hasHeaderRow = t
/// <typeparam name="T"></typeparam>
/// <param name="package"></param>
/// <param name="worksheetIndex"></param>
/// <param name="onCaught"></param>
/// <param name="configurationAction"></param>
/// <returns></returns>
public static List<T> ToList<T>(this ExcelPackage package, int worksheetIndex = 1, Action<IExcelConfiguration<T>> configurationAction = null) where T : class, new()
public static List<T> ToList<T>(this ExcelPackage package, int worksheetIndex = 1, OnCaught<T> onCaught = null, Action<IExcelConfiguration<T>> configurationAction = null) where T : class, new()
{
return package.AsEnumerable(worksheetIndex, configurationAction).ToList();
return package.AsEnumerable(worksheetIndex, onCaught, configurationAction).ToList();
}
}
}
10 changes: 7 additions & 3 deletions src/EPPlus.Core.Extensions/ExcelTableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ public static ExcelAddress GetDataBounds(this ExcelTable table)
/// </remarks>
/// <typeparam name="T">Type to map to. Type should be a class and should have parameterless constructor.</typeparam>
/// <param name="table">Table object to fetch</param>
/// <param name="onCaught"></param>
/// <param name="configurationAction"></param>
/// <returns>An enumerable of the generating type</returns>
public static IEnumerable<T> AsEnumerable<T>(this ExcelTable table, Action<IExcelConfiguration<T>> configurationAction = null) where T : class, new()
public static IEnumerable<T> AsEnumerable<T>(this ExcelTable table, OnCaught<T> onCaught = null, Action<IExcelConfiguration<T>> configurationAction = null) where T : class, new()
{
IExcelConfiguration<T> configuration = DefaultExcelConfiguration<T>.Instance;
configurationAction?.Invoke(configuration);
Expand Down Expand Up @@ -132,6 +133,8 @@ public static ExcelAddress GetDataBounds(this ExcelTable table)
}
}

onCaught?.Invoke(item, row);

// TODO:
if (!configuration.SkipValidationErrors)
{
Expand All @@ -153,11 +156,12 @@ public static ExcelAddress GetDataBounds(this ExcelTable table)
/// </remarks>
/// <typeparam name="T">Type to map to. Type should be a class and should have parameterless constructor.</typeparam>
/// <param name="table">Table object to fetch</param>
/// <param name="onCaught"></param>
/// <param name="configurationAction"></param>
/// <returns>An enumerable of the generating type</returns>
public static List<T> ToList<T>(this ExcelTable table, Action<IExcelConfiguration<T>> configurationAction = null) where T : class, new()
public static List<T> ToList<T>(this ExcelTable table, OnCaught<T> onCaught = null, Action<IExcelConfiguration<T>> configurationAction = null) where T : class, new()
{
return AsEnumerable(table, configurationAction).ToList();
return AsEnumerable(table, onCaught, configurationAction).ToList();
}

/// <summary>
Expand Down
10 changes: 6 additions & 4 deletions src/EPPlus.Core.Extensions/ExcelWorksheetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,26 +121,28 @@ public static DataTable ToDataTable(this ExcelWorksheet worksheet, bool hasHeade
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="worksheet"></param>
/// <param name="onCaught"></param>
/// <param name="configurationAction"></param>
/// <returns></returns>
public static IEnumerable<T> AsEnumerable<T>(this ExcelWorksheet worksheet, Action<IExcelConfiguration<T>> configurationAction = null) where T : class, new()
public static IEnumerable<T> AsEnumerable<T>(this ExcelWorksheet worksheet, OnCaught<T> onCaught = null, Action<IExcelConfiguration<T>> configurationAction = null) where T : class, new()
{
IExcelConfiguration<T> configuration = DefaultExcelConfiguration<T>.Instance;
configurationAction?.Invoke(configuration);

return worksheet.AsExcelTable(configuration.HasHeaderRow).AsEnumerable(configurationAction);
return worksheet.AsExcelTable(configuration.HasHeaderRow).AsEnumerable(onCaught, configurationAction);
}

/// <summary>
/// Returns objects of specified type from the ExcelWorksheet as a list.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="worksheet"></param>
/// <param name="onCaught"></param>
/// <param name="configurationAction"></param>
/// <returns></returns>
public static List<T> ToList<T>(this ExcelWorksheet worksheet, Action<IExcelConfiguration<T>> configurationAction = null) where T : class, new()
public static List<T> ToList<T>(this ExcelWorksheet worksheet, OnCaught<T> onCaught = null, Action<IExcelConfiguration<T>> configurationAction = null) where T : class, new()
{
return worksheet.AsEnumerable(configurationAction).ToList();
return worksheet.AsEnumerable(onCaught, configurationAction).ToList();
}

/// <summary>
Expand Down
4 changes: 4 additions & 0 deletions src/EPPlus.Core.Extensions/OnCaught.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace EPPlus.Core.Extensions
{
public delegate void OnCaught<T>(T current, int rowIndex);
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,34 @@ public void Should_convert_given_ExcelPackage_to_list_of_objects_with_worksheet_
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
list = excelPackage.ToList<StocksNullable>(6, configuration =>
list = excelPackage.ToList<StocksNullable>(6, null, configuration =>
{
configuration.HasHeaderRow = false;
configuration.SkipCastingErrors = true;
});

//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
list.Any().Should().BeTrue();
list.Count.Should().Be(4);
}

[Fact]
public void should_be_interceptable_current_row_when_its_converting_to_a_list()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
List<StocksNullable> list;

//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
list = excelPackage.ToList<StocksNullable>(6, (current, index) =>
{
current.Barcode.Should().NotBeNull();
}, configuration =>
{
configuration.HasHeaderRow = false;
configuration.SkipCastingErrors = true;
Expand Down
10 changes: 5 additions & 5 deletions test/EPPlus.Core.Extensions.Tests/ExcelTableExtensions_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,10 @@ public void Test_MapSilentFail()
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
List<EnumFailMap> list = table.ToList<EnumFailMap>(configuration =>
{
configuration.SkipCastingErrors = true;
});
List<EnumFailMap> list = table.ToList<EnumFailMap>(null, configuration =>
{
configuration.SkipCastingErrors = true;
});

//-----------------------------------------------------------------------------------------------------------
// Assert
Expand Down Expand Up @@ -309,7 +309,7 @@ public void Test_Nullable_With_SkipCastErrors()
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
List<StocksNullable> list = table.ToList<StocksNullable>(configuration =>
List<StocksNullable> list = table.ToList<StocksNullable>(null,configuration =>
{
configuration.SkipCastingErrors = true;
});
Expand Down
82 changes: 41 additions & 41 deletions test/EPPlus.Core.Extensions.Tests/ExcelWorksheetExtensions_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void Test_GetAsExcelTable_Without_Header()
// Assert
//-----------------------------------------------------------------------------------------------------------

List<StocksNullable> listOfStocks = excelTable.ToList<StocksNullable>(configuration => { configuration.SkipCastingErrors = true; });
List<StocksNullable> listOfStocks = excelTable.ToList<StocksNullable>(null, configuration => { configuration.SkipCastingErrors = true; });
listOfStocks.Count.Should().Be(4);
}

Expand Down Expand Up @@ -152,16 +152,16 @@ public void Test_Worksheet_AsEnumerable()
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
IEnumerable<StocksNullable> list1 = worksheet1.AsEnumerable<StocksNullable>(configuration =>
{
configuration.SkipCastingErrors = true;
configuration.HasHeaderRow = true;
});
IEnumerable<StocksNullable> list2 = worksheet2.AsEnumerable<StocksNullable>(configuration =>
{
configuration.SkipCastingErrors = true;
configuration.HasHeaderRow = false;
});
IEnumerable<StocksNullable> list1 = worksheet1.AsEnumerable<StocksNullable>(null, configuration =>
{
configuration.SkipCastingErrors = true;
configuration.HasHeaderRow = true;
});
IEnumerable<StocksNullable> list2 = worksheet2.AsEnumerable<StocksNullable>(null, configuration =>
{
configuration.SkipCastingErrors = true;
configuration.HasHeaderRow = false;
});

//-----------------------------------------------------------------------------------------------------------
// Assert
Expand All @@ -182,16 +182,16 @@ public void Test_Worksheet_ToList()
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
IEnumerable<StocksNullable> list1 = worksheet1.ToList<StocksNullable>(configuration =>
{
configuration.SkipCastingErrors = true;
configuration.HasHeaderRow = true;
});
IEnumerable<StocksNullable> list2 = worksheet2.ToList<StocksNullable>(configuration =>
{
configuration.SkipCastingErrors = true;
configuration.HasHeaderRow = false;
});
IEnumerable<StocksNullable> list1 = worksheet1.ToList<StocksNullable>(null, configuration =>
{
configuration.SkipCastingErrors = true;
configuration.HasHeaderRow = true;
});
IEnumerable<StocksNullable> list2 = worksheet2.ToList<StocksNullable>(null, configuration =>
{
configuration.SkipCastingErrors = true;
configuration.HasHeaderRow = false;
});

//-----------------------------------------------------------------------------------------------------------
// Assert
Expand Down Expand Up @@ -222,11 +222,11 @@ public void Should_work_AddObjects_method_with_parameters()
// Act
//-----------------------------------------------------------------------------------------------------------
worksheet.AddObjects(stocks, 5, _ => _.Barcode, _ => _.Quantity, _ => _.UpdatedDate);
IEnumerable<StocksNullable> list = worksheet.ToList<StocksNullable>(configuration =>
{
configuration.SkipCastingErrors = false;
configuration.HasHeaderRow = true;
});
IEnumerable<StocksNullable> list = worksheet.ToList<StocksNullable>(null, configuration =>
{
configuration.SkipCastingErrors = false;
configuration.HasHeaderRow = true;
});

//-----------------------------------------------------------------------------------------------------------
// Assert
Expand Down Expand Up @@ -284,11 +284,11 @@ public void Should_AddObjects_method_work_without_parameters()
// Act
//-----------------------------------------------------------------------------------------------------------
worksheet.AddObjects(stocks, 5, 3);
IEnumerable<StocksNullable> list = worksheet.ToList<StocksNullable>(configuration =>
{
configuration.SkipCastingErrors = true;
configuration.HasHeaderRow = true;
});
IEnumerable<StocksNullable> list = worksheet.ToList<StocksNullable>(null, configuration =>
{
configuration.SkipCastingErrors = true;
configuration.HasHeaderRow = true;
});

//-----------------------------------------------------------------------------------------------------------
// Assert
Expand All @@ -308,11 +308,11 @@ public void Should_AddLine_method_work()
// Act
//-----------------------------------------------------------------------------------------------------------
worksheet.AddLine(5, "barcode123", 5, DateTime.UtcNow);
IEnumerable<StocksNullable> list = worksheet.ToList<StocksNullable>(configuration =>
{
configuration.SkipCastingErrors = false;
configuration.HasHeaderRow = true;
});
IEnumerable<StocksNullable> list = worksheet.ToList<StocksNullable>(null, configuration =>
{
configuration.SkipCastingErrors = false;
configuration.HasHeaderRow = true;
});

//-----------------------------------------------------------------------------------------------------------
// Assert
Expand Down Expand Up @@ -472,11 +472,11 @@ public void Test_StocksValidation()
//-----------------------------------------------------------------------------------------------------------
Action action = () =>
{
list = worksheet.ToList<StocksValidation>(configuration =>
{
configuration.SkipCastingErrors = false;
configuration.HasHeaderRow = true;
});
list = worksheet.ToList<StocksValidation>(null, configuration =>
{
configuration.SkipCastingErrors = false;
configuration.HasHeaderRow = true;
});
};

//-----------------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit 74018d9

Please sign in to comment.