Write data
Write rows under the declared names, write any table under its own column ids, change the schema, and show the write's status.
A widget writes three ways, from the simplest:
- under your declared names:
updateRecordandaddRecord; - any table, under its own column ids:
w.getTable(tableId); - anything else Grist does, the schema included:
applyActions.
All three need requiredAccess: "full".
Under your declared names
import { defineGristWidget } from "grist-widget-sdk"
export const widget = defineGristWidget({
requiredAccess: "full",
columns: [
{ name: "Title", type: "Text" },
{ name: "Tags", type: "ChoiceList" },
],
})
export function Actions() {
const w = widget.useGrist()
return (
<>
<button onClick={() => w.updateRecord({ Tags: ["urgent"] })}>Tag urgent</button>
<button onClick={() => w.updateRecord(7, { Title: "Renamed" })}>Rename row 7</button>
<button onClick={() => w.addRecord({ Title: "New task" })}>Add</button>
</>
)
}w.updateRecord(patch)writes the selected row;w.updateRecord(rowId, patch)any row of the table.w.addRecord(patch)adds a row and resolves to its id.- Each name is written into the column the person mapped to it, and each
value is encoded for its declared type. What
w.mappedRecordgives can be written back as is: astring[]to a ChoiceList, aDateto a Date column. - A name mapped to several columns (
allowMultiple) isn't written: the "Write skipped" alert names it.
Any table, under its own column ids
const tasks = w.getTable("Tasks")
await tasks.create([{ fields: { Title: "Buy milk" } }, { fields: { Title: "Walk the dog" } }])
await tasks.update({ id: 42, fields: { Done: true } })
await tasks.upsert({ require: { Email: "alice@example.com" }, fields: { Name: "Alice" } })
await tasks.destroy([1, 2, 3])w.table is the same for the section's table. Each method takes one
record or a list. create resolves to the new id (or ids); upsert
updates the rows matching require, or adds one (its options: add,
update, onMany).
These write values as given, as Grist's plugin API does: a bare list written to a ChoiceList, RefList or Attachments column is stored as an error cell. Encode such values for the column's type:
import { encodeGristCell } from "grist-widget-sdk"
encodeGristCell("ChoiceList", ["a", "b"]) // ["L", "a", "b"]
encodeGristCell("ChoiceList", ["L", "M"]) // ["L", "L", "M"]: the sizes L and M
encodeGristCell("RefList:Tasks", [1, 2, 3]) // ["L", 1, 2, 3]
encodeGristCell("Date", new Date("2026-03-16")) // 1773619200The emulator stores a bare list the way Grist does, so pnpm test shows
the mistake: an error cell, listed in w.cellIssues.
The schema, and several actions as one
w.applyActions(actions, { desc }) applies Grist user actions as one step
of the document's undo. The builders in grist-widget-sdk/actions write
them for you:
import { gristAddColumnAction, gristAddTableAction } from "grist-widget-sdk/actions"
await w.applyActions(
[
gristAddTableAction("Reports", { columns: [{ id: "Title", type: "Text" }] }),
gristAddColumnAction("Reports", "CreatedAt", { type: "DateTime" }),
],
{ desc: "Set up the Reports table" },
)There is a builder for each record and column action (gristAddRecordAction,
gristBulkUpdateRecordAction, gristRenameColumnAction, …: see the
reference); any other action is passed as Grist's
own tuple, ["RenameTable", "Old", "New"].
While it writes, and when it fails
w.actionStatus is "running" while any write is in flight (overlapping
writes count as one run), then "idle", or "error" with Grist's message
in w.actionError:
<button disabled={w.actionStatus === "running"} onClick={() => w.updateRecord({ Done: true })}>
Done
</button>A failed write also shows as an alert above the widget, with Grist's
message, or as an access alert when the widget wasn't granted full access
(Errors and alerts). A write's promise
rejects for whoever awaits it; one nobody awaits (onClick={() => w.updateRecord(…)}) logs nothing more: the alert is enough.
On a document open read-only, w.readOnly is true and writes fail: a
widget that writes says so, and offers no button that can't work.
The view after a write
The section's rows update from Grist once the write is done: the widget re-renders with the new values, nothing to refresh. For a control that should change at once, keep the pending value in local state until the next update arrives.
Read data
Read the selected row and the section's rows as Grist updates them, and fetch other tables when the widget needs them.
Errors and alerts
Where a widget's problems show: the screens before it renders, the alerts above it, a failed call, a cell that doesn't fit, and a widget that stops working.