Method FillTemplate
- Namespace
- DocWright.Spreadsheet
- Assembly
- DocWright.Spreadsheet.dll
FillTemplate(IReadOnlyDictionary<string, object?>, XlsxTemplateOptions?)
Fills the sheet as a template: each block of rows marked {{#Name}} is repeated
once for each item of the list Name, with the item's members filled in, and every
other placeholder is filled from data.
public XlsxTemplateResult FillTemplate(IReadOnlyDictionary<string, object?> data, XlsxTemplateOptions? options = null)
Parameters
dataIReadOnlyDictionary<string, object>The values and the lists, by name.
optionsXlsxTemplateOptionsHow placeholders and blocks are filled; null for the defaults.
Returns
- XlsxTemplateResult
What was filled.
Remarks
Markers. A row with {{#Orders}} in any of its cells is a block of
one row; with {{/Orders}} in a cell of a row below, the block is every row from
the one to the other. The markers are taken out of the cells; a cell that held only a
marker is left empty, with its format. A list the data has no value for leaves its block
as it stands and is reported in UnresolvedNames, as a
document's template leaves a table whose list it is not given.
Lists and members. A list is a DataTable, a
DataView, an XlsxTemplateList, or any other sequence of
dictionaries, DataRows or plain values. In a block, {{Product}} and
{{Orders.Product}} are a member of the item; a name the item does not have is
looked for in the outer item, then in data. A plain value answers
to its list's name. A placeholder that is the whole of a cell gives the cell its value's
type, as ReplacePlaceholders(IReadOnlyDictionary<string, object?>, XlsxPlaceholderOptions?) does.
Each repeat has the block's cells, their formats, the rows' heights and
the merges inside the block. A formula moves with its row as Excel fills it: a relative
reference into the block points into the same repeat, an absolute one does not move.
Rows below the block move down, and everything that pointed at them follows (see
InsertRows(int, int, XlsxInsertOptions?)). A range that runs from the block's first row or above down
to its last row grows over the repeats: SUM(D6:D6) under a one-row block filled
with four items becomes SUM(D6:D9), and so do a table, a filter, a conditional
format or a validation over the block. This is the template's rule, not an insert's:
Excel itself grows a range only for rows inserted strictly inside it.
Nested lists. A block inside a block repeats, for each outer item, once for each item of the outer item's member of that name: an order and its lines. A range in the outer block that covers the inner block's rows covers all of its repeats.
Nothing is calculated: the saved workbook asks for a calculation when it is next opened.
Exceptions
- ArgumentNullException
datais null.- ArgumentException
A block's value is not a list, a list holds objects whose members cannot be read, or the prefix or the suffix is empty.
- InvalidOperationException
The sheet is not a worksheet, or a list needs more rows than the sheet has below its block.
- UnsupportedFeatureException
Rows of a block cannot be inserted or deleted, as Excel refuses (an array formula across the block's edge, a table's header).
- ResourceLimitExceededException
The blocks would write more cells than MaxCells.
- InvalidDocumentException
The markers do not pair up, blocks nest more than one level, the data has no value for a name and the options say to throw (DXP1004), or the sheet's part is malformed.