Skip to content

ReferenceAPI

Sheet

A worksheet: ranges, tables, rules, validation, charts and pictures.

Sheet.md

A worksheet: ranges, tables, rules, validation, charts and pictures. The same text agents read through help().

One sheet of a workbook. Get cells with sheet.range("A1:C3").

Option Type Description
showGridLines (required) boolean Hide the cell grid lines.
tabColor (required) string | undefined The sheet tab’s color, e.g. “accent2”.
zoom (required) number Zoom percent (10–400).
name (required) string The sheet’s name, as on its tab.
refName (required) string The name as formulas write it (‘Q1 Plan’).
spills (required) readonly { anchor: string; range: string }[] Arrays that formulas spilled into neighbouring cells: their anchor and the range filled.
range() (required) method A rectangle of cells: “A1:C3”, “B2”, “A:C” or “2:5”.
usedRange (required) Range | undefined The smallest range holding every cell with content or a format, or undefined for an empty sheet.
setColumnWidth() (required) method Sets column widths in px: sheet.setColumnWidth("A", 160), sheet.setColumnWidth("B:F", 96).
columnWidth() (required) method Column width in px (64 by default).
setRowHeight() (required) method Sets row heights in px: sheet.setRowHeight(1, 32), sheet.setRowHeight("2:10", 24).
rowHeight() (required) method Row height in px (20 by default); rows are 1-based.
autofitColumns() (required) method Widens columns to fit what their cells show (an estimate from character counts), within min and max px: sheet.autofitColumns("A:F").
freeze() (required) method Keeps rows above and columns left of the split in view: sheet.freeze(1) or sheet.freeze(1, 1).
frozenPanes (required) { rows: number; columns: number } Frozen rows and columns.
merge() (required) method Merges cells: sheet.merge("A1:D1").
unmerge() (required) method Removes the merge that covers this address.
mergedRanges (required) string[] Merged ranges.
clear() (required) method Empties the sheet to rebuild it: cells, formats, merges, tables, conditional formats, validation, charts, pictures and sparklines go; column widths, row heights and frozen panes stay.
import() (required) method Writes a table given as text: CSV or TSV, a Markdown pipe table or an HTML table (format detected).
whatIf() (required) method A what-if data table, like Excel’s Data Table command.
dataTables (required) readonly { range: string; rowInput?: string; columnInput?: string }[] The what-if data tables on the sheet (whole table ranges and input cells).
pageSetup() (required) method How the sheet prints: sheet.pageSetup({ orientation: "landscape", paper: "a4", fitTo: { width: 1 }, printArea: "A1:F40", repeatRows: "1:2", footer: "&CPage &P of &N" }).
printSetup (required) PageSetupInput The sheet’s print setup, as pageSetup() takes it.
protect() (required) method Protects the sheet: locked cells (all, unless formatted with locked: false) can’t be changed in Excel.
unprotect() (required) method Removes the sheet’s protection.
isProtected (required) boolean Whether the sheet is protected.
pivots.… (required) collection
sparklines.… (required) collection
tables.… (required) collection
conditionalFormats.… (required) collection Conditional formats: add, list (the file’s own too), replace and remove rules.
validations.… (required) collection Data validation: add, list (the file’s own too), replace and remove rules.
fileDrawings (required) readonly { name: string; kind: "chart" | "picture"; from: string; to: string }[] Charts and pictures that came with the file: they are kept as they were, drawn in previews, and can be removed with removeFileDrawing(name).
removeFileDrawing() (required) method Removes a chart or picture that came with the file, by its name.
charts.… (required) collection
images.… (required) collection
render() (required) method Draws part of the sheet as an image, so you can look at it: values as formatted, fills, borders, conditional formats, charts and pictures.
sheet.autofitColumns(columns?: string, options: { min?: number; max?: number }?): this

Widens columns to fit what their cells show (an estimate from character counts), within min and max px: sheet.autofitColumns("A:F").

sheet.autofitColumns("A:F")
sheet.charts.add(type: ChartType, options: SheetChartOptions): SheetChart

A native chart of cells on this (or another) sheet. The first row names the series and the first column holds the categories: charts.add("column", { data: "A1:C13", at: "E2", title: "Revenue by month" }).

Options: SheetChartOptions.

charts.add("column", { data: "A1:C13", at: "E2", title: "Revenue by month" })
sheet.charts.items: readonly SheetChart[]

The charts on this sheet.

sheet.clear(): this

Empties the sheet to rebuild it: cells, formats, merges, tables, conditional formats, validation, charts, pictures and sparklines go; column widths, row heights and frozen panes stay.

sheet.columnWidth(column: string | number): number

Column width in px (64 by default).

sheet.conditionalFormats.add(ranges: string | string[], rule: ConditionalRuleInput): Worksheet

Formats cells by rule: add("C2:C13", { type: "cellIs", operator: "<", value: 0, format: { color: "red-700" } }), add("D2:D13", { type: "dataBar", color: "accent1" }), add("B2:B13", { type: "colorScale", colors: ["white", "green-400"] }).

Options: ConditionalRuleInput.

add("C2:C13", { type: "cellIs", operator: "<", value: 0, format: { color: "red-700" } })
add("D2:D13", { type: "dataBar", color: "accent1" })
add("B2:B13", { type: "colorScale", colors: ["white", "green-400"] })
sheet.conditionalFormats.clear(range?: string): Worksheet

Removes every rule, or those touching a range: clear("C2:C40").

clear("C2:C40")
sheet.conditionalFormats.count(): number

How many conditional format rules the sheet has.

sheet.conditionalFormats.items: readonly { ranges: string[]; rule?: ConditionalRuleInput; fromFile: boolean; kind: string }[]

The rules, in order (later ones apply first in Excel), including those read from the file. A file rule Artiwright can’t express has no rule; it is kept and can be removed.

Options: ConditionalRuleInput.

sheet.conditionalFormats.remove(index: number): Worksheet

Removes a rule by its position in items.

sheet.conditionalFormats.replace(index: number, rule: ConditionalRuleInput, ranges?: string | string[]): Worksheet

Replaces a rule (by its position in items) and keeps its ranges unless new ones are given.

Options: ConditionalRuleInput.

dataTables: sheet.dataTables: readonly { range: string; rowInput?: string; columnInput?: string }[]

The what-if data tables on the sheet (whole table ranges and input cells).

fileDrawings: sheet.fileDrawings: readonly { name: string; kind: "chart" | "picture"; from: string; to: string }[]

Charts and pictures that came with the file: they are kept as they were, drawn in previews, and can be removed with removeFileDrawing(name).

sheet.freeze(rows: number, columns?): this

Keeps rows above and columns left of the split in view: sheet.freeze(1) or sheet.freeze(1, 1).

sheet.freeze(1)
sheet.freeze(1, 1)
frozenPanes: sheet.frozenPanes: { rows: number; columns: number }

Frozen rows and columns.

sheet.images.add(options: SheetImageOptions): SheetImage

A picture at a cell: images.add({ source: "logo.png", at: "H2", size: { w: 120, h: 40 } }).

Options: SheetImageOptions.

images.add({ source: "logo.png", at: "H2", size: { w: 120, h: 40 } })
sheet.images.items: readonly SheetImage[]

The pictures on this sheet.

sheet.import(text: string, options: ImportOptions?): Range

Writes a table given as text: CSV or TSV, a Markdown pipe table or an HTML table (format detected). Numbers, percentages, currency, dates and true/false become typed values in the format they were written in; a header row is bolded. Returns the range written: sheet.import(csv, { at: "A1", table: "Sales" }). Text starting with “=” stays text unless formulas: true.

sheet.import(csv, { at: "A1", table: "Sales" })
isProtected: sheet.isProtected: boolean

Whether the sheet is protected.

sheet.merge(address: string): this

Merges cells: sheet.merge("A1:D1").

sheet.merge("A1:D1")
mergedRanges: sheet.mergedRanges: string[]

Merged ranges.

name: sheet.name: string

The sheet’s name, as on its tab.

sheet.pageSetup(input: PageSetupInput): this

How the sheet prints: sheet.pageSetup({ orientation: "landscape", paper: "a4", fitTo: { width: 1 }, printArea: "A1:F40", repeatRows: "1:2", footer: "&CPage &P of &N" }). Options left out stay as they were.

sheet.pageSetup({ orientation: "landscape", paper: "a4", fitTo: { width: 1 }, printArea: "A1:F40", repeatRows: "1:2", footer: "&CPage &P of &N" })
sheet.pivots.add(source: string, options: PivotOptions): PivotInfo

A pivot table from a range (“Data!A1:E200”, first row the field names) or a table name: sheet.pivots.add("Data!A1:E200", { at: "H3", rows: ["Region", "Rep"], columns: "Quarter", values: { field: "Sales", function: "sum" }, filters: "Year" }). The result is written into the cells; Excel rebuilds it from the source when it opens the file.

sheet.pivots.add("Data!A1:E200", { at: "H3", rows: ["Region", "Rep"], columns: "Quarter", values: { field: "Sales", function: "sum" }, filters: "Year" })
sheet.pivots.get(name: string): PivotInfo | undefined

A pivot table on this sheet by name.

sheet.pivots.items: readonly PivotInfo[]

The pivot tables made on this sheet.

sheet.pivots.refresh(): void

Recomputes the pivot tables from their sources (after the data changed) and rewrites their cells.

sheet.pivots.remove(name: string): void

Removes a pivot table and empties its cells.

printSetup: sheet.printSetup: PageSetupInput

The sheet’s print setup, as pageSetup() takes it.

sheet.protect(input: ProtectInput?): this

Protects the sheet: locked cells (all, unless formatted with locked: false) can’t be changed in Excel. sheet.protect({ password: "plan", allow: ["sort", "autoFilter"] }).

sheet.protect({ password: "plan", allow: ["sort", "autoFilter"] })
sheet.range(address: string): Range

A rectangle of cells: “A1:C3”, “B2”, “A:C” or “2:5”.

refName: sheet.refName: string

The name as formulas write it (‘Q1 Plan’).

sheet.removeFileDrawing(name: string): this

Removes a chart or picture that came with the file, by its name.

sheet.render(options: SheetRenderOptions?): Promise<Uint8Array>

Draws part of the sheet as an image, so you can look at it: values as formatted, fills, borders, conditional formats, charts and pictures. Default: every used cell, with headers. await sheet.render({ range: "A1:H20" }).

Options: SheetRenderOptions.

await sheet.render({ range: "A1:H20" })
sheet.rowHeight(row: number): number

Row height in px (20 by default); rows are 1-based.

sheet.setColumnWidth(columns: string, px: number): this

Sets column widths in px: sheet.setColumnWidth("A", 160), sheet.setColumnWidth("B:F", 96).

sheet.setColumnWidth("A", 160)
sheet.setColumnWidth("B:F", 96)
sheet.setRowHeight(rows: number | string, px: number): this

Sets row heights in px: sheet.setRowHeight(1, 32), sheet.setRowHeight("2:10", 24). Rows are 1-based like Excel’s.

sheet.setRowHeight(1, 32)
sheet.setRowHeight("2:10", 24)
showGridLines: sheet.showGridLines: boolean

Hide the cell grid lines.

sheet.sparklines.add(location: string, data: string, options: SparklineOptions?): SparklineGroupInfo

Small charts inside cells, one per row (or column) of data: sheet.sparklines.add("F2:F10", "B2:E10", { type: "line", high: true, low: "red" }). Types: line, column, winLoss. Data can be on another sheet (“Data!B2:E10”).

sheet.sparklines.add("F2:F10", "B2:E10", { type: "line", high: true, low: "red" })
sheet.sparklines.clear(): void

Removes every sparkline on the sheet.

sheet.sparklines.items: readonly SparklineGroupInfo[]

The sheet’s sparkline groups.

sheet.sparklines.remove(location: string): void

Removes the sparklines in a range of cells: sheet.sparklines.remove("F2:F10").

sheet.sparklines.remove("F2:F10")
spills: sheet.spills: readonly { anchor: string; range: string }[]

Arrays that formulas spilled into neighbouring cells: their anchor and the range filled.

tabColor: sheet.tabColor: string | undefined

The sheet tab’s color, e.g. “accent2”.

sheet.tables.add(address: string, options: SheetTableOptions?): SheetTable

Turns cells into an Excel table: the first row names the columns. Totals add a row below: sheet.tables.add("A1:D13", { name: "Sales", totals: { Revenue: "sum", Region: { label: "Total" } } }).

Options: SheetTableOptions.

sheet.tables.add("A1:D13", { name: "Sales", totals: { Revenue: "sum", Region: { label: "Total" } } })
sheet.tables.get(name: string): SheetTable | undefined

A table on this sheet by name.

sheet.tables.items: readonly SheetTable[]

The tables on this sheet.

sheet.tables.remove(name: string): void

Turns a table back into plain cells (they keep their values and formats).

sheet.tables.resize(name: string, address: string): SheetTable

Grows or shrinks a table to a new range with the same top-left (header) cell, e.g. after adding a row below it: sheet.tables.resize("Sales", "A1:F8"). Formulas that use the table (SUM(Sales[Revenue])) then cover the new rows. New columns are named by their header cells.

sheet.tables.resize("Sales", "A1:F8")
SUM(Sales[Revenue])
sheet.unmerge(address: string): this

Removes the merge that covers this address.

sheet.unprotect(): this

Removes the sheet’s protection.

usedRange: sheet.usedRange: Range | undefined

The smallest range holding every cell with content or a format, or undefined for an empty sheet.

sheet.validations.add(ranges: string | string[], rule: ValidationInput): Worksheet

Limits what can be typed in cells: add("E2:E20", { type: "list", values: ["Low", "High"] }), add("B2:B20", { type: "decimal", min: 0, max: 1, error: "Use a rate from 0 to 1." }).

Options: ValidationInput.

add("E2:E20", { type: "list", values: ["Low", "High"] })
add("B2:B20", { type: "decimal", min: 0, max: 1, error: "Use a rate from 0 to 1." })
sheet.validations.clear(range?: string): Worksheet

Removes every rule, or those touching a range.

sheet.validations.count(): number

How many validation rules the sheet has.

sheet.validations.items: readonly { ranges: string[]; rule?: ValidationInput; fromFile: boolean; kind: string }[]

The rules, including those read from the file (a file rule Artiwright can’t express has no rule).

Options: ValidationInput.

sheet.validations.remove(index: number): Worksheet

Removes a rule by its position in items.

sheet.validations.replace(index: number, rule: ValidationInput, ranges?: string | string[]): Worksheet

Replaces a rule (by its position in items) and keeps its ranges unless new ones are given.

Options: ValidationInput.

sheet.whatIf(range: string, options: { rowInput?: string; columnInput?: string }): Range

A what-if data table, like Excel’s Data Table command. range is the whole table: with a column input, values to try run down its first column and formulas across its top row; with a row input, values across the top row and formulas down the first column; with both, the top-left cell is the formula. Results fill the rest and Excel recomputes them: sheet.whatIf("D2:F8", { columnInput: "B1" }).

sheet.whatIf("D2:F8", { columnInput: "B1" })
zoom: sheet.zoom: number

Zoom percent (10–400).