Open
Description
API proposal
namespace SpreadCheetah.Tables;
public sealed class Table
{
public Table(TableStyle style, string? name = null);
public bool BandedColumns { get; set; }
public bool BandedRows { get; set; } = true;
// If not set, then the call to AddHeaderRowAsync determines the number of columns.
public int? NumberOfColumns { get; set; }
public TableColumnOptions Column(int columnNumber);
}
public sealed class TableColumnOptions
{
public string? TotalRowLabel { get; set; }
public TableTotalRowFunction? TotalRowFunction { get; set; }
}
public enum TableStyle
{
None,
Light1,
// Light2 .. Light21
Medium1,
// Medium2 .. Medium28
Dark1,
// Dark2 .. Dark11
}
public enum TableTotalRowFunction
{
Average = 1,
Count,
CountNumbers,
Max,
Min,
Sum,
StdDev,
Var
}
public sealed class Spreadsheet
{
// Starts the table from the next row. Doesn't write anything to the file, so it doesn't need to be async.
public void StartTable(Table table, string firstColumnName = "A");
public ValueTask AddHeaderRowAsync(IList<string> headerNames, StyleId? styleId = null, CancellationToken token = default);
// Finishes the table at the current row.
// May also add a total row, depending on what options have been set on the Table instance.
// The total row will be written to the file asynchronously.
public ValueTask FinishTableAsync(CancellationToken token = default);
}
API usage
await using var spreadsheet = await Spreadsheet.CreateNewAsync(stream);
await spreadsheet.StartWorksheetAsync("Sheet");
var table = new Table(TableStyle.Light9);
table.Column(1).TotalRowLabel = "Total";
table.Column(4).TotalRowFunction = TableTotalRowFunction.Sum;
spreadsheet.StartTable(table);
await spreadsheet.AddHeaderRowAsync(headerNames);
await spreadsheet.AddRowAsync(row1);
await spreadsheet.AddRowAsync(row2);
await spreadsheet.FinishAsync();