From 0800d271b6fd6ed63760abe91a4cc6f03d78c06b Mon Sep 17 00:00:00 2001 From: eraydin Date: Wed, 27 Jun 2018 15:58:57 +0300 Subject: [PATCH] - Minor performance improvements, - Renamed CheckAndThrowColumn() method as CheckColumnAndThrow(), - Implemented CheckExistenceOfColumnsAndThrow() method, - Incremented version (v2.2.3) --- common.props | 4 +- .../EPPlus.Core.Extensions.csproj | 15 +-- .../ExcelTableExtensions.cs | 4 +- .../ExcelWorksheetExtensions.cs | 38 ++++-- .../EPPlus.Core.Extensions.Tests.csproj | 3 +- .../ExcelWorksheetExtensions_Tests.cs | 125 +++++++++++------- 6 files changed, 121 insertions(+), 68 deletions(-) diff --git a/common.props b/common.props index 811f7ea..502a805 100644 --- a/common.props +++ b/common.props @@ -1,7 +1,7 @@ - 2.2.2 - An extensions library for both EPPlus and EPPlus.Core packages to generate and manipulate Excel files easily. + 2.2.3 + An extensions library for EPPlus package to generate and manipulate Excel files easily. $(NoWarn);CS1591 diff --git a/src/EPPlus.Core.Extensions/EPPlus.Core.Extensions.csproj b/src/EPPlus.Core.Extensions/EPPlus.Core.Extensions.csproj index e8dab3b..3d95f06 100644 --- a/src/EPPlus.Core.Extensions/EPPlus.Core.Extensions.csproj +++ b/src/EPPlus.Core.Extensions/EPPlus.Core.Extensions.csproj @@ -1,12 +1,11 @@  - - + + - netstandard2.0;net461; - - + netstandard2.0;net461 + - - - + + + \ No newline at end of file diff --git a/src/EPPlus.Core.Extensions/ExcelTableExtensions.cs b/src/EPPlus.Core.Extensions/ExcelTableExtensions.cs index e197d54..287673b 100644 --- a/src/EPPlus.Core.Extensions/ExcelTableExtensions.cs +++ b/src/EPPlus.Core.Extensions/ExcelTableExtensions.cs @@ -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++) @@ -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 map in mapping) { diff --git a/src/EPPlus.Core.Extensions/ExcelWorksheetExtensions.cs b/src/EPPlus.Core.Extensions/ExcelWorksheetExtensions.cs index 08ce4aa..0da1cc3 100644 --- a/src/EPPlus.Core.Extensions/ExcelWorksheetExtensions.cs +++ b/src/EPPlus.Core.Extensions/ExcelWorksheetExtensions.cs @@ -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 column in worksheet.GetColumns(rowIndex).Where(x => !string.IsNullOrEmpty(x.Value))) { @@ -349,7 +349,7 @@ public static void CheckAndThrowIfDuplicatedColumnsFoundOnRow(this ExcelWorkshee } } - public static void CheckAndThrowIfDuplicatedColumnsFoundOnRow(this ExcelWorksheet worksheet, int rowIndex, string exceptionMessage = null) + public static void CheckAndThrowIfDuplicatedColumnsFound(this ExcelWorksheet worksheet, int rowIndex, string exceptionMessage = null) { List properyInfoAndColumnAttributes = typeof(T).GetExcelTableColumnAttributesWithProperyInfo(); @@ -363,8 +363,8 @@ public static void CheckAndThrowIfDuplicatedColumnsFoundOnRow(this ExcelWorks } throw new ExcelValidationException($"'{columnAttribute}' column is duplicated on {rowIndex}. row."); - } - } + } + } } /// @@ -415,7 +415,7 @@ public static ExcelWorksheet DeleteColumns(this ExcelWorksheet worksheet, string /// /// /// Custom exception message with format parameters: columnIndex, expectedValue - 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)) { @@ -455,7 +455,7 @@ public static void CheckHeadersAndThrow(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); } } @@ -466,9 +466,9 @@ public static void CheckHeadersAndThrow(this ExcelWorksheet worksheet, int he /// /// /// - 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()); } @@ -518,5 +518,27 @@ public static ExcelAddressBase GetValuedDimension(this ExcelWorksheet worksheet) return hasValue ? new ExcelAddressBase(minRow, minCol, maxRow, maxCol) : null; } + + /// + /// Checks existence of columns on given row, and throws the if one of the + /// columns is missing + /// + /// + /// + /// + /// + public static void CheckExistenceOfColumnsAndThrow(this ExcelWorksheet worksheet, int rowIndex, string exceptionMessage = null) + { + List properyInfoAndColumnAttributes = typeof(T).GetExcelTableColumnAttributesWithProperyInfo(); + List> 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])); + } + } + } } } diff --git a/test/EPPlus.Core.Extensions.Tests/EPPlus.Core.Extensions.Tests.csproj b/test/EPPlus.Core.Extensions.Tests/EPPlus.Core.Extensions.Tests.csproj index 1ea3626..6ebf953 100644 --- a/test/EPPlus.Core.Extensions.Tests/EPPlus.Core.Extensions.Tests.csproj +++ b/test/EPPlus.Core.Extensions.Tests/EPPlus.Core.Extensions.Tests.csproj @@ -14,7 +14,8 @@ - + + diff --git a/test/EPPlus.Core.Extensions.Tests/ExcelWorksheetExtensions_Tests.cs b/test/EPPlus.Core.Extensions.Tests/ExcelWorksheetExtensions_Tests.cs index 95fa13c..2e91f1e 100644 --- a/test/EPPlus.Core.Extensions.Tests/ExcelWorksheetExtensions_Tests.cs +++ b/test/EPPlus.Core.Extensions.Tests/ExcelWorksheetExtensions_Tests.cs @@ -216,11 +216,11 @@ public void Should_check_and_throw_exception_if_column_value_is_wrong_on_specifi //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- - Action action1 = () => worksheet.CheckAndThrowColumn(2, 3, "Barcode", "Barcode column is missing"); + Action action1 = () => worksheet.CheckColumnAndThrow(2, 3, "Barcode", "Barcode column is missing"); - Action action2 = () => worksheet.CheckAndThrowColumn(2, 1, "Barcode"); + Action action2 = () => worksheet.CheckColumnAndThrow(2, 1, "Barcode"); - Action action3 = () => worksheet.CheckAndThrowColumn(3, 14, "Barcode"); + Action action3 = () => worksheet.CheckColumnAndThrow(3, 14, "Barcode"); //----------------------------------------------------------------------------------------------------------- // Assert @@ -231,45 +231,39 @@ public void Should_check_and_throw_exception_if_column_value_is_wrong_on_specifi } [Fact] - public void Should_check_if_duplicated_column_exists_on_a_row() + public void Should_check_and_throw_if_duplicated_column_found_on_a_row() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- ExcelWorksheet worksheet1 = excelPackage2.GetWorksheet("RandomOrderedColumns"); - ExcelWorksheet worksheet2 = excelPackage2.GetWorksheet("EmptyWorksheet"); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- - bool duplicatedColumn = worksheet1.IsColumnDuplicatedOnRow(1, "Barcode"); - bool notDuplicatedColumn = worksheet1.IsColumnDuplicatedOnRow(1, "Quantity"); - bool notfoundColumn = worksheet1.IsColumnDuplicatedOnRow(1, "Barcode1asdacfddsd"); - bool emptyRow = worksheet2.IsColumnDuplicatedOnRow(2, "empty"); + Action act1 = () => worksheet1.CheckAndThrowIfDuplicatedColumnsFound(1); + Action act2 = () => worksheet1.CheckAndThrowIfDuplicatedColumnsFound(1, "'{0}' column is duplicated (rowIndex: {1})"); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- - duplicatedColumn.Should().BeTrue(); - notDuplicatedColumn.Should().BeFalse(); - notfoundColumn.Should().BeFalse(); - emptyRow.Should().BeFalse(); + act1.Should().Throw().WithMessage("'Barcode' column is duplicated on 1. row."); + act2.Should().Throw().WithMessage("'Barcode' column is duplicated (rowIndex: 1)"); } [Fact] - public void Should_check_and_throw_if_duplicated_column_found_on_a_row() + public void Should_check_and_throw_if_duplicated_column_found_on_a_row_with_object() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- - ExcelWorksheet worksheet1 = excelPackage2.GetWorksheet("RandomOrderedColumns"); + ExcelWorksheet worksheet1 = excelPackage2.GetWorksheet("RandomOrderedColumns"); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- - Action act1 = () => worksheet1.CheckAndThrowIfDuplicatedColumnsFoundOnRow(1); - Action act2 = () => worksheet1.CheckAndThrowIfDuplicatedColumnsFoundOnRow(1, "'{0}' column is duplicated (rowIndex: {1})"); - + Action act1 = () => worksheet1.CheckAndThrowIfDuplicatedColumnsFound(1); + Action act2 = () => worksheet1.CheckAndThrowIfDuplicatedColumnsFound(1, "'{0}' column is duplicated (rowIndex: {1})"); //----------------------------------------------------------------------------------------------------------- // Assert @@ -278,48 +272,51 @@ public void Should_check_and_throw_if_duplicated_column_found_on_a_row() act2.Should().Throw().WithMessage("'Barcode' column is duplicated (rowIndex: 1)"); } - [Fact] - public void Should_check_and_throw_if_duplicated_column_found_on_a_row_with_object() + public void Should_check_if_a_cell_is_null_or_empty_on_given_index() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- - ExcelWorksheet worksheet1 = excelPackage2.GetWorksheet("RandomOrderedColumns"); + ExcelWorksheet worksheet = excelPackage1.GetWorksheet("TEST6"); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- - Action act1 = () => worksheet1.CheckAndThrowIfDuplicatedColumnsFoundOnRow(1); - Action act2 = () => worksheet1.CheckAndThrowIfDuplicatedColumnsFoundOnRow(1, "'{0}' column is duplicated (rowIndex: {1})"); - + bool result1 = worksheet.IsCellEmpty(3, 4); + bool result2 = worksheet.IsCellEmpty(2, 1); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- - act1.Should().Throw().WithMessage("'Barcode' column is duplicated on 1. row."); - act2.Should().Throw().WithMessage("'Barcode' column is duplicated (rowIndex: 1)"); + result1.Should().BeTrue(); + result2.Should().BeFalse(); } [Fact] - public void Should_check_if_column_value_is_null_or_empty_on_given_index() + public void Should_check_if_duplicated_column_exists_on_a_row() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- - ExcelWorksheet worksheet = excelPackage1.GetWorksheet("TEST6"); + ExcelWorksheet worksheet1 = excelPackage2.GetWorksheet("RandomOrderedColumns"); + ExcelWorksheet worksheet2 = excelPackage2.GetWorksheet("EmptyWorksheet"); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- - bool result1 = worksheet.CheckColumnValueIsNullOrEmpty(3, 4); - bool result2 = worksheet.CheckColumnValueIsNullOrEmpty(2, 1); + bool duplicatedColumn = worksheet1.IsColumnDuplicatedOnRow(1, "Barcode"); + bool notDuplicatedColumn = worksheet1.IsColumnDuplicatedOnRow(1, "Quantity"); + bool notfoundColumn = worksheet1.IsColumnDuplicatedOnRow(1, "Barcode1asdacfddsd"); + bool emptyRow = worksheet2.IsColumnDuplicatedOnRow(2, "empty"); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- - result1.Should().BeTrue(); - result2.Should().BeFalse(); + duplicatedColumn.Should().BeTrue(); + notDuplicatedColumn.Should().BeFalse(); + notfoundColumn.Should().BeFalse(); + emptyRow.Should().BeFalse(); } [Fact] @@ -400,15 +397,17 @@ public void Should_delete_a_column_by_using_header_text() // Arrange //----------------------------------------------------------------------------------------------------------- ExcelWorksheet worksheet = excelPackage1.GetWorksheet("TEST6"); + const string columnName = "Quantity"; //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- - worksheet.DeleteColumn("Quantity"); + worksheet.DeleteColumn(columnName); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- + worksheet.GetColumns(1).Any(x => x.Value == columnName).Should().BeFalse(); worksheet.GetValuedDimension().End.Column.Should().Be(2); worksheet.Cells[2, 2, 2, 2].Text.Should().Be("UpdatedDate"); } @@ -420,21 +419,23 @@ public void Should_delete_columns_by_given_header_text() // Arrange //----------------------------------------------------------------------------------------------------------- ExcelWorksheet worksheet = excelPackage1.GetWorksheet("TEST6"); + const string columnName = "Quantity"; ExcelAddressBase valuedDimension = worksheet.GetValuedDimension(); - worksheet.ChangeCellValue(2, valuedDimension.End.Column + 1, "Quantity"); - worksheet.ChangeCellValue(2, valuedDimension.End.Column + 2, "Quantity"); - worksheet.ChangeCellValue(2, valuedDimension.End.Column + 3, "Quantity"); + worksheet.ChangeCellValue(2, valuedDimension.End.Column + 1, columnName); + worksheet.ChangeCellValue(2, valuedDimension.End.Column + 2, columnName); + worksheet.ChangeCellValue(2, valuedDimension.End.Column + 3, columnName); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- - worksheet.DeleteColumns("Quantity"); + worksheet.DeleteColumns(columnName); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- + worksheet.GetColumns(2).Any(x => x.Value == columnName).Should().BeFalse(); worksheet.GetValuedDimension().End.Column.Should().Be(2); worksheet.Cells[2, 2, 2, 2].Text.Should().Be("UpdatedDate"); } @@ -690,7 +691,7 @@ public void Should_throw_an_exception_when_columns_of_worksheet_not_matched_with //----------------------------------------------------------------------------------------------------------- Action action1 = () => worksheet1.CheckHeadersAndThrow(2, "The {0}.column of worksheet should be '{1}'."); Action action2 = () => worksheet1.CheckHeadersAndThrow(2); - Action action3 = () => worksheet1.CheckHeadersAndThrow(2); + Action action3 = () => worksheet1.CheckHeadersAndThrow(2); Action action4 = () => worksheet1.CheckHeadersAndThrow(2); Action actionForEmptySheet1 = () => emptySheet1.CheckHeadersAndThrow(1, "The {0}.column of worksheet should be '{1}'."); @@ -699,17 +700,13 @@ public void Should_throw_an_exception_when_columns_of_worksheet_not_matched_with //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- - action1.Should().Throw().And.Message.Should() - .Be("The 1.column of worksheet should be 'Name'."); - action2.Should().Throw().And.Message.Should() - .Be("The 1. column of worksheet should be 'Name'."); + action1.Should().Throw().And.Message.Should().Be("The 1.column of worksheet should be 'Name'."); + action2.Should().Throw().And.Message.Should().Be("The 1. column of worksheet should be 'Name'."); action3.Should().NotThrow(); action4.Should().Throw(); - actionForEmptySheet1.Should().Throw().And.Message.Should() - .Be("The 1.column of worksheet should be 'Barcode'."); - actionForEmptySheet2.Should().Throw().And.Message.Should() - .Be("The 1.column of worksheet should be 'LicensePlate'."); + actionForEmptySheet1.Should().Throw().And.Message.Should().Be("The 1.column of worksheet should be 'Barcode'."); + actionForEmptySheet2.Should().Throw().And.Message.Should().Be("The 1.column of worksheet should be 'LicensePlate'."); } [Fact] @@ -798,5 +795,39 @@ public void Should_valued_dimension_be_E9G13() valuedDimension.End.Column.Should().Be(7); valuedDimension.End.Row.Should().Be(13); } - } + + + [Fact] + public void Should_check_columns_of_given_row_whether_it_contains_same_values_with_excel_exportable_object_properties() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + ExcelWorksheet worksheet1 = excelPackage2.GetWorksheet("RandomOrderedColumns"); + ExcelWorksheet emptySheet1 = excelPackage1.GetWorksheet("EmptySheet"); + ExcelWorksheet emptySheet2 = excelPackage1.GetWorksheet("EmptySheet"); + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action action1 = () => worksheet1.CheckExistenceOfColumnsAndThrow(2, "'{0}' column is not found on the Excel."); + Action action2 = () => worksheet1.CheckExistenceOfColumnsAndThrow(2); + Action action3 = () => worksheet1.CheckExistenceOfColumnsAndThrow(1); + Action action4 = () => worksheet1.CheckExistenceOfColumnsAndThrow(2); + + Action actionForEmptySheet1 = () => emptySheet1.CheckExistenceOfColumnsAndThrow(1, "'{0}' column is not found on the worksheet."); + Action actionForEmptySheet2 = () => emptySheet2.CheckExistenceOfColumnsAndThrow(1, "'{0}' column is not found on the worksheet."); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + action1.Should().Throw().And.Message.Should().Be("'Name' column is not found on the Excel."); + action2.Should().Throw().And.Message.Should().Be("'Name' column is not found on the worksheet."); + action3.Should().NotThrow(); + action4.Should().Throw(); + + 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."); + } + } }