grist-core
grist-core copied to clipboard
Create table over API
Hello, is there possibility to crate table over Grist API?
Yes, it is possible. We've been remiss in not creating a friendly documented endpoint for it, but you can see it done in a testcase here: https://github.com/gristlabs/grist-core/blob/4554de661eb7373d27814973907f471c95792f30/test/server/lib/DocApi.ts#L651-L661
POST to this endpoint .../api/docs/<docId>/apply
with the following kind of JSON body:
[
["AddTable", "TableName", [{id: 'Column1Name'}, {id: 'Column2Name'}]]
]
You can specify types of columns, and initialize formulas, see e.g. https://github.com/gristlabs/grist-core/blob/4554de661eb7373d27814973907f471c95792f30/sandbox/grist/testscript.json#L1224-L1227
Ok okay! I see now how to do it! I was inspecting a websocket also so i can extract all messages from WS, and then apply by this endpoint
Cool!
Just to add because someone asked, to add a new column to a table you can do something like:
curl -XPOST -H "Authorization: Bearer XXXXX" \
-H "Content-Type: application/json" \
..../api/docs/NNNN/apply \
-d '[["AddColumn", "Table1", "Col2", {"type": "Text"}]]'
Use AddVisibleColumn
instead of AddColumn
to make it show in existing pages/widgets.
The type
for Choices or Choice Lists looks like:
{"type": "Choice", "widgetOptions": "{\"choices\":[\"Choice1\",\"Choice2\",\"Choice3\"]}"}
{"type": "ChoiceList", "widgetOptions": "{\"choices\":[\"Choice1\",\"Choice2\",\"Choice3\"]}"}
And for References or Reference Lists to records in table Table2
is:
{"type": "Ref:Table2"}
{"type": "RefList:Table2"}
Other type names can be found in https://github.com/gristlabs/grist-core/blob/main/app/common/gristTypes.ts
Update: there is dedicated API support for creating tables and columns now: