Read data
Read the selected row and the section's rows as Grist updates them, and fetch other tables when the widget needs them.
A widget reads two ways:
- what the section shows, kept up to date: the selected row and the section's rows re-render the widget whenever Grist changes them;
- anything else, on demand: another table, the section's rows in Grist's raw form, the document's schema.
Use the first for the widget's view; the second for what it fetches when something happens.
What the section shows
import { defineGristWidget } from "grist-widget-sdk"
export const widget = defineGristWidget({
requiredAccess: "read table",
columns: [{ name: "Title", type: "Text" }],
})
export default function App() {
const w = widget.useGrist()
if (w.mode === "new-row") return <p>Add the row in Grist, then select it.</p>
return (
<section>
<h1>{w.mappedRecord?.Title ?? "Select a row"}</h1>
<p>{w.mappedRecords?.length ?? 0} rows in this section</p>
</section>
)
}w.mappedRecord | The selected row under your declared names, typed, or null (Declare and map columns). |
w.mappedRecords | The section's rows (its filters, its order) the same way. |
w.mode | "row" when a row is selected, "empty" when none is, "new-row" on the blank line Grist adds at the end. |
w.record, w.records | The same rows under the table's own column ids, decoded but unchecked. |
w.widgetOptions | The widget's saved options (Widget options). |
Values arrive decoded: a Date is a Date, a list a list, never Grist's
wire tuples. The mapped rows go further: each cell is checked against its
declared type, and one that doesn't fit is null with a cell issue
(Typed reads).
On demand
These return promises. Under mountGristWidget(), the widget only renders
once it is connected, so they can be called from any handler or effect.
| Method | Returns |
|---|---|
w.fetchRows() | The section's rows, as Grist sends them now. |
w.fetchRows(tableId) | Any table's rows, in Grist's order. Needs full access. |
w.fetchRows(…, { columnar: true }) | Grist's own payload, column by column. |
w.listTables() | The document's tables (pass { includeSystem: true } for Grist's own). |
w.listColumns(tableId) | A table's columns (colId, type, label…), without its rows. |
fetchRows(tableId) gives each cell as Grist sends it. Pass the table's
column metadata (columns, from listColumns or useGristSchema) to have
it decoded by type, and order to sort by "grist" (the table's own
order, the default), "id" or "none".
Watching another table
Only the section's rows update by themselves: Grist sends no event when another table changes. Fetch it again on a timer:
import { useEffect, useState } from "react"
import { defineGristWidget, type GristRowRecord } from "grist-widget-sdk"
export const widget = defineGristWidget({ requiredAccess: "full" })
export function useReports() {
const { fetchRows } = widget.useGrist()
const [rows, setRows] = useState<GristRowRecord[]>([])
useEffect(() => {
const load = () => void fetchRows("Reports").then(setRows, () => {})
load()
const timer = setInterval(load, 10_000)
return () => clearInterval(timer)
}, [fetchRows])
return rows
}The whole document
useGristSchema() (full access) gives the document's tables, columns and
optionally sample rows, as a structure or as one JSON string, for a model's
context for example:
const schema = useGristSchema({ excludeSystemTableData: true, replicaRowMode: "schema+samples", sampleRowLimit: 5 })
schema.documentJson // a JSON string of the document's shapeSee useGristSchema.
A raw cell
To decode a cell you got some other way, or encode one to write it:
import { decodeGristValue, encodeGristValue } from "grist-widget-sdk"
decodeGristValue(["D", 1773619200, "UTC"], { type: "Date" }) // a Date
encodeGristValue(new Date("2026-03-16"), { type: "Date" }) // 1773619200To check a value against a Grist type, use
checkGristCell.