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

Commit

Permalink
Implemented GetValuedDimension() method for ExcelWorksheet object, an…
Browse files Browse the repository at this point in the history
…d incremented version
  • Loading branch information
eraydin committed Nov 7, 2017
1 parent 915970e commit ed00396
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 53 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.1</VersionPrefix>
<VersionPrefix>1.2.2</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
57 changes: 53 additions & 4 deletions src/EPPlus.Core.Extensions/ExcelWorksheetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ public static class ExcelWorksheetExtensions
/// <returns></returns>
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
);
}

Expand Down Expand Up @@ -350,6 +352,53 @@ public static bool CheckColumnValueIsNullOrEmpty(this ExcelWorksheet worksheet,
return string.IsNullOrWhiteSpace(value?.ToString());
}

/// <summary>
/// Gets valued dimensions of worksheet
/// </summary>
/// <param name="worksheet"></param>
/// <returns></returns>
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;
}

/// <summary>
/// Sets the font of ExcelWorksheet cells from a Font object
/// </summary>
Expand Down
109 changes: 61 additions & 48 deletions test/EPPlus.Core.Extensions.Tests/ExcelWorksheetExtensions_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Drawing;
using System.Linq;

using EPPlus.Core.Extensions.Configuration;
using EPPlus.Core.Extensions.Validation;

using FluentAssertions;
Expand Down Expand Up @@ -66,7 +65,7 @@ public void Test_GetAsExcelTable_With_Header()
// Arrange
//-----------------------------------------------------------------------------------------------------------
ExcelWorksheet excelWorksheet = excelPackage.Workbook.Worksheets["TEST5"];

//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -97,10 +96,7 @@ public void Test_GetAsExcelTable_Without_Header()
// Assert
//-----------------------------------------------------------------------------------------------------------

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

Expand Down Expand Up @@ -152,7 +148,7 @@ public void Test_Worksheet_AsEnumerable()
//-----------------------------------------------------------------------------------------------------------
ExcelWorksheet worksheet1 = excelPackage.Workbook.Worksheets["TEST4"];
ExcelWorksheet worksheet2 = excelPackage.Workbook.Worksheets["TEST5"];

//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -182,7 +178,7 @@ public void Test_Worksheet_ToList()
//-----------------------------------------------------------------------------------------------------------
ExcelWorksheet worksheet1 = excelPackage.Workbook.Worksheets["TEST4"];
ExcelWorksheet worksheet2 = excelPackage.Workbook.Worksheets["TEST5"];

//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -213,14 +209,14 @@ public void Should_work_AddObjects_method_with_parameters()
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets["TEST5"];

var stocks = new List<StocksNullable>
{
new StocksNullable
{
Barcode = "barcode123",
Quantity = 5,
UpdatedDate = DateTime.MaxValue
}
};
{
new StocksNullable
{
Barcode = "barcode123",
Quantity = 5,
UpdatedDate = DateTime.MaxValue
}
};

//-----------------------------------------------------------------------------------------------------------
// Act
Expand All @@ -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<StocksNullable>
{
new StocksNullable
{
Barcode = "barcode123",
Quantity = 5,
UpdatedDate = DateTime.MaxValue
}
};
{
new StocksNullable
{
Barcode = "barcode123",
Quantity = 5,
UpdatedDate = DateTime.MaxValue
}
};

//-----------------------------------------------------------------------------------------------------------
// Act
Expand All @@ -275,14 +271,14 @@ public void Should_AddObjects_method_work_without_parameters()
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets["TEST5"];

var stocks = new List<StocksNullable>
{
new StocksNullable
{
Barcode = "barcode123",
Quantity = 5,
UpdatedDate = DateTime.MaxValue
}
};
{
new StocksNullable
{
Barcode = "barcode123",
Quantity = 5,
UpdatedDate = DateTime.MaxValue
}
};

//-----------------------------------------------------------------------------------------------------------
// Act
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -458,11 +445,14 @@ public void Test_StocksValidation()
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action action = () => { list = worksheet.ToList<StocksValidation>(configuration =>
Action action = () =>
{
configuration.SkipCastingErrors = false;
configuration.HasHeaderRow = true;
}); };
list = worksheet.ToList<StocksValidation>(configuration =>
{
configuration.SkipCastingErrors = false;
configuration.HasHeaderRow = true;
});
};

//-----------------------------------------------------------------------------------------------------------
// Assert
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit ed00396

Please sign in to comment.