Skip to content

Commit

Permalink
#969, #1058 - implemented EPPlusDictionaryColumnAttribute for LoadFro…
Browse files Browse the repository at this point in the history
…mCollection with attributes
  • Loading branch information
swmal committed Nov 8, 2023
1 parent 796c3c7 commit f11b939
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 121 deletions.
41 changes: 41 additions & 0 deletions src/EPPlus/Attributes/EPPlusDictionaryColumnAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*************************************************************************************************
Required Notice: Copyright (C) EPPlus Software AB.
This software is licensed under PolyForm Noncommercial License 1.0.0
and may only be used for noncommercial purposes
https://polyformproject.org/licenses/noncommercial/1.0.0/
A commercial license to use this software can be purchased at https://epplussoftware.com
*************************************************************************************************
Date Author Change
*************************************************************************************************
7/11/2023 EPPlus Software AB EPPlus 7
*************************************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OfficeOpenXml.Attributes
{
/// <summary>
/// This attributes can only be used on properties that are of the type IDictionary&lt;string, string&gt;.
/// Columns will be added based on the items in <see cref="EPPlusDictionaryColumnAttribute.ColumnHeaders"/>
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field)]
internal class EPPlusDictionaryColumnAttribute : Attribute
{
/// <summary>
/// Order of the columns value, default value is 0
/// </summary>
public int Order
{
get;
set;
}

/// <summary>
/// The values of this array will be used to generate columns (one column for each item).
/// </summary>
public string[] ColumnHeaders { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace OfficeOpenXml.FormulaParsing.Excel.Functions.Engineering
[FunctionMetadata(
Category = ExcelFunctionCategory.Engineering,
EPPlusVersion = "5.1",
Description = "Calculates the modified Bessel function Yn(x)")]
Description = "Converts a number from one measurement system to another")]
public class ConvertFunction : ExcelFunction
{
public override int ArgumentMinLength => 3;
Expand Down
4 changes: 4 additions & 0 deletions src/EPPlus/LoadFunctions/ColumnInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public ColumnInfo()

public int SortOrder { get; set; }

public bool IsDictionaryProperty { get; set; }

public string DictinaryKey { get; set; }

public List<int> SortOrderLevels { get; set; }
public int Index { get; set; }

Expand Down
28 changes: 27 additions & 1 deletion src/EPPlus/LoadFunctions/LoadFromCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,21 @@ private void SetValuesAndFormulas(object[,] values, Dictionary<int, FormulaCell>
{
if(!string.IsNullOrEmpty(colInfo.Path) && colInfo.Path.Contains("."))
{
values[row, col++] = GetValueByPath(item, colInfo.Path);
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);
}
continue;
}
var obj = item;
Expand All @@ -226,6 +240,18 @@ private void SetValuesAndFormulas(object[,] values, Dictionary<int, FormulaCell>
{
v = ((MethodInfo)member).Invoke(obj, null);
}
if (colInfo.IsDictionaryProperty)
{
var dict = v as Dictionary<string, object>;
if(dict != null && dict.ContainsKey(colInfo.DictinaryKey))
{
v = dict[colInfo.DictinaryKey];
}
else
{
v = null;
}
}

#if (!NET35)
if (v != null)
Expand Down
Loading

0 comments on commit f11b939

Please sign in to comment.