Typed-xlsx
Reference

API Reference

Full reference for createExcelSchema, createWorkbook, createWorkbookStream, and summaries.

createExcelSchema<T>()

Returns a SchemaBuilder<T>. Schemas carry no mutable state after .build() and can be reused across any number of tables.

const 
schema
=
createExcelSchema
<
Row
>().
column
("name", {
accessor
: "name" }).
build
();

SchemaBuilder.column(id, definition)

FieldTypeDescription
idstringStable column identifier. Must be unique within the schema.
accessorPath<T> or (row: T) => valueExtracts the cell value from the row.
headerstringColumn header text. Defaults to id.
defaultValueCellValueFallback when accessor returns null or undefined.
transform(value, row, rowIndex) => CellValuePost-processes the extracted value.
formatstring or (row, rowIndex, subRowIndex) => stringExcel number format string applied to the cell.
styleCellStyle or (row, rowIndex, subRowIndex) => CellStyleStyle for data cells.
headerStyleCellStyleStyle for the header cell.
widthnumberColumn width in character units.
autoWidthbooleanCompute width from content. Overridden by explicit width.
minWidthnumberMinimum width when autoWidth is active.
maxWidthnumberMaximum width when autoWidth is active.
summarySummaryInput<T>Reducer-based summary cells, defined directly or via the callback builder.

SchemaBuilder.group(id, buildFn)

Generates columns dynamically from an optional context value passed at table-build time. If the callback declares a second parameter, that parameter type becomes the required context shape for the group id.

ArgumentTypeDescription
idstringGroup identifier. Used as the key in the context object passed to .table().
buildFn(builder: SchemaBuilder<T>, context?: TContext) => voidCalled with the current builder and, optionally, the resolved context value.

Selection stays strongly typed at the group level. Use the group id in select.include or select.exclude to include or exclude every generated column from that group:

import { 
createExcelSchema
,
createWorkbook
} from "@chronicstone/typed-xlsx";
type
Row
= {
name
: string;
orgs
: number[] };
const
schema
=
createExcelSchema
<
Row
>()
.
column
("name", {
accessor
: "name" })
.
group
("memberships", (
builder
,
orgIds
: number[]) => {
for (const
id
of
orgIds
) {
builder
.
column
(`org-${
id
}`, {
accessor
: (
row
) =>
row
.
orgs
.
includes
(
id
),
}); } }) .
build
();
createWorkbook
()
.
sheet
("Users")
.
table
({
rows
: [],
schema
,
select
: {
exclude
: ["memberships"] },
});

When the excluded group is the only contextful group in the schema, context is optional and can be omitted entirely.

SchemaBuilder.build()

Returns a frozen SchemaDefinition<T> object. Safe to share across multiple workbooks.


createWorkbook(options?)

Returns a Workbook builder for synchronous, in-memory XLSX generation.

Workbook.sheet(name, options?)

OptionTypeDescription
tablesPerRownumberNumber of tables placed side-by-side before wrapping. Default: 1.
tableColumnGapnumberBlank columns between tables in the same row. Default: 1.
tableRowGapnumberBlank rows between table rows. Default: 1.
freezePane{ rows?: number; columns?: number }Freeze the first N rows and/or columns.
rightToLeftbooleanEnable right-to-left sheet direction.

Returns a WorkbookSheet with a .table(input) method.

WorkbookSheet.table(input)

FieldTypeDescription
schemaSchemaDefinition<T>Built schema from createExcelSchema<T>().build().
rowsT[]Data rows.
idstringOptional table identifier.
titlestringOptional title row rendered above the header.
select{ include?: readonly (SchemaColumnId<TSchema> | SchemaGroupId<TSchema>)[]; exclude?: readonly (SchemaColumnId<TSchema> | SchemaGroupId<TSchema>)[] }Column or group selection by id.
contextSchemaGroupContext<TSchema>Required only when selected groups declare a context parameter. Context values are keyed by group id.

Returns the WorkbookSheet for chaining.

Output methods

MethodReturn typeDescription
toUint8Array()Uint8ArrayXLSX bytes.
toBuffer()BufferNode.js Buffer wrapping the XLSX bytes.
writeToFile(path)Promise<void>Write the XLSX file to disk.

createWorkbookStream(options?)

Returns a WorkbookStream builder for incremental, commit-based XLSX generation.

OptionTypeDefaultDescription
tempStorage"file" or "memory""file"Where serialized sheet fragments are spooled.
tempDirectorystringOS temp dirDirectory for file-backed spool.
strings"auto" · "inline" · "shared""auto"String storage strategy. Overrides memoryProfile.
memoryProfile"balanced" · "low-memory" · "compact-file""balanced"Convenience preset for string strategy.

WorkbookStream.sheet(name, options?)

Accepts a subset of sheet options: freezePane and rightToLeft. Layout options (tablesPerRow, gaps) are also supported.

Returns a WorkbookSheetStream.

WorkbookSheetStream.table(options)

Async. Registers a table and returns a WorkbookTableStream<T>.

FieldTypeDescription
idstringRequired table identifier.
schemaSchemaDefinition<T>Built schema.
select{ include?: readonly (SchemaColumnId<TSchema> | SchemaGroupId<TSchema>)[]; exclude?: readonly (SchemaColumnId<TSchema> | SchemaGroupId<TSchema>)[] }Column or group selection.
contextSchemaGroupContext<TSchema>Required only when selected groups declare a context parameter.

WorkbookTableStream.commit(batch)

Async. Serializes the batch and appends it to the sheet spool.

FieldTypeDescription
rowsT[]Batch of rows to serialize.

Output methods

MethodDescription
writeToFile(path)Write directly to a file path.
pipeToNode(stream)Pipe to any NodeJS.WritableStream.
pipeTo(stream)Pipe to a Web Streams WritableStream<Uint8Array>.
toNodeReadable()Returns a NodeJS.ReadableStream.
toReadableStream()Returns a Web ReadableStream<Uint8Array>.

Only one output method may be called per workbook instance.


SummaryInput<T>

The summary field accepts either:

  • a single SummaryDefinition<T, TAcc>
  • an array of SummaryDefinition<T, TAcc>
  • a callback of shape (summary) => SummaryDefinition[]

The callback form is the recommended API because it gives you access to helper methods like summary.cell(...), summary.label(...), and summary.empty().

SummaryDefinition<T, TAcc>

FieldTypeDescription
labelstringOptional label cell rendered in non-summary columns on the same summary row.
init() => TAccReturns the initial accumulator value. Called once.
step(acc: TAcc, row: T, rowIndex: number) => TAccUpdates the accumulator for each row.
finalize(acc: TAcc) => SummaryCellValueConverts the final accumulator to a cell value.
formatstring or (value) => stringNumber format applied to the summary cell.
styleCellStyle or (value) => CellStyleStyle applied to the summary cell.
Copyright © 2026 Cyprien Thao. Released under the MIT License.