grist-core icon indicating copy to clipboard operation
grist-core copied to clipboard

Create table over API

Open Razikus opened this issue 1 year ago • 3 comments

Hello, is there possibility to crate table over Grist API?

Razikus avatar Aug 19 '22 07:08 Razikus

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

paulfitz avatar Aug 19 '22 12:08 paulfitz

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!

Razikus avatar Aug 22 '22 06:08 Razikus

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

paulfitz avatar Sep 09 '22 14:09 paulfitz

Update: there is dedicated API support for creating tables and columns now:

paulfitz avatar Oct 31 '22 19:10 paulfitz