typed-xlsx
Columns

Column Sets

Define a block of columns once, such as audit timestamps or money columns, and add it to any schema whose rows have the fields it reads.

Apps repeat the same columns across exports: created and updated timestamps, amounts with a currency, customer details. defineColumns() defines them once, and .columns(set) adds them to a schema.

import { 
createExcelSchema
,
defineColumns
} from "typed-xlsx";
const
audit
=
defineColumns
<{
createdAt
:
Date
;
updatedBy
: string }>()
.
column
("createdAt", {
header
: "Created",
accessor
: "createdAt",
format
: "yyyy-mm-dd" })
.
column
("updatedBy", {
header
: "Updated by",
accessor
: "updatedBy" });
type
Order
= {
id
: string;
total
: number;
createdAt
:
Date
;
updatedBy
: string };
type
Invoice
= {
number
: string;
createdAt
:
Date
;
updatedBy
: string;
paid
: boolean };
const
orders
=
createExcelSchema
<
Order
>()
.
column
("id", {
header
: "Order",
accessor
: "id" })
.
column
("total", {
header
: "Total",
accessor
: "total" })
.
columns
(
audit
)
.
build
();
const
invoices
=
createExcelSchema
<
Invoice
>()
.
column
("number", {
header
: "Invoice",
accessor
: "number" })
.
columns
(
audit
)
.
column
("paid", {
type
: "checkbox",
header
: "Paid",
accessor
: "paid" })
.
build
();

A set is written with the same .column(), .group() and .dynamic() calls as a schema. Its columns go where .columns(set) is called.

The row a set reads

The first type argument of defineColumns is the part of the row the set reads, not a full row type. A schema can use the set when its row has those fields with the same types. If it does not, .columns(set) is a type error that names the fields:

import { 
createExcelSchema
,
defineColumns
} from "typed-xlsx";
const
audit
=
defineColumns
<{
createdAt
:
Date
;
updatedBy
: string }>()
.
column
("createdAt", {
accessor
: "createdAt" })
.
column
("updatedBy", {
accessor
: "updatedBy" });
type
Lead
= {
email
: string;
createdAt
: string };
createExcelSchema
<
Lead
>().
columns
(audit);
Argument of type 'SchemaBuilder<{ createdAt: Date; updatedBy: string; }, "createdAt" | "updatedBy", never, never, unknown, never, WithColumnValue<WithColumnValue<EmptyColumnValues, "createdAt", Date>, "updatedBy", string>>' is not assignable to parameter of type 'SchemaBuilder<{ createdAt: Date; updatedBy: string; }, "createdAt" | "updatedBy", never, never, unknown, never, WithColumnValue<WithColumnValue<EmptyColumnValues, "createdAt", Date>, "updatedBy", string>> & "The column set reads row fields that this schema's row lacks or types differently: createdAt, updatedBy​"'. Type 'SchemaBuilder<{ createdAt: Date; updatedBy: string; }, "createdAt" | "updatedBy", never, never, unknown, never, WithColumnValue<WithColumnValue<EmptyColumnValues, "createdAt", Date>, "updatedBy", string>>' is not assignable to type '"The column set reads row fields that this schema's row lacks or types differently: createdAt, updatedBy​"'.

Ids join the schema

The set's column ids, group ids and dynamic ids become the schema's. Formulas declared after .columns(set) can reference them, and so can select and summaries:

import { 
createExcelSchema
,
createWorkbook
,
defineColumns
} from "typed-xlsx";
const
money
=
defineColumns
<{
net
: number;
vat
: number }>()
.
column
("net", {
header
: "Net",
accessor
: "net",
format
: "#,##0.00" })
.
column
("vat", {
header
: "VAT",
accessor
: "vat",
format
: "#,##0.00" });
type
Line
= {
sku
: string;
net
: number;
vat
: number };
const
schema
=
createExcelSchema
<
Line
>()
.
column
("sku", {
header
: "SKU",
accessor
: "sku" })
.
columns
(
money
)
.
column
("gross", {
header
: "Gross",
format
: "#,##0.00",
formula
: ({
refs
}) =>
refs
.
column
("net").
add
(
refs
.
column
("vat")),
}) .
build
();
createWorkbook
()
.
sheet
("Lines")
.
table
("lines", {
schema
,
rows
: [],
select
: {
include
: ["sku", "net", "gross"] } });

Every id must be new in the schema. Reusing an id the schema already declares is a type error that names it. Adding a set whose ids are already at the same level throws when the schema is built, and two written columns with the same id throw when the table is written (for example a column the types can't see, inside a group callback without return).

Formulas inside a set

A formula in a set can reference the set's own columns declared before it, with refs.column(...) and refs.group(...). It cannot reference the columns of the schemas that will use the set: they are not known when the set is defined. Put those formulas in the schema, after .columns(set).

import { 
defineColumns
} from "typed-xlsx";
const
pricing
=
defineColumns
<{
unitPrice
: number;
quantity
: number }>()
.
column
("unitPrice", {
header
: "Unit price",
accessor
: "unitPrice" })
.
column
("quantity", {
header
: "Qty",
accessor
: "quantity" })
.
column
("lineTotal", {
header
: "Line total",
formula
: ({
refs
}) =>
refs
.
column
("unitPrice").
mul
(
refs
.
column
("quantity")),
});

Context

A set that reads the schema context declares the fields it needs as the second type argument. The schema's context must have them:

import { 
createExcelSchema
,
defineColumns
} from "typed-xlsx";
const
amount
=
defineColumns
<{
amount
: number }, {
currency
: string }>().
column
("amount", {
header
: ({
ctx
}) => `Amount (${
ctx
.
currency
})`,
accessor
: "amount",
}); type
Payment
= {
id
: string;
amount
: number };
createExcelSchema
<
Payment
, {
currency
: string;
locale
: string }>()
.
column
("id", {
accessor
: "id" })
.
columns
(
amount
)
.
build
();

Using amount in a schema whose context has no currency is a type error naming currency.

Excel table mode

A set is defined for one mode, like a schema. Pass { mode: "excel-table" } for a set used in Excel table schemas:

import { 
createExcelSchema
,
defineColumns
} from "typed-xlsx";
const
tableAudit
=
defineColumns
<{
createdAt
:
Date
}>({
mode
: "excel-table" }).
column
("createdAt", {
header
: "Created",
accessor
: "createdAt",
});
createExcelSchema
<{
id
: string;
createdAt
:
Date
}>({
mode
: "excel-table" })
.
column
("id", {
header
: "Id",
accessor
: "id" })
.
columns
(
tableAudit
)
.
build
();

Using a report set in an Excel table schema, or the reverse, is a type error. The two modes accept different column options (summaries in reports, totals rows in tables), so a set is checked against its own mode when it is defined.

Sets inside groups

.columns(set) also works in a group callback, to put the set under a group header:

import { 
createExcelSchema
,
defineColumns
} from "typed-xlsx";
const
audit
=
defineColumns
<{
createdAt
:
Date
;
updatedBy
: string }>()
.
column
("createdAt", {
header
: "Created",
accessor
: "createdAt" })
.
column
("updatedBy", {
header
: "By",
accessor
: "updatedBy" });
createExcelSchema
<{
id
: string;
createdAt
:
Date
;
updatedBy
: string }>()
.
column
("id", {
accessor
: "id" })
.
group
("history", {
header
: "History" }, (
group
) =>
group
.
columns
(
audit
))
.
build
();
Copyright © 2026 Cyprien Thao. Released under the MIT License.