Skip to content

Commit 3995501

Browse files
authored
#2331, #2332 - fix for combining EpplusTableColumn and Display attributes in LoadFromCollection. (#2333)
1 parent c99bddc commit 3995501

17 files changed

+848
-6
lines changed

src/EPPlus/Attributes/EpplusTableColumnAttributeBase.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,35 @@ namespace OfficeOpenXml.Attributes
2424
public abstract class EpplusTableColumnAttributeBase : Attribute
2525
{
2626

27+
private int _order = int.MaxValue;
28+
private bool _orderIsSet = false;
29+
2730
/// <summary>
2831
/// Order of the columns value, default value is 0
2932
/// </summary>
3033
public int Order
3134
{
32-
get;
33-
set;
34-
} = int.MaxValue;
35+
get
36+
{
37+
return _order;
38+
}
39+
set
40+
{
41+
_order = value;
42+
_orderIsSet = true;
43+
}
44+
}
45+
46+
/// <summary>
47+
/// Returns true if the Order property has been explicitly set.
48+
/// </summary>
49+
internal bool OrderIsSet
50+
{
51+
get
52+
{
53+
return _orderIsSet;
54+
}
55+
}
3556

3657
/// <summary>
3758
/// Name shown in the header row, overriding the property name

src/EPPlus/LoadFunctions/LoadFromCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ private string GetHeaderFromDotNetAttributes(MemberInfo member)
389389
var displayAttribute = member.GetFirstAttributeOfType<DisplayAttribute>();
390390
if (displayAttribute != null)
391391
{
392-
return displayAttribute.Name;
392+
return displayAttribute.GetName();
393393
}
394394
#endif
395395
return default;

src/EPPlus/LoadFunctions/ReflectionHelpers/MemberPath.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public override string GetHeader()
114114
#if !NET35
115115
else if (last.Member.HasAttributeOfType(out DisplayAttribute displayAttr))
116116
{
117-
header = displayAttr.Name;
117+
header = displayAttr.GetName();
118118
}
119119
#endif
120120
if (string.IsNullOrEmpty(header))

src/EPPlus/LoadFunctions/ReflectionHelpers/SortOrderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static int GetSortOrder(this MemberInfo member, List<MemberInfo> filterMe
5252
{
5353
sortOrder = entcAttr.Order;
5454
}
55-
else if(member.HasAttributeOfType(out EpplusTableColumnAttribute etcAttr))
55+
else if (member.HasAttributeOfType(out EpplusTableColumnAttribute etcAttr) && etcAttr.OrderIsSet)
5656
{
5757
sortOrder = etcAttr.Order;
5858
}

src/EPPlusTest/EPPlus.Test.csproj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@
5050
<Compile Update="Filter\ValueFilterTest.cs" />
5151
<Compile Update="Filter\DynamicFilterTest.cs" />
5252
<Compile Update="Filter\AutoFilterReadWriteTest.cs" />
53+
<Compile Update="LoadFunctions\AttributesTestClasses\LoadFromCollResources.Designer.cs">
54+
<DesignTime>True</DesignTime>
55+
<AutoGen>True</AutoGen>
56+
<DependentUpon>LoadFromCollResources.resx</DependentUpon>
57+
</Compile>
5358
</ItemGroup>
5459
<ItemGroup>
5560
<None Update="App.config">
@@ -272,6 +277,12 @@
272277
<ItemGroup>
273278
<Folder Include="Style\XmlAccess\" />
274279
</ItemGroup>
280+
<ItemGroup>
281+
<EmbeddedResource Update="LoadFunctions\AttributesTestClasses\LoadFromCollResources.resx">
282+
<Generator>PublicResXFileCodeGenerator</Generator>
283+
<LastGenOutput>LoadFromCollResources.Designer.cs</LastGenOutput>
284+
</EmbeddedResource>
285+
</ItemGroup>
275286
<PropertyGroup>
276287
<SsdtUnitTestVersion>3.1</SsdtUnitTestVersion>
277288
</PropertyGroup>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*************************************************************************************************
2+
Required Notice: Copyright (C) EPPlus Software AB.
3+
This software is licensed under PolyForm Noncommercial License 1.0.0
4+
and may only be used for noncommercial purposes
5+
https://polyformproject.org/licenses/noncommercial/1.0.0/
6+
A commercial license to use this software can be purchased at https://epplussoftware.com
7+
*************************************************************************************************/
8+
#if !NET35
9+
using System.ComponentModel.DataAnnotations;
10+
11+
namespace EPPlusTest.LoadFunctions.AttributesTestClasses
12+
{
13+
/// <summary>
14+
/// Test class where only DisplayAttribute is present (no EpplusTableColumnAttribute).
15+
/// DisplayAttribute.Order should be used in this case.
16+
/// </summary>
17+
public class ClassWithDisplayOrderOnly
18+
{
19+
[Display(Name = "Id Column", Order = 3)]
20+
public int Id { get; set; }
21+
22+
[Display(Name = "Name Column", Order = 1)]
23+
public string Name { get; set; }
24+
25+
[Display(Name = "Description Column", Order = 2)]
26+
public string Description { get; set; }
27+
}
28+
}
29+
#endif
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*************************************************************************************************
2+
Required Notice: Copyright (C) EPPlus Software AB.
3+
This software is licensed under PolyForm Noncommercial License 1.0.0
4+
and may only be used for noncommercial purposes
5+
https://polyformproject.org/licenses/noncommercial/1.0.0/
6+
7+
A commercial license to use this software can be purchased at https://epplussoftware.com
8+
*************************************************************************************************/
9+
using OfficeOpenXml.Attributes;
10+
#if !NET35
11+
using System.ComponentModel.DataAnnotations;
12+
13+
namespace EPPlusTest.LoadFunctions.AttributesTestClasses
14+
{
15+
/// <summary>
16+
/// Test class using DisplayAttribute with ResourceType.
17+
/// When ResourceType is set, GetName() should be used instead of Name
18+
/// to get the localized value from the resource class.
19+
/// </summary>
20+
public class ClassWithDisplayResourceType
21+
{
22+
[EpplusTableColumn(Order = 1)]
23+
[Display(Name = "IdHeader", ResourceType = typeof(LoadFromCollResources))]
24+
public int Id { get; set; }
25+
26+
[EpplusTableColumn(Order = 2)]
27+
[Display(Name = "NameHeader", ResourceType = typeof(LoadFromCollResources))]
28+
public string Name { get; set; }
29+
30+
[EpplusTableColumn(Order = 3)]
31+
[Display(Name = "DescriptionHeader", ResourceType = typeof(LoadFromCollResources))]
32+
public string Description { get; set; }
33+
}
34+
}
35+
#endif
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*************************************************************************************************
2+
Required Notice: Copyright (C) EPPlus Software AB.
3+
This software is licensed under PolyForm Noncommercial License 1.0.0
4+
and may only be used for noncommercial purposes
5+
https://polyformproject.org/licenses/noncommercial/1.0.0/
6+
7+
A commercial license to use this software can be purchased at https://epplussoftware.com
8+
*************************************************************************************************/
9+
#if !NET35
10+
using System.ComponentModel.DataAnnotations;
11+
12+
namespace EPPlusTest.LoadFunctions.AttributesTestClasses
13+
{
14+
/// <summary>
15+
/// Test class using DisplayAttribute with ResourceType but without EpplusTableColumnAttribute.
16+
/// GetName() should return the localized value from the resource class.
17+
/// </summary>
18+
public class ClassWithDisplayResourceTypeOnly
19+
{
20+
[Display(Name = "IdHeader", ResourceType = typeof(LoadFromCollResources), Order = 1)]
21+
public int Id { get; set; }
22+
23+
[Display(Name = "NameHeader", ResourceType = typeof(LoadFromCollResources), Order = 2)]
24+
public string Name { get; set; }
25+
26+
[Display(Name = "DescriptionHeader", ResourceType = typeof(LoadFromCollResources), Order = 3)]
27+
public string Description { get; set; }
28+
}
29+
}
30+
#endif
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*************************************************************************************************
2+
Required Notice: Copyright (C) EPPlus Software AB.
3+
This software is licensed under PolyForm Noncommercial License 1.0.0
4+
and may only be used for noncommercial purposes
5+
https://polyformproject.org/licenses/noncommercial/1.0.0/
6+
7+
A commercial license to use this software can be purchased at https://epplussoftware.com
8+
*************************************************************************************************/
9+
using OfficeOpenXml.Attributes;
10+
#if !NET35
11+
using System.ComponentModel.DataAnnotations;
12+
13+
namespace EPPlusTest.LoadFunctions.AttributesTestClasses
14+
{
15+
/// <summary>
16+
/// Test class where EpplusTableColumnAttribute.Header IS set.
17+
/// It should take precedence over DisplayAttribute.Name.
18+
/// </summary>
19+
public class ClassWithEpplusHeaderAndDisplayName
20+
{
21+
[EpplusTableColumn(Order = 1, Header = "EPPlus Id")]
22+
[Display(Name = "Display Id")]
23+
public int Id { get; set; }
24+
25+
[EpplusTableColumn(Order = 2, Header = "EPPlus Name")]
26+
[Display(Name = "Display Name")]
27+
public string Name { get; set; }
28+
}
29+
}
30+
#endif
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using OfficeOpenXml.Attributes;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.ComponentModel.DataAnnotations;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace EPPlusTest.LoadFunctions.AttributesTestClasses
10+
{
11+
/// <summary>
12+
/// Test class where EpplusTableColumnAttribute exists but Order is NOT set.
13+
/// Should fall back to DisplayAttribute.Order.
14+
/// </summary>
15+
public class ClassWithEpplusNoOrderAndDisplayWithOrder
16+
{
17+
[EpplusTableColumn(NumberFormat = "0")]
18+
[Display(Name = "Id Column", Order = 3)]
19+
public int Id { get; set; }
20+
21+
[EpplusTableColumn]
22+
[Display(Name = "Name Column", Order = 1)]
23+
public string Name { get; set; }
24+
25+
[EpplusTableColumn]
26+
[Display(Name = "Description Column", Order = 2)]
27+
public string Description { get; set; }
28+
}
29+
}

0 commit comments

Comments
 (0)