Skip to content

Commit

Permalink
Merge pull request #52 from a-frazier/issue-51
Browse files Browse the repository at this point in the history
Fix Issue #51: Empty Lists with Applied NumberFormat Throw ArgumentOutOfRangeException
  • Loading branch information
jordangray committed Feb 2, 2016
2 parents d8ce6d9 + d904bb9 commit 7d27829
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/WebApiContrib.Formatting.Xlsx/XlsxDocumentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public void FormatColumn(int column, string format, bool skipHeaderRow = true)
{
var firstRow = skipHeaderRow ? 2 : 1;

Worksheet.Cells[firstRow, column, RowCount, column].Style.Numberformat.Format = format;
if (firstRow <= RowCount)
Worksheet.Cells[firstRow, column, RowCount, column].Style.Numberformat.Format = format;
}

public static bool IsExcelSupportedType(object expression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void WriteToStreamAsync_WithArrayOfSimpleTestItem_WritesExcelDocumentToSt
var expected = new[] { new object[] { "Value1", "Value2" },
new object[] { data[0].Value1, data[0].Value2 },
new object[] { data[1].Value1, data[1].Value2 } };

GenerateAndCompareWorksheet(data, expected);
}

Expand All @@ -104,7 +104,7 @@ public void WriteToStreamAsync_WithArrayOfFormatStringTestItem_ValuesFormattedAp
var excelDate = (tomorrow - new DateTime(1900, 1, 1)).TotalDays + 2;
var excelDateStr = excelDate.ToString();


var data = new[] { new FormatStringTestItem { Value1 = tomorrow,
Value2 = tomorrow,
Value3 = tomorrow,
Expand Down Expand Up @@ -134,7 +134,7 @@ public void WriteToStreamAsync_WithArrayOfBooleanTestItem_TrueOrFalseValueUsedAs
Value2 = false,
Value3 = false,
Value4 = false },

new BooleanTestItem { Value1 = true,
Value2 = true,
Value3 = null,
Expand All @@ -159,6 +159,16 @@ public void WriteToStreamAsync_WithSimpleTestItem_WritesExcelDocumentToStream()
GenerateAndCompareWorksheet(data, expected);
}

[TestMethod]
public void WriteToStreamAsync_WithEmptyListOfComplexTestItem_DoesNotCrash()
{
var data = new ComplexTestItem[0];

var expected = new[] { new object[] { "Header 4", "Value1", "Header 5", "Header 3", "Value2" } };

GenerateAndCompareWorksheet(data, expected);
}

[TestMethod]
public void WriteToStreamAsync_WithComplexTestItem_WritesExcelDocumentToStream()
{
Expand Down Expand Up @@ -198,7 +208,7 @@ public void WriteToStreamAsync_WithListOfComplexTestItem_WritesExcelDocumentToSt
var expected = new[] { new object[] { "Header 4", "Value1", "Header 5", "Header 3", "Value2" },
new object[] { data[0].Value4, data[0].Value1, data[0].Value5.ToString(), data[0].Value3.ToString(), data[0].Value2 },
new object[] { data[1].Value4, data[1].Value1, data[1].Value5.ToString(), data[1].Value3.ToString(), data[1].Value2 } };

var sheet = GenerateAndCompareWorksheet(data, expected);

Assert.AreEqual("???.???", sheet.Cells[2, 1].Style.Numberformat.Format, "NumberFormat of A2 is incorrect.");
Expand Down Expand Up @@ -227,7 +237,7 @@ public void WriteToStreamAsync_WithArrayOfAnonymousObject_WritesExcelDocumentToS
var expected = new[] { new object[] { "prop1", "prop2", "prop3" },
new object[] { data[0].prop1, data[0].prop2, data[0].prop3 },
new object[] { data[1].prop1, data[1].prop2, data[1].prop3 } };

GenerateAndCompareWorksheet(data, expected);
}

Expand Down Expand Up @@ -483,7 +493,7 @@ public override ChannelBinding GetChannelBinding(ChannelBindingKind kind)
public class FakeHttpContext : HttpContextBase
{
private IDictionary _items = new Dictionary<string, object>();

public override IDictionary Items
{
get
Expand Down

0 comments on commit 7d27829

Please sign in to comment.