Ods: Fix small issues

This commit is contained in:
2023-11-13 22:40:12 +01:00
parent 8509f04d4d
commit d485f0fda1
2 changed files with 20 additions and 14 deletions

View File

@ -161,13 +161,12 @@ namespace Elwig.Helpers.Export {
public async Task AddTable<T>(DataTable<T> table) { public async Task AddTable<T>(DataTable<T> table) {
if (Content == null) await AddHeader(); if (Content == null) await AddHeader();
if (Content == null) return; if (Content == null) return;
var totalSpan = table.ColumnSpans.Sum(s => s.Item2); var totalSpan = table.ColumnSpans.Sum();
_tables.Add(table.FullName); _tables.Add(table.FullName);
await Content.WriteAsync($" <table:table table:name=\"{table.FullName}\" table:default-cell-style-name=\"default\">\r\n");
await Content.WriteAsync( await Content.WriteAsync(
$" <table:table table:name=\"{table.FullName}\">\r\n" +
$" <table:table-column table:default-cell-style-name=\"default\"/>\r\n" +
$" <table:table-row>\r\n" + $" <table:table-row>\r\n" +
FormatCell(table.FullName, colSpan: totalSpan, style: "header") + FormatCell(table.FullName, colSpan: totalSpan, style: "header") +
$" </table:table-row>\r\n" + $" </table:table-row>\r\n" +
@ -175,7 +174,7 @@ namespace Elwig.Helpers.Export {
$" <table:table-cell table:number-columns-repeated=\"{totalSpan}\"/>\r\n" + $" <table:table-cell table:number-columns-repeated=\"{totalSpan}\"/>\r\n" +
$" </table:table-row>\r\n" + $" </table:table-row>\r\n" +
$" <table:table-row>\r\n"); $" <table:table-row>\r\n");
foreach (var (name, span) in table.ColumnSpans) { foreach (var (name, span) in table.ColumnNames.Zip(table.ColumnSpans)) {
await Content.WriteAsync(FormatCell(name, colSpan: span, style: "th")); await Content.WriteAsync(FormatCell(name, colSpan: span, style: "th"));
} }
await Content.WriteAsync(" </table:table-row>\r\n"); await Content.WriteAsync(" </table:table-row>\r\n");
@ -219,7 +218,7 @@ namespace Elwig.Helpers.Export {
c = $"<table:table-cell office:value-type=\"string\"{add}><text:p>{data}</text:p></table:table-cell>"; c = $"<table:table-cell office:value-type=\"string\"{add}><text:p>{data}</text:p></table:table-cell>";
} }
return c = $" {c}\r\n" + (colSpan > 1 ? $" <table:covered-table-cell table:number-rows-repeated=\"{colSpan - 1}\"/>\r\n" : ""); return $" {c}\r\n" + (colSpan > 1 ? $" <table:covered-table-cell table:number-rows-repeated=\"{colSpan - 1}\"/>\r\n" : "");
} }
} }
} }

View File

@ -10,18 +10,14 @@ namespace Elwig.Models.Dtos {
public string FullName { get; set; } public string FullName { get; set; }
public IEnumerable<T> Rows { get; private set; } public IEnumerable<T> Rows { get; private set; }
public int RowNum => Rows.Count(); public int RowNum => Rows.Count();
public int ColNum => ColumnNames.Count();
public IEnumerable<(string, Type?)> ColumnDefs => _map.Select(m => (m.Item1, m.Item2?.PropertyType ?? m.Item3?.FieldType)); public IEnumerable<(string, Type?)> ColumnDefs => _map.Select(m => (m.Item1, m.Item2?.PropertyType ?? m.Item3?.FieldType));
public IEnumerable<string> ColumnNames => ColumnDefs.Select(m => m.Item1); public IEnumerable<string> ColumnNames => ColumnDefs.Select(m => m.Item1);
public IEnumerable<Type?> ColumnTypes => ColumnDefs.Select(m => m.Item2); public IEnumerable<Type?> ColumnTypes => ColumnDefs.Select(m => m.Item2);
public IEnumerable<(string, int)> ColumnSpans => ColumnDefs.Select(c => { public IEnumerable<Type?> ColumnFlatTypes { get; private set; }
var type = c.Item2; public IEnumerable<int> ColumnSpans { get; private set; }
var elType = type?.GetElementType();
return (c.Item1,
type != null && type.IsValueType && type.Name.StartsWith("ValueTuple") ? type.GetFields().Length :
type != null && elType != null && type.IsArray && elType.IsValueType && elType.Name.StartsWith("ValueTuple") ? elType.GetFields().Length : 1
);
}).ToList();
public int ColNum => ColumnNames.Count();
private readonly PropertyInfo[] _properties; private readonly PropertyInfo[] _properties;
private readonly FieldInfo[] _fields; private readonly FieldInfo[] _fields;
private readonly (string, PropertyInfo?, FieldInfo?)[] _map; private readonly (string, PropertyInfo?, FieldInfo?)[] _map;
@ -34,6 +30,17 @@ namespace Elwig.Models.Dtos {
Name = name; Name = name;
FullName = fullName; FullName = fullName;
Rows = rows; Rows = rows;
ColumnFlatTypes = ColumnTypes.SelectMany(type => {
var elType = type?.GetElementType();
return type != null && type.IsValueType && type.Name.StartsWith("ValueTuple") ? type.GetFields().Select(f => f.FieldType) :
type != null && elType != null && type.IsArray && elType.IsValueType && elType.Name.StartsWith("ValueTuple") ? elType.GetFields().Select(f => f.FieldType) :
new Type?[] { type };
}).ToList();
ColumnSpans = ColumnTypes.Select(type => {
var elType = type?.GetElementType();
return type != null && type.IsValueType && type.Name.StartsWith("ValueTuple") ? type.GetFields().Length :
type != null && elType != null && type.IsArray && elType.IsValueType && elType.Name.StartsWith("ValueTuple") ? elType.GetFields().Length : 1;
}).ToList();
} }
public DataTable(string name, IEnumerable<T> rows, IEnumerable<(string, string)>? colNames = null) : public DataTable(string name, IEnumerable<T> rows, IEnumerable<(string, string)>? colNames = null) :