Re-Arrange column in a table through API

i re arrange the columns of table manually from the sidebar and opened the console, i got the api,
_grist_Views_section_field, but im not sure what field i should change to re-order my columns.
So if i have A-B-C, and i want it to be C-A-B. what should i do?
And how can i get the col id, because in the api get - docs/docId/tables/tableId/columns the id is the name of the col not the number.

Hi @Ghady_Gergie , and welcome to the community!

You are right that _grist_Views_section_field is the right place to change. This determines which fields are shown in the “view section” (a widget on a page). The section is determined by parentId and the order is determined by parentPos.

To rearrange, here are suggested steps:

  1. Find the ID of the view section. Most easily, hover over the “Download as CSV” link for that section, and look for viewSection=N parameter. Use that number N below.
  2. Find column of interest using GET /columns API. Get its numeric ID using .fields.colRef.
  3. Find the list of fields in viewSection using GET /records API for _grist_Views_section_field table, with filter {"parentId": [N]} (using view-section number from above)
  4. In this list of fields, one will have colRef from step 2, get the ID of that field (let’s call it fieldRef below). All fields will have parentPos — order is determined by sorted parentPos. If you set parentPos of a field to parentPos of an existing field, it will get adjusted to be just before that existing field. If you set parentPos to null, it will get adjusted to go at the end.
  5. Use PATCH /records endpoint for _grist_Views_section_field with a payload like {"records": [{"id": fieldRef, "fields": {"parentPos": newPosition}}]}

Hope this helps!