Skip to content

Commit 2f0bc52

Browse files
Adds mapping fields capability (#931)
* Refactoring of the mapping utilities, Attributes and Exceptions - Added `MiniExcelAttributeBase` abstract class and made the other attributes inherit it - Added feature for public fields of a class to be mapped alongside its properties if decorated with one of the attributes inheriting from afermonetioned base attribute - Renamed all mapping utilities to reflect their mapping functionality and the custom accessors to reflect the shift from only properties to members - Moved the `GetEmptyExpandoObject` methods to dedicated new class `ExpandoHelper` * Refactoring of Importers Refactored `OpenXmlImporter` and `CsvImporter` removing redundant logic and moving duplicate method in shared extension helper * Added some tests for the field mapping feature and updated readme * minor bugfixes
1 parent f52c8db commit 2f0bc52

46 files changed

Lines changed: 859 additions & 630 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README-V2.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,30 @@ using var stream = File.OpenRead(path);
259259
var rows = importer.Query<UserAccount>(stream);
260260
```
261261

262+
Only public properties get mapped by default, but public fields can also be mapped if decorated with `MiniExcelColumnAttribute` or any of the other MiniExcel attributes:
263+
```csharp
264+
public class UserAccount
265+
{
266+
[MiniExcelColumn]
267+
public Guid ID;
268+
269+
public string Name { get; set; }
270+
271+
[MiniExcelFormat("dd/MM/yyyy")]
272+
public DateTime BoD;
273+
274+
public int Age { get; set; }
275+
276+
[MiniExcelColumnIndex(2)]
277+
public bool VIP;
278+
279+
public decimal Points { get; set; }
280+
}
281+
282+
var importer = MiniExcel.Importers.GetOpenXmlImporter();
283+
var rows = importer.Query<UserAccount>(path);
284+
```
285+
262286
#### 2. Execute a query and map it to a list of dynamic objects
263287

264288
By default no header will be used and the dynamic keys will be `.A`, `.B`, `.C`, etc..:

src/MiniExcel.Core/Abstractions/IMiniExcelWriteAdapter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
public interface IMiniExcelWriteAdapter
44
{
55
bool TryGetKnownCount(out int count);
6-
List<MiniExcelColumnInfo>? GetColumns();
7-
IEnumerable<IEnumerable<CellWriteInfo>> GetRows(List<MiniExcelColumnInfo> props, CancellationToken cancellationToken = default);
6+
List<MiniExcelColumnMapping>? GetColumns();
7+
IEnumerable<IEnumerable<CellWriteInfo>> GetRows(List<MiniExcelColumnMapping> props, CancellationToken cancellationToken = default);
88
}
99

10-
public readonly struct CellWriteInfo(object? value, int cellIndex, MiniExcelColumnInfo prop)
10+
public readonly struct CellWriteInfo(object? value, int cellIndex, MiniExcelColumnMapping prop)
1111
{
1212
public object? Value { get; } = value;
1313
public int CellIndex { get; } = cellIndex;
14-
public MiniExcelColumnInfo Prop { get; } = prop;
14+
public MiniExcelColumnMapping Prop { get; } = prop;
1515
}

src/MiniExcel.Core/Abstractions/IMiniExcelWriteAdapterAsync.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
public interface IMiniExcelWriteAdapterAsync
44
{
5-
Task<List<MiniExcelColumnInfo>?> GetColumnsAsync();
6-
IAsyncEnumerable<CellWriteInfo[]> GetRowsAsync(List<MiniExcelColumnInfo> props, CancellationToken cancellationToken);
5+
Task<List<MiniExcelColumnMapping>?> GetColumnsAsync();
6+
IAsyncEnumerable<CellWriteInfo[]> GetRowsAsync(List<MiniExcelColumnMapping> props, CancellationToken cancellationToken);
77
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
namespace MiniExcelLib.Core.Attributes;
2+
3+
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
4+
public abstract class MiniExcelAttributeBase : Attribute;

src/MiniExcel.Core/Attributes/MiniExcelColumnAttribute.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
namespace MiniExcelLib.Core.Attributes;
22

3-
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
4-
public class MiniExcelColumnAttribute : Attribute
3+
public class MiniExcelColumnAttribute : MiniExcelAttributeBase
54
{
65
private int _index = -1;
76
private string? _xName;
87

98
public string? Name { get; set; }
10-
public string[]? Aliases { get; set; } = [];
11-
public string? Format { get; set; }
12-
public bool Ignore { get; set; }
13-
9+
public string[]? Aliases { get; set; } = [];
10+
public string? Format { get; set; }
11+
public bool Ignore { get; set; }
12+
1413
internal int FormatId { get; private set; } = -1;
1514
public double Width { get; set; } = 8.42857143;
1615
public ColumnType Type { get; set; } = ColumnType.Value;
@@ -35,19 +34,19 @@ private void Init(int index, string? columnName = null)
3534
_index = index;
3635
_xName ??= columnName ?? CellReferenceConverter.GetAlphabeticalIndex(index);
3736
}
38-
39-
public void SetFormatId(int formatId) => FormatId = formatId;
40-
}
4137

42-
public enum ColumnType { Value, Formula }
38+
public void SetFormatId(int formatId) => FormatId = formatId;
39+
}
4340

4441
public class DynamicExcelColumn : MiniExcelColumnAttribute
4542
{
4643
public string Key { get; set; }
47-
public Func<object, object>? CustomFormatter { get; set; }
44+
public Func<object?, object?>? CustomFormatter { get; set; }
4845

4946
public DynamicExcelColumn(string key)
5047
{
5148
Key = key;
5249
}
5350
}
51+
52+
public enum ColumnType { Value, Formula }

src/MiniExcel.Core/Attributes/MiniExcelColumnIndexAttribute.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace MiniExcelLib.Core.Attributes;
22

3-
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
4-
public class MiniExcelColumnIndexAttribute : Attribute
3+
public class MiniExcelColumnIndexAttribute : MiniExcelAttributeBase
54
{
65
public int ExcelColumnIndex { get; set; }
76
internal string? ExcelXName { get; set; }

src/MiniExcel.Core/Attributes/MiniExcelColumnNameAttribute.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace MiniExcelLib.Core.Attributes;
22

3-
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
4-
public class MiniExcelColumnNameAttribute(string columnName, string[]? aliases = null) : Attribute
3+
public class MiniExcelColumnNameAttribute(string columnName, string[]? aliases = null) : MiniExcelAttributeBase
54
{
65
public string ExcelColumnName { get; set; } = columnName;
76
public string[] Aliases { get; set; } = aliases ?? [];
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace MiniExcelLib.Core.Attributes;
22

3-
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
4-
public class MiniExcelColumnWidthAttribute(double columnWidth) : Attribute
3+
public class MiniExcelColumnWidthAttribute(double columnWidth) : MiniExcelAttributeBase
54
{
65
public double ExcelColumnWidth { get; set; } = columnWidth;
76
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace MiniExcelLib.Core.Attributes;
22

3-
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
4-
public class MiniExcelFormatAttribute(string format) : Attribute
3+
public class MiniExcelFormatAttribute(string format) : MiniExcelAttributeBase
54
{
65
public string Format { get; set; } = format;
76
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace MiniExcelLib.Core.Attributes;
22

3-
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
4-
public class MiniExcelIgnoreAttribute(bool ignore = true) : Attribute
3+
public class MiniExcelIgnoreAttribute(bool ignore = true) : MiniExcelAttributeBase
54
{
65
public bool Ignore { get; set; } = ignore;
76
}

0 commit comments

Comments
 (0)