Ods: add mergeSubRowCells to AddTable()

This commit is contained in:
2023-11-16 10:06:55 +01:00
parent 9488c4c853
commit 077e03ab2f

View File

@ -179,7 +179,7 @@ namespace Elwig.Helpers.Export {
} }
} }
public async Task AddTable<T>(DataTable<T> table) { public async Task AddTable<T>(DataTable<T> table, bool mergeSubRowCells = true) {
if (Content == null) await AddHeader(); if (Content == null) await AddHeader();
if (Content == null) return; if (Content == null) return;
var totalSpan = table.ColumnSpans.Sum(); var totalSpan = table.ColumnSpans.Sum();
@ -217,13 +217,13 @@ namespace Elwig.Helpers.Export {
await Content.WriteAsync(" </table:table-row>\r\n"); await Content.WriteAsync(" </table:table-row>\r\n");
foreach (var row in table.GetData()) { foreach (var row in table.GetData()) {
await FormatRow(row, table.ColumnUnits); await FormatRow(row, table.ColumnUnits, mergeSubRowCells);
} }
await Content.WriteAsync(" </table:table>\r\n"); await Content.WriteAsync(" </table:table>\r\n");
} }
protected async Task FormatRow(IEnumerable<object?> row, IEnumerable<string?[]?> colUnits) { protected async Task FormatRow(IEnumerable<object?> row, IEnumerable<string?[]?> colUnits, bool mergeSubRowCells) {
if (Content == null) throw new InvalidOperationException(); if (Content == null) throw new InvalidOperationException();
var arrays = row.Where(c => c is Array).Cast<Array>().Select(c => c.Length).ToArray(); var arrays = row.Where(c => c is Array).Cast<Array>().Select(c => c.Length).ToArray();
int rowNum = Math.Max(1, arrays.Length > 0 ? arrays.Max() : 0); int rowNum = Math.Max(1, arrays.Length > 0 ? arrays.Max() : 0);
@ -232,6 +232,8 @@ namespace Elwig.Helpers.Export {
foreach (var (data, units) in row.Zip(colUnits)) { foreach (var (data, units) in row.Zip(colUnits)) {
if (data is Array a) { if (data is Array a) {
await Content.WriteAsync(i < a.Length ? FormatCell(a.GetValue(i), units: units) : $" <table:table-cell table:number-columns-repeated=\"{GetSubCols(a.GetType().GetElementType())}\"/>\r\n"); await Content.WriteAsync(i < a.Length ? FormatCell(a.GetValue(i), units: units) : $" <table:table-cell table:number-columns-repeated=\"{GetSubCols(a.GetType().GetElementType())}\"/>\r\n");
} else if (!mergeSubRowCells) {
await Content.WriteAsync(FormatCell(data, units: units));
} else { } else {
await Content.WriteAsync(FormatCell(data, rowSpan: i == 0 ? rowNum : 1, isCovered: i > 0, units: units)); await Content.WriteAsync(FormatCell(data, rowSpan: i == 0 ? rowNum : 1, isCovered: i > 0, units: units));
} }