Skip to content

Commit 2537ed8

Browse files
Added check to OpenXmlValueExtractor to exclude indexers from property mapping
1 parent c4f457a commit 2537ed8

4 files changed

Lines changed: 70 additions & 9 deletions

File tree

‎src/MiniExcel.OpenXml/Templates/OpenXmlValueExtractor.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ public class OpenXmlValueExtractor : IInputValueExtractor
2020
{
2121
var type = valueObject.GetType();
2222

23+
//todo: consider throwing an exception if a property has indexer instead of silently ignoring it
2324
var propertyValues = type
2425
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
26+
.Where(property => property.CanRead && property.GetIndexParameters().Length == 0)
2527
.Select(property => new { property.Name, Value = property.GetValue(valueObject) });
2628

2729
var fieldValues = type

‎src/MiniExcel.OpenXml/Utils/CalcChainHelper.cs‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22

33
internal static partial class CalcChainHelper
44
{
5-
65
// The calcChain.xml file in an Excel file (in the xl folder) is an XML file that stores the calculation chain for the workbook.
76
// The calculation chain specifies the order in which cells should be recalculated in order to update all formulas in the workbook correctly.
87
// It should include a series of <c> elements, each of which represents a cell in the workbook that contains a formula.
98
// Each <c> element should have a r attribute that specifies the cell's address (e.g., "A1" or "B2").
109
// The <c> element should also have a i attribute that specifies the index of the formula in the formulas collection (in the workbook's sheet data file).
1110
// https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.calculationchain?view=openxml-2.8.1
12-
public static string GetCalcChainContent( List<string> cellRefs, int sheetIndex ) {
13-
11+
public static string GetCalcChainContent(List<string> cellRefs, int sheetIndex )
12+
{
1413
var calcChainContent = new StringBuilder();
1514
foreach (var cr in cellRefs)
1615
{
17-
calcChainContent.Append($"""<c r="{cr}" i="{sheetIndex}"/>""" );
16+
calcChainContent.Append($"""<c r="{cr}" i="{sheetIndex}"/>""");
1817
}
1918

2019
return calcChainContent.ToString();
@@ -23,11 +22,15 @@ public static string GetCalcChainContent( List<string> cellRefs, int sheetIndex
2322
[CreateSyncVersion]
2423
public static async Task GenerateCalcChainSheetAsync(Stream calcChainStream, string calcChainContent, CancellationToken cancellationToken = default)
2524
{
25+
var content = $"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><calcChain xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\">{calcChainContent}</calcChain>";
26+
27+
#if NET8_0_OR_GREATER
28+
var writer = new StreamWriter(calcChainStream, Encoding.UTF8);
29+
await using var disposableWriter = writer.ConfigureAwait(false);
30+
await writer.WriteAsync(content.AsMemory(), cancellationToken).ConfigureAwait(false);
31+
#else
2632
using var writer = new StreamWriter(calcChainStream, Encoding.UTF8);
27-
await writer.WriteAsync($"""<?xml version="1.0" encoding="UTF-8" standalone="yes"?><calcChain xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">{calcChainContent}</calcChain>"""
28-
#if NET7_0_OR_GREATER
29-
.AsMemory(), cancellationToken
30-
#endif
31-
).ConfigureAwait(false);
33+
await writer.WriteAsync(content).ConfigureAwait(false);
34+
#endif
3235
}
3336
}

‎tests/MiniExcel.OpenXml.Tests/MiniExcelIssueAsyncTests.cs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,4 +1476,32 @@ private class Issue138ExcelRow
14761476
public double? 波段 { get; set; }
14771477
public double? 當沖 { get; set; }
14781478
}
1479+
1480+
[Fact]
1481+
public async Task TestIssue951()
1482+
{
1483+
var templatePath = PathHelper.GetFile("xlsx/TestTemplateEasyFill.xlsx");
1484+
using var path = AutoDeletingPath.Create();
1485+
1486+
var value = new Issue951
1487+
{
1488+
Name = "Jack",
1489+
CreateDate = new DateTime(2021, 01, 01),
1490+
VIP = true,
1491+
Points = 123
1492+
};
1493+
1494+
// must not throw
1495+
await _excelTemplater.FillTemplateAsync(path.ToString(), templatePath, value);
1496+
}
1497+
1498+
class Issue951
1499+
{
1500+
public string? Name { get; set; }
1501+
public DateTime CreateDate { get; set; }
1502+
public bool VIP { get; set; }
1503+
public double Points { get; set; }
1504+
1505+
public object this[string test] => new();
1506+
}
14791507
}

‎tests/MiniExcel.OpenXml.Tests/MiniExcelIssueTests.cs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3827,4 +3827,32 @@ public void TestIssue915()
38273827
Assert.Equal(7.4, result[1].Altitude);
38283828
Assert.Equal(8.6, result[2].Altitude);
38293829
}
3830+
3831+
[Fact]
3832+
public void TestIssue951()
3833+
{
3834+
var templatePath = PathHelper.GetFile("xlsx/TestTemplateEasyFill.xlsx");
3835+
using var path = AutoDeletingPath.Create();
3836+
3837+
var value = new Issue951
3838+
{
3839+
Name = "Jack",
3840+
CreateDate = new DateTime(2021, 01, 01),
3841+
VIP = true,
3842+
Points = 123
3843+
};
3844+
3845+
// must not throw
3846+
_excelTemplater.FillTemplate(path.ToString(), templatePath, value);
3847+
}
3848+
3849+
class Issue951
3850+
{
3851+
public string? Name { get; set; }
3852+
public DateTime CreateDate { get; set; }
3853+
public bool VIP { get; set; }
3854+
public double Points { get; set; }
3855+
3856+
public object this[string test] => new();
3857+
}
38303858
}

0 commit comments

Comments
 (0)