Skip to content

Commit

Permalink
#1058, #969 - added unit test for reading column headers/keys from at…
Browse files Browse the repository at this point in the history
…tribute
  • Loading branch information
swmal committed Nov 22, 2023
1 parent 3804d33 commit 4063b0c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
15 changes: 0 additions & 15 deletions src/EPPlus/LoadFunctions/LoadFromCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,21 +203,6 @@ private void SetValuesAndFormulas(object[,] values, Dictionary<int, FormulaCell>
{
if(!string.IsNullOrEmpty(colInfo.Path) && colInfo.Path.Contains("."))
{
//if(colInfo.IsDictionaryProperty)
//{
// var dict = GetValueByPath(item, colInfo.Path) as Dictionary<string, object>;
// if(dict != null)
// {
// if (dict.ContainsKey(colInfo.DictinaryKey))
// {
// values[row, col++] = dict[colInfo.DictinaryKey];
// }
// }
//}
//else
//{
// values[row, col++] = GetValueByPath(item, colInfo.Path);
//}
values[row, col++] = GetValueByPath(item, colInfo.Path);
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ public class TestClass3
public Dictionary<string, object> Columns { get; set; }
}

[EpplusTable]
public class TestClass4
{
[EpplusTableColumn(Order = 1)]
public string Name { get; set; }

[EPPlusDictionaryColumn(Order = 2, ColumnHeaders = new string[] { "C", "B", "A" })]
public Dictionary<string, object> Columns { get; set; }
}


[TestMethod]
public void ShouldReadColumnsAndValuesFromDictionaryProperty()
Expand Down Expand Up @@ -174,5 +184,32 @@ public void ShouldUseDefaultKeys()
Assert.AreEqual(3, sheet.Cells["D2"].Value);
}
}

[TestMethod]
public void ShouldUseHeadersFromAttribute()
{
var item1 = new TestClass4
{
Name = "test 1",
Columns = new Dictionary<string, object> { { "A", 3 }, { "B", 2 } }
};
var items = new List<TestClass4> { item1 };
using (var package = new ExcelPackage())
{
var sheet = package.Workbook.Worksheets.Add("test");
sheet.Cells["A1"].LoadFromCollection(items, c =>
{
c.PrintHeaders = true;
});
Assert.AreEqual("Name", sheet.Cells["A1"].Value);
Assert.AreEqual("C", sheet.Cells["B1"].Value);
Assert.AreEqual("B", sheet.Cells["C1"].Value);
Assert.AreEqual("A", sheet.Cells["D1"].Value);
Assert.AreEqual("test 1", sheet.Cells["A2"].Value);
Assert.IsNull(sheet.Cells["B2"].Value);
Assert.AreEqual(2, sheet.Cells["C2"].Value);
Assert.AreEqual(3, sheet.Cells["D2"].Value);
}
}
}
}

0 comments on commit 4063b0c

Please sign in to comment.