Skip to content

Commit

Permalink
2.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
mustaddon committed Nov 7, 2021
1 parent fa7acd4 commit b2fe3bc
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 30 deletions.
6 changes: 3 additions & 3 deletions ArrayToExcel/ArrayToExcel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
<Nullable>enable</Nullable>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\ArrayToExcel.snk</AssemblyOriginatorKeyFile>
<AssemblyVersion>2.0.0</AssemblyVersion>
<FileVersion>2.0.0</FileVersion>
<Version>2.0.0</Version>
<AssemblyVersion>2.0.1</AssemblyVersion>
<FileVersion>2.0.1</FileVersion>
<Version>2.0.1</Version>
<Company></Company>
<Authors>Leonid Salavatov</Authors>
<Copyright>Leonid Salavatov 2021</Copyright>
Expand Down
19 changes: 19 additions & 0 deletions ArrayToExcel/ColumnInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Reflection;

namespace ArrayToExcel
{
public sealed class ColumnInfo
{
internal ColumnInfo(int index, ColumnSchema schema)
{
Index = index;
Schema = schema;
}

internal ColumnSchema Schema { get; }

public int Index { get; }
public string Name { get => this.Schema.Name; }
public MemberInfo? Member { get => this.Schema.Member; }
}
}
53 changes: 26 additions & 27 deletions ArrayToExcel/SchemaBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Reflection;

Expand Down Expand Up @@ -35,37 +34,37 @@ public SchemaBuilder<T> SheetName(string name)
return this;
}

public SchemaBuilder<T> ColumnName(Func<MemberInfo, string> name)
public SchemaBuilder<T> ColumnName(Func<ColumnInfo, string> name)
{
_rootSheetBuilder.ColumnName(name);
return this;
}

public SchemaBuilder<T> ColumnWidth(Func<MemberInfo, uint> width)
public SchemaBuilder<T> ColumnWidth(Func<ColumnInfo, uint> width)
{
_rootSheetBuilder.ColumnWidth(width);
return this;
}

public SchemaBuilder<T> ColumnFilter(Func<MemberInfo, bool> filter)
public SchemaBuilder<T> ColumnFilter(Func<ColumnInfo, bool> filter)
{
_rootSheetBuilder.ColumnFilter(filter);
return this;
}

public SchemaBuilder<T> ColumnSort<TKey>(Func<MemberInfo, TKey> sort, bool desc = false)
public SchemaBuilder<T> ColumnSort<TKey>(Func<ColumnInfo, TKey> sort, bool desc = false)
{
_rootSheetBuilder.ColumnSort(sort, desc);
return this;
}

public SchemaBuilder<T> ColumnValue(Func<MemberInfo, T, object?> value)
public SchemaBuilder<T> ColumnValue(Func<ColumnInfo, T, object?> value)
{
_rootSheetBuilder.ColumnValue(value);
return this;
}

public SchemaBuilder<T> AddColumn(string name, Func<T, object?> value, uint width = 20)
public SchemaBuilder<T> AddColumn(string name, Func<T, object?> value, uint width = ColumnSchema.DefaultWidth)
{
_rootSheetBuilder.AddColumn(name, value, width);
return this;
Expand All @@ -89,46 +88,46 @@ public SheetSchemaBuilder<T> SheetName(string name)
return this;
}

public SheetSchemaBuilder<T> ColumnName(Func<MemberInfo, string> name)
public SheetSchemaBuilder<T> ColumnName(Func<ColumnInfo, string> name)
{
foreach (var col in Schema.Columns)
if (col.Member != null)
col.Name = name(col.Member);
foreach (var col in Schema.Columns.Select((x, i) => new ColumnInfo(i, x)))
col.Schema.Name = name(col);
return this;
}

public SheetSchemaBuilder<T> ColumnWidth(Func<MemberInfo, uint> width)
public SheetSchemaBuilder<T> ColumnWidth(Func<ColumnInfo, uint> width)
{
foreach (var col in Schema.Columns)
if (col.Member != null)
col.Width = width(col.Member);
foreach (var col in Schema.Columns.Select((x, i) => new ColumnInfo(i, x)))
col.Schema.Width = width(col);
return this;
}

public SheetSchemaBuilder<T> ColumnFilter(Func<MemberInfo, bool> filter)
public SheetSchemaBuilder<T> ColumnFilter(Func<ColumnInfo, bool> filter)
{
Schema.Columns = Schema.Columns.Where(x => x.Member == null || filter(x.Member)).ToList();
Schema.Columns = Schema.Columns
.Select((x, i) => new ColumnInfo(i, x))
.Where(x => filter(x))
.Select(x => x.Schema)
.ToList();
return this;
}

public SheetSchemaBuilder<T> ColumnSort<TKey>(Func<MemberInfo, TKey> sort, bool desc = false)
public SheetSchemaBuilder<T> ColumnSort<TKey>(Func<ColumnInfo, TKey> sort, bool desc = false)
{
if (!_defaultCols)
return this;
var colInfos = Schema.Columns.Select((x, i) => new ColumnInfo(i, x)).ToList();

Schema.Columns = (desc
? Schema.Columns.OrderByDescending(x => x.Member != null ? sort(x.Member) : default)
: Schema.Columns.OrderBy(x => x.Member != null ? sort(x.Member) : default)
).ToList();
? colInfos.OrderByDescending(sort)
: colInfos.OrderBy(sort)
).Select(x => x.Schema).ToList();

return this;
}

public SheetSchemaBuilder<T> ColumnValue(Func<MemberInfo, T, object?> value)
public SheetSchemaBuilder<T> ColumnValue(Func<ColumnInfo, T, object?> value)
{
foreach (var col in Schema.Columns)
if (col.Member != null)
col.Value = x => value(col.Member, (T)x);
foreach (var col in Schema.Columns.Select((x, i) => new ColumnInfo(i, x)))
col.Schema.Value = x => value(col, (T)x);
return this;
}

Expand Down
Binary file modified Examples/example1.xlsx
Binary file not shown.
Binary file modified Examples/example2.xlsx
Binary file not shown.
Binary file modified Examples/example3.xlsx
Binary file not shown.
Binary file modified Examples/example4.xlsx
Binary file not shown.
Binary file modified Examples/example5.xlsx
Binary file not shown.
Binary file modified Examples/example6.xlsx
Binary file not shown.
Binary file modified Examples/example7.xlsx
Binary file not shown.

0 comments on commit b2fe3bc

Please sign in to comment.