Skip to content

ReferenceAPI

Workbook

Create, open, render, check and save a workbook.

Workbook.md

Create, open, render, check and save a workbook. The same text agents read through help().

Build a workbook with workbook.sheets.add(name), write cells through sheet.range("A1:C3") (values, formulas, format), then look with sheet.render(), check with workbook.lint() and publish with workbook.finalize(). Formulas compute as you write them; arrays spill. Help topics: workbook, sheet, range, CellFormatInput, SheetTableOptions, ConditionalRuleInput, ValidationInput, SheetChartOptions, functions, numberFormats.

const s = workbook.sheets.add("Model"); s.range("A1").values = [["Month", "Revenue"], ["Jan", 120], ["Feb", 150]]; s.range("B4").formula = "=SUM(B2:B3)";
Workbook.create(options: { theme?: ThemePresetName | ThemePreset | string }?): Workbook

A new, empty workbook; add sheets with wb.sheets.add("Name"). theme applies a preset (Workbook.create({ theme: "ledger" })): its colors and fonts for tables, charts and rules.

wb.sheets.add("Name")
Workbook.create({ theme: "ledger" })
Workbook.fromJSON(json: WorkbookJSON): Workbook

A workbook from toJSON().

Workbook.open(file: string | Uint8Array): Promise<Workbook>

Opens an .xlsx (or an .xltx template) to edit it: const wb = await Workbook.open("plan.xlsx"). Cells, formats, tables, rules and charts are read; what you change is saved into the file and everything else (pivot tables, macros-free extras, styles you don’t touch) stays as it was.

const wb = await Workbook.open("plan.xlsx")

An Excel workbook: const wb = Workbook.create(); const s = wb.sheets.add("Model"); then s.range("A1").values = [["Month", "Revenue"], ["Jan", 120]] and await wb.save("model.xlsx").

Option Type Description
creationId (required) string A unique id for this workbook.
theme (required) Theme Theme colors and fonts (the same as a deck’s): wb.theme.set({ colors: { accent1: "#2346C8" } }).
now (required) Date | undefined The clock for TODAY() and NOW(); default the real time.
Workbook.create() (required) method A new, empty workbook; add sheets with wb.sheets.add("Name").
Workbook.open() (required) method Opens an .xlsx (or an .xltx template) to edit it: const wb = await Workbook.open("plan.xlsx").
sheets.… (required) collection
names.… (required) collection
recalculate() (required) method Computes every formula now (reads and saves do this on their own when something changed).
circularReferences (required) readonly CircularReference[] Formula cells in a cycle, from the last calculation.
lint() (required) method Checks for formula errors, cycles, #### columns, clipped text, numbers stored as text, typed-in numbers in formulas, unformatted numbers and empty charts.
inspect() (required) method Compact records of the workbook: sheets, cell values row by row, formulas with results, tables, charts and names.
toJSON() (required) method The whole workbook as JSON (cells, formats, tables, rules, charts, names, theme and images), for saving between turns: Workbook.fromJSON(wb.toJSON()) gives an equal workbook.
Workbook.fromJSON() (required) method A workbook from toJSON().
finalize() (required) method Publishes the workbook: exports and validates it, checks your requirements (sheets, tables, charts, expected values in key cells) and lint, then writes a new .xlsx (never overwriting).
trace() (required) method Where a number comes from: the cell’s formula and value, and the cells, ranges and names it reads, followed depth levels down (default 3): wb.trace("Summary!B2").
toProto() (required) method The document model as a protobuf message.
toBytes() (required) method Encodes the workbook as .xlsx bytes using the Artiwright engine.
save() (required) method Writes the workbook to an .xlsx file.
circularReferences: workbook.circularReferences: readonly CircularReference[]

Formula cells in a cycle, from the last calculation.

creationId: workbook.creationId: string

A unique id for this workbook.

workbook.finalize(options: WorkbookFinalizeOptions): Promise<WorkbookFinalizeResult>

Publishes the workbook: exports and validates it, checks your requirements (sheets, tables, charts, expected values in key cells) and lint, then writes a new .xlsx (never overwriting). await wb.finalize({ output: "model.xlsx", requirements: { values: [{ cell: "Checks!B2", equals: 0 }] } }).

Options: WorkbookFinalizeOptions.

await wb.finalize({ output: "model.xlsx", requirements: { values: [{ cell: "Checks!B2", equals: 0 }] } })
workbook.inspect(options: WorkbookInspectOptions?): WorkbookInspectResult

Compact records of the workbook: sheets, cell values row by row, formulas with results, tables, charts and names. wb.inspect({ sheet: "Model", kind: "formula" }).ndjson.

Options: WorkbookInspectOptions.

wb.inspect({ sheet: "Model", kind: "formula" }).ndjson
workbook.lint(options: WorkbookLintOptions?): WorkbookLintResult

Checks for formula errors, cycles, #### columns, clipped text, numbers stored as text, typed-in numbers in formulas, unformatted numbers and empty charts. Each finding names the sheet and cell and suggests a fix. wb.lint({ rules: "formula/" }).

Options: WorkbookLintOptions.

wb.lint({ rules: "formula/" })
workbook.names.add(name: string, refersTo: string, options: { sheet?: string; comment?: string }?): WorkbookNames

Names a cell, range or value for formulas: wb.names.add("Growth", "Inputs!$B$2"), then =B2*(1+Growth). { sheet } scopes the name to one sheet.

wb.names.add("Growth", "Inputs!$B$2")
=B2*(1+Growth)
workbook.names.get(name: string): DefinedNameInput | undefined

A defined name by name (not case-sensitive).

workbook.names.items: readonly DefinedNameInput[]

Every defined name.

workbook.names.remove(name: string): void

Deletes a defined name.

now: workbook.now: Date | undefined

The clock for TODAY() and NOW(); default the real time.

workbook.recalculate(): CircularReference[]

Computes every formula now (reads and saves do this on their own when something changed). Returns formula cells that refer to themselves; those show 0, as in Excel.

workbook.save(path: string): Promise<void>

Writes the workbook to an .xlsx file.

workbook.sheets.active: Worksheet | undefined

The sheet shown when the file opens (default the first).

workbook.sheets.add(name?: string, index?: number): Worksheet

Adds a sheet at the end (or at index): wb.sheets.add("Model").

wb.sheets.add("Model")
workbook.sheets.get(nameOrIndex: string | number): Worksheet | undefined

A sheet by name (not case-sensitive) or 0-based position.

workbook.sheets.items: readonly Worksheet[]

The sheets in tab order.

workbook.sheets.length: number

How many sheets the workbook has.

workbook.sheets.move(sheet: Worksheet | string, to: number): void

Moves a sheet to a position, 0 being first.

workbook.sheets.remove(sheet: Worksheet | string): void

Deletes a sheet.

theme: workbook.theme: Theme

Theme colors and fonts (the same as a deck’s): wb.theme.set({ colors: { accent1: "#2346C8" } }).

workbook.toBytes(): Promise<Uint8Array>

Encodes the workbook as .xlsx bytes using the Artiwright engine.

workbook.toJSON(): WorkbookJSON

The whole workbook as JSON (cells, formats, tables, rules, charts, names, theme and images), for saving between turns: Workbook.fromJSON(wb.toJSON()) gives an equal workbook.

Workbook.fromJSON(wb.toJSON())
workbook.toProto(): pb.Document

The document model as a protobuf message.

workbook.trace(cell: string, options: { depth?: number }?): TraceNode

Where a number comes from: the cell’s formula and value, and the cells, ranges and names it reads, followed depth levels down (default 3): wb.trace("Summary!B2").

levels down (default 3):