Skip to content

Commit

Permalink
Bugfix/dynamic columns not working (#581)
Browse files Browse the repository at this point in the history
* Fix DynamicExcelColumn configuration while saving IDictionary values

* adapt using

---------

Co-authored-by: Johannes Barta <johannes.barta@buhlergroup.com>
  • Loading branch information
johannes-barta and Johannes Barta authored Apr 12, 2024
1 parent 9e3f1bd commit 623ecc8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/MiniExcel/Utils/CustomPropertyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ internal static List<ExcelColumnInfo> SortCustomProps(List<ExcelColumnInfo> prop
if (withCustomIndexProps.Any())
maxColumnIndex = Math.Max((int)withCustomIndexProps.Max(w => w.ExcelColumnIndex), maxColumnIndex);

var withoutCustomIndexProps = props.Where(w => w.ExcelColumnIndex == null).ToList();
var withoutCustomIndexProps = props.Where(w => w.ExcelColumnIndex == null || w.ExcelColumnIndex == -1).ToList();

List<ExcelColumnInfo> newProps = new List<ExcelColumnInfo>();
var index = 0;
Expand Down
38 changes: 38 additions & 0 deletions tests/MiniExcelTests/MiniExcelOpenXmlAsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Dapper;
using ExcelDataReader;
using MiniExcelLibs.Attributes;
using MiniExcelLibs.OpenXml;
using MiniExcelLibs.Tests.Utils;
using OfficeOpenXml;
using System;
Expand Down Expand Up @@ -856,6 +857,43 @@ public async Task SaveAsByIEnumerableIDictionary()
}
}

[Fact]
public async Task SaveAsByIEnumerableIDictionaryWithDynamicConfiguration()
{
var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.xlsx");
var dynamicColumns = new[]
{
new DynamicExcelColumn("Column1") { Name = "Name Column" },
new DynamicExcelColumn("Column2") { Name = "Value Column" }
};
var config = new OpenXmlConfiguration
{
DynamicColumns = dynamicColumns
};
var values = new List<Dictionary<string, object>>()
{
new Dictionary<string, object>() { { "Column1", "MiniExcel" }, { "Column2", 1 } },
new Dictionary<string, object>() { { "Column1", "Github" }, { "Column2", 2 } },
};
await MiniExcel.SaveAsAsync(path, values, configuration: config);

using (var stream = File.OpenRead(path))
{
var d = (await stream.QueryAsync(useHeaderRow: true)).Cast<IDictionary<string, object>>();
var rows = d.ToList();
Assert.Equal(2, rows.Count);
Assert.Equal("Name Column", rows[0].Keys.ElementAt(0));
Assert.Equal("Value Column", rows[0].Keys.ElementAt(1));
Assert.Equal("MiniExcel", rows[0].Values.ElementAt(0));
Assert.Equal(1d, rows[0].Values.ElementAt(1));
Assert.Equal("Github", rows[1].Values.ElementAt(0));
Assert.Equal(2d, rows[1].Values.ElementAt(1));
}

Assert.Equal("A1:B3", Helpers.GetFirstSheetDimensionRefValue(path));
File.Delete(path);
}

[Fact()]
public async Task SaveAsByDapperRows()
{
Expand Down

0 comments on commit 623ecc8

Please sign in to comment.