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

Commit

Permalink
- Minor performance improvements,
Browse files Browse the repository at this point in the history
- Renamed CheckAndThrowColumn() method as CheckColumnAndThrow(),
- Implemented CheckExistenceOfColumnsAndThrow() method,
- Incremented version (v2.2.3)
  • Loading branch information
eraydin committed Jun 27, 2018
1 parent d694e4f commit 0800d27
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 68 deletions.
4 changes: 2 additions & 2 deletions common.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<VersionPrefix>2.2.2</VersionPrefix>
<Description>An extensions library for both EPPlus and EPPlus.Core packages to generate and manipulate Excel files easily.</Description>
<VersionPrefix>2.2.3</VersionPrefix>
<Description>An extensions library for EPPlus package to generate and manipulate Excel files easily.</Description>

<NoWarn>$(NoWarn);CS1591</NoWarn>
<PackageIconUrl></PackageIconUrl>
Expand Down
15 changes: 7 additions & 8 deletions src/EPPlus.Core.Extensions/EPPlus.Core.Extensions.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\common.props"></Import>

<Import Project="..\..\common.props">
</Import>
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net461;</TargetFrameworks>
</PropertyGroup>

<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="EPPlus" Version="4.5.2.1" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.5.0" />
</ItemGroup>
</Project>
<PackageReference Include="System.ComponentModel.Annotations" Version="4.5.0" />
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions src/EPPlus.Core.Extensions/ExcelTableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static ExcelAddress GetDataBounds(this ExcelTable table)

ExcelAddress bounds = table.GetDataBounds();

var item = (T)Activator.CreateInstance(typeof(T));
var item = new T();

// Parse table
for (int row = bounds.Start.Row; row <= bounds.End.Row; row++)
Expand Down Expand Up @@ -105,7 +105,7 @@ public static ExcelAddress GetDataBounds(this ExcelTable table)
// Parse table
for (int row = bounds.Start.Row; row <= bounds.End.Row; row++)
{
var item = (T)Activator.CreateInstance(typeof(T));
var item = new T();

foreach (KeyValuePair<int, PropertyInfo> map in mapping)
{
Expand Down
38 changes: 30 additions & 8 deletions src/EPPlus.Core.Extensions/ExcelWorksheetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ public static bool IsColumnDuplicatedOnRow(this ExcelWorksheet worksheet, int ro
return worksheet.GetColumns(rowIndex).Where(x => x.Value.Equals(columnText, stringComparison)).IsGreaterThanOne();
}

public static void CheckAndThrowIfDuplicatedColumnsFoundOnRow(this ExcelWorksheet worksheet, int rowIndex, string exceptionMessage = null)
public static void CheckAndThrowIfDuplicatedColumnsFound(this ExcelWorksheet worksheet, int rowIndex, string exceptionMessage = null)
{
foreach (KeyValuePair<int, string> column in worksheet.GetColumns(rowIndex).Where(x => !string.IsNullOrEmpty(x.Value)))
{
Expand All @@ -349,7 +349,7 @@ public static void CheckAndThrowIfDuplicatedColumnsFoundOnRow(this ExcelWorkshee
}
}

public static void CheckAndThrowIfDuplicatedColumnsFoundOnRow<T>(this ExcelWorksheet worksheet, int rowIndex, string exceptionMessage = null)
public static void CheckAndThrowIfDuplicatedColumnsFound<T>(this ExcelWorksheet worksheet, int rowIndex, string exceptionMessage = null)
{
List<ExcelTableColumnAttributeAndPropertyInfo> properyInfoAndColumnAttributes = typeof(T).GetExcelTableColumnAttributesWithProperyInfo();

Expand All @@ -363,8 +363,8 @@ public static void CheckAndThrowIfDuplicatedColumnsFoundOnRow<T>(this ExcelWorks
}

throw new ExcelValidationException($"'{columnAttribute}' column is duplicated on {rowIndex}. row.");
}
}
}
}
}

/// <summary>
Expand Down Expand Up @@ -415,7 +415,7 @@ public static ExcelWorksheet DeleteColumns(this ExcelWorksheet worksheet, string
/// <param name="columnIndex"></param>
/// <param name="expectedValue"></param>
/// <param name="exceptionMessage">Custom exception message with format parameters: columnIndex, expectedValue</param>
public static void CheckAndThrowColumn(this ExcelWorksheet worksheet, int rowIndex, int columnIndex, string expectedValue, string exceptionMessage = null)
public static void CheckColumnAndThrow(this ExcelWorksheet worksheet, int rowIndex, int columnIndex, string expectedValue, string exceptionMessage = null)
{
if (!worksheet.GetColumns(rowIndex).Any(x => x.Value == expectedValue && x.Key == columnIndex))
{
Expand Down Expand Up @@ -455,7 +455,7 @@ public static void CheckHeadersAndThrow<T>(this ExcelWorksheet worksheet, int he

for (var i = 0; i < properyInfoAndColumnAttributes.Count; i++)
{
worksheet.CheckAndThrowColumn(headerRowIndex, i + 1, properyInfoAndColumnAttributes[i].ToString(), formattedExceptionMessage);
worksheet.CheckColumnAndThrow(headerRowIndex, i + 1, properyInfoAndColumnAttributes[i].ToString(), formattedExceptionMessage);
}
}

Expand All @@ -466,9 +466,9 @@ public static void CheckHeadersAndThrow<T>(this ExcelWorksheet worksheet, int he
/// <param name="rowIndex"></param>
/// <param name="columnIndex"></param>
/// <returns></returns>
public static bool CheckColumnValueIsNullOrEmpty(this ExcelWorksheet worksheet, int rowIndex, int columnIndex)
public static bool IsCellEmpty(this ExcelWorksheet worksheet, int rowIndex, int columnIndex)
{
object value = worksheet.Cells[rowIndex, columnIndex, rowIndex, columnIndex].Value;
object value = worksheet.Cells[rowIndex, columnIndex, rowIndex, columnIndex]?.Value;
return string.IsNullOrWhiteSpace(value?.ToString());
}

Expand Down Expand Up @@ -518,5 +518,27 @@ public static ExcelAddressBase GetValuedDimension(this ExcelWorksheet worksheet)

return hasValue ? new ExcelAddressBase(minRow, minCol, maxRow, maxCol) : null;
}

/// <summary>
/// Checks existence of columns on given row, and throws the <see cref="ExcelValidationException" /> if one of the
/// columns is missing
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="worksheet"></param>
/// <param name="rowIndex"></param>
/// <param name="exceptionMessage"></param>
public static void CheckExistenceOfColumnsAndThrow<T>(this ExcelWorksheet worksheet, int rowIndex, string exceptionMessage = null)
{
List<ExcelTableColumnAttributeAndPropertyInfo> properyInfoAndColumnAttributes = typeof(T).GetExcelTableColumnAttributesWithProperyInfo();
List<KeyValuePair<int, string>> columns = worksheet.GetColumns(rowIndex).ToList();

for (var i = 0; i < properyInfoAndColumnAttributes.Count; i++)
{
if (!columns.Any(x => x.Value.Equals(properyInfoAndColumnAttributes[i].ToString())))
{
throw new ExcelValidationException(string.Format(exceptionMessage ?? "'{0}' column is not found on the worksheet.", properyInfoAndColumnAttributes[i]));
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\EPPlus.Core.Extensions\EPPlus.Core.Extensions.csproj" />
</ItemGroup>
Expand Down
Loading

0 comments on commit 0800d27

Please sign in to comment.