diff --git a/common.props b/common.props index 931d193..502fdeb 100644 --- a/common.props +++ b/common.props @@ -1,6 +1,6 @@ - 1.2.1 + 1.2.2 An extensions library for both EPPlus and EPPlus.Core packages to generate and manipulate Excel files easily. $(NoWarn);CS1591 diff --git a/src/EPPlus.Core.Extensions/ExcelWorksheetExtensions.cs b/src/EPPlus.Core.Extensions/ExcelWorksheetExtensions.cs index 5cc7cdc..d596891 100644 --- a/src/EPPlus.Core.Extensions/ExcelWorksheetExtensions.cs +++ b/src/EPPlus.Core.Extensions/ExcelWorksheetExtensions.cs @@ -23,11 +23,13 @@ public static class ExcelWorksheetExtensions /// public static ExcelAddress GetDataBounds(this ExcelWorksheet worksheet, bool hasHeaderRow = true) { + ExcelAddressBase valuedDimension = worksheet.GetValuedDimension(); + return new ExcelAddress( - worksheet.Dimension.Start.Row + (hasHeaderRow ? 1 : 0), - worksheet.Dimension.Start.Column, - worksheet.Dimension.End.Row, - worksheet.Dimension.End.Column + valuedDimension.Start.Row + (hasHeaderRow ? 1 : 0), + valuedDimension.Start.Column, + valuedDimension.End.Row, + valuedDimension.End.Column ); } @@ -350,6 +352,53 @@ public static bool CheckColumnValueIsNullOrEmpty(this ExcelWorksheet worksheet, return string.IsNullOrWhiteSpace(value?.ToString()); } + /// + /// Gets valued dimensions of worksheet + /// + /// + /// + public static ExcelAddressBase GetValuedDimension(this ExcelWorksheet worksheet) + { + ExcelAddressBase dimension = worksheet.Dimension; + + if (dimension == null) + { + return null; + } + + ExcelRange cells = worksheet.Cells[dimension.Address]; + int minRow = 0, minCol = 0, maxRow = 0, maxCol = 0; + var hasValue = false; + foreach (ExcelRangeBase cell in cells.Where(cell => cell.Value != null)) + { + if (!hasValue) + { + minRow = cell.Start.Row; + minCol = cell.Start.Column; + maxRow = cell.End.Row; + maxCol = cell.End.Column; + hasValue = true; + } + else + { + if (cell.Start.Column < minCol) + { + minCol = cell.Start.Column; + } + if (cell.End.Row > maxRow) + { + maxRow = cell.End.Row; + } + if (cell.End.Column > maxCol) + { + maxCol = cell.End.Column; + } + } + } + + return hasValue ? new ExcelAddressBase(minRow, minCol, maxRow, maxCol) : null; + } + /// /// Sets the font of ExcelWorksheet cells from a Font object /// diff --git a/test/EPPlus.Core.Extensions.Tests/ExcelWorksheetExtensions_Tests.cs b/test/EPPlus.Core.Extensions.Tests/ExcelWorksheetExtensions_Tests.cs index 5bb974c..ca7a765 100644 --- a/test/EPPlus.Core.Extensions.Tests/ExcelWorksheetExtensions_Tests.cs +++ b/test/EPPlus.Core.Extensions.Tests/ExcelWorksheetExtensions_Tests.cs @@ -4,7 +4,6 @@ using System.Drawing; using System.Linq; -using EPPlus.Core.Extensions.Configuration; using EPPlus.Core.Extensions.Validation; using FluentAssertions; @@ -66,7 +65,7 @@ public void Test_GetAsExcelTable_With_Header() // Arrange //----------------------------------------------------------------------------------------------------------- ExcelWorksheet excelWorksheet = excelPackage.Workbook.Worksheets["TEST5"]; - + //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- @@ -97,10 +96,7 @@ public void Test_GetAsExcelTable_Without_Header() // Assert //----------------------------------------------------------------------------------------------------------- - IList listOfStocks = excelTable.ToList(configuration => - { - configuration.SkipCastingErrors = true; - }); + IList listOfStocks = excelTable.ToList(configuration => { configuration.SkipCastingErrors = true; }); listOfStocks.Count.Should().Be(4); } @@ -152,7 +148,7 @@ public void Test_Worksheet_AsEnumerable() //----------------------------------------------------------------------------------------------------------- ExcelWorksheet worksheet1 = excelPackage.Workbook.Worksheets["TEST4"]; ExcelWorksheet worksheet2 = excelPackage.Workbook.Worksheets["TEST5"]; - + //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- @@ -182,7 +178,7 @@ public void Test_Worksheet_ToList() //----------------------------------------------------------------------------------------------------------- ExcelWorksheet worksheet1 = excelPackage.Workbook.Worksheets["TEST4"]; ExcelWorksheet worksheet2 = excelPackage.Workbook.Worksheets["TEST5"]; - + //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- @@ -213,14 +209,14 @@ public void Should_work_AddObjects_method_with_parameters() ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets["TEST5"]; var stocks = new List - { - new StocksNullable - { - Barcode = "barcode123", - Quantity = 5, - UpdatedDate = DateTime.MaxValue - } - }; + { + new StocksNullable + { + Barcode = "barcode123", + Quantity = 5, + UpdatedDate = DateTime.MaxValue + } + }; //----------------------------------------------------------------------------------------------------------- // Act @@ -246,14 +242,14 @@ public void Should_throw_exception_when_the_parameters_of_AddObjects_method_are_ //----------------------------------------------------------------------------------------------------------- ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets["TEST5"]; var stocks = new List - { - new StocksNullable - { - Barcode = "barcode123", - Quantity = 5, - UpdatedDate = DateTime.MaxValue - } - }; + { + new StocksNullable + { + Barcode = "barcode123", + Quantity = 5, + UpdatedDate = DateTime.MaxValue + } + }; //----------------------------------------------------------------------------------------------------------- // Act @@ -275,14 +271,14 @@ public void Should_AddObjects_method_work_without_parameters() ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets["TEST5"]; var stocks = new List - { - new StocksNullable - { - Barcode = "barcode123", - Quantity = 5, - UpdatedDate = DateTime.MaxValue - } - }; + { + new StocksNullable + { + Barcode = "barcode123", + Quantity = 5, + UpdatedDate = DateTime.MaxValue + } + }; //----------------------------------------------------------------------------------------------------------- // Act @@ -402,20 +398,11 @@ public void Test_CheckAndThrowColumn() //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- - Action action1 = () => - { - worksheet.CheckAndThrowColumn(1, 3, "Barcode", "Barcode column is missing"); - }; + Action action1 = () => { worksheet.CheckAndThrowColumn(1, 3, "Barcode", "Barcode column is missing"); }; - Action action2 = () => - { - worksheet.CheckAndThrowColumn(1, 1, "Barcode"); - }; + Action action2 = () => { worksheet.CheckAndThrowColumn(1, 1, "Barcode"); }; - Action action3 = () => - { - worksheet.CheckAndThrowColumn(2, 14, "Barcode"); - }; + Action action3 = () => { worksheet.CheckAndThrowColumn(2, 14, "Barcode"); }; //----------------------------------------------------------------------------------------------------------- // Assert @@ -458,11 +445,14 @@ public void Test_StocksValidation() //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- - Action action = () => { list = worksheet.ToList(configuration => + Action action = () => { - configuration.SkipCastingErrors = false; - configuration.HasHeaderRow = true; - }); }; + list = worksheet.ToList(configuration => + { + configuration.SkipCastingErrors = false; + configuration.HasHeaderRow = true; + }); + }; //----------------------------------------------------------------------------------------------------------- // Assert @@ -566,5 +556,28 @@ public void Should_set_vertical_alignment_of_the_worksheet() //----------------------------------------------------------------------------------------------------------- worksheet.Cells.Style.VerticalAlignment.Should().Be(ExcelVerticalAlignment.Justify); } + + [Fact] + public void Should_valued_dimension_be_E9G13() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets["TEST4"]; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + ExcelAddressBase valuedDimension = worksheet.GetValuedDimension(); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + valuedDimension.Address.Should().Be("E9:G13"); + valuedDimension.Start.Column.Should().Be(5); + valuedDimension.Start.Row.Should().Be(9); + valuedDimension.End.Column.Should().Be(7); + valuedDimension.End.Row.Should().Be(13); + } } }