Skip to content

Commit

Permalink
Added TrimLastEmptyRows and IsLastRowEmpty.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecole committed Aug 8, 2018
1 parent 267c59e commit 9fb090c
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/EPPlus.Extensions.Tests/EPPlus.Extensions.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="MarvelWithSpaces.csv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="Marvel.csv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
51 changes: 49 additions & 2 deletions src/EPPlus.Extensions.Tests/ExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,40 @@ public void ToDataSet_WhenCsv_AllowsDateFormat()
Convert.ToDateTime(cellValue).ToString("o").ShouldBe(DateTime.Parse("04/22/1950 08:41 PM").ToString("o"));
}

public void IsLastRowEmpty_ReturnsFalse_WhenLastRowIsNotEmpty()
{
var package = GetMarvelCsvPackage();
package.Workbook.Worksheets["Marvel"].IsLastRowEmpty().ShouldBeFalse();
}

public void IsLastRowEmpty_ReturnsTrue_WhenLastRowIsEmpty()
{
var package = GetMarvelWithSpacesCsvPackage();
package.Workbook.Worksheets["Marvel"].IsLastRowEmpty().ShouldBeTrue();
}

public void TrimLastEmptyRows_RemovesEmptyRows()
{
var package = GetMarvelWithSpacesCsvPackage();
var sheet = package.Workbook.Worksheets["Marvel"];

sheet.TrimLastEmptyRows();

sheet.IsLastRowEmpty().ShouldBeFalse();
sheet.Dimension.End.Row.ShouldBe(11);
}

public void TrimLastEmptyRows_DoesRemoveNotEmptyRows()
{
var package = GetMarvelCsvPackage();
var sheet = package.Workbook.Worksheets["Marvel"];

sheet.TrimLastEmptyRows();

sheet.IsLastRowEmpty().ShouldBeFalse();
sheet.Dimension.End.Row.ShouldBe(11);
}

private static ExcelPackage GetMarvelPackage()
{
var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Marvel.xlsx");
Expand All @@ -147,8 +181,21 @@ private static ExcelPackage GetMarvelCsvPackage()
var file = new FileInfo(path);
var package = new ExcelPackage();

var textFormat = new ExcelTextFormat();
textFormat.TextQualifier = '"';
var textFormat = new ExcelTextFormat { TextQualifier = '"' };

var sheet = package.Workbook.Worksheets.Add("Marvel");
sheet.Cells.LoadFromText(File.ReadAllText(file.FullName), textFormat);

return package;
}

private static ExcelPackage GetMarvelWithSpacesCsvPackage()
{
var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "MarvelWithSpaces.csv");
var file = new FileInfo(path);
var package = new ExcelPackage();

var textFormat = new ExcelTextFormat { TextQualifier = '"' };

var sheet = package.Workbook.Worksheets.Add("Marvel");
sheet.Cells.LoadFromText(File.ReadAllText(file.FullName), textFormat);
Expand Down
2 changes: 1 addition & 1 deletion src/EPPlus.Extensions.Tests/Marvel.csv
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ Steve,Rogers,Captain America,04/22/2017 08:41 PM
James,Barnes,The Winter Solider,04/22/2017 08:41 PM
Sam,Wilson,Falcon,04/22/2017 08:41 PM
Wanda,Maximoff,Scarlet Witch,04/22/2017 08:41 PM
Natasha,Romanoff,Black WIdow,04/22/2017 08:41 PM
Natasha,Romanoff,Black Widow,04/22/2017 08:41 PM
13 changes: 13 additions & 0 deletions src/EPPlus.Extensions.Tests/MarvelWithSpaces.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
First Name,Last Name,Alter Ego,Birth Date
Tony,Stark,Iron Man,04/22/1950 08:41 PM
Peter,Parker,Spider-man,04/22/2017 08:41 PM
Bruce,Banner,Hulk,04/22/2017 08:41 PM
Clint,Barton,Hawkeye,04/22/2017 08:41 PM
James,Rhodes,War Machine,04/22/2017 08:41 PM
Steve,Rogers,Captain America,04/22/2017 08:41 PM
James,Barnes,The Winter Solider,04/22/2017 08:41 PM
Sam,Wilson,Falcon,04/22/2017 08:41 PM
Wanda,Maximoff,Scarlet Witch,04/22/2017 08:41 PM
Natasha,Romanoff,Black Widow,04/22/2017 08:41 PM


22 changes: 22 additions & 0 deletions src/EPPlus.Extensions/EPPlusExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using OfficeOpenXml;
using System;
using System.Collections.Generic;

namespace EPPlus.Extensions
{
Expand Down Expand Up @@ -67,5 +68,26 @@ public static DataSet ToDataSet(this ExcelPackage package, int headerRow = 0)

return result;
}

public static void TrimLastEmptyRows(this ExcelWorksheet worksheet)
{
while (worksheet.IsLastRowEmpty())
{
worksheet.DeleteRow(worksheet.Dimension.End.Row, 1);
}
}

public static bool IsLastRowEmpty(this ExcelWorksheet worksheet)
{
var empties = new List<bool>();

for (var index = 1; index <= worksheet.Dimension.End.Column; index++)
{
var value = worksheet.Cells[worksheet.Dimension.End.Row, index].Value;
empties.Add(value == null || string.IsNullOrWhiteSpace(value.ToString()));
}

return empties.All(e => e);
}
}
}

0 comments on commit 9fb090c

Please sign in to comment.