Grist Widget API - Get column names and types?

Hi! I’ve been pouring through the grist widget api documentation at https://support.getgrist.com/code/modules/grist_plugin_api/ and can’t seem for the life of me to find a way for us to be able to get column names and their types.

Is this even possible through the widget api?

Thanks

Officially it is not supported yet. The assumption is that a custom widget should know very little about the table it is used on. The dependency is inverted here, widget should inform Grist what columns it expects and Grist should map those columns back. You can read more about column mappings here Custom - Grist Help Center

But if you really need to get this information, you can use general API exposed to widgets and just query for the metdata. Here is an example how the Calendar widget is doing it:

1 Like

I truly appreciate the quick response!

This has resolved my issue. Unfortunately, I need to query the column information, as the widget I’m building is meant to work with different types of tables, with an arbitrary amount of columns. Perhaps this is something that could be officially supported in the future?

I’m including what I’ve ended up doing below in case it’s helpful to others. (Get all columns and their details, for the selected table)
Thank you!

async getAllColumns() {
  const table = await grist.getTable();
  const tableId = await table._platform.getTableId();
  const tables = await grist.docApi.fetchTable('_grist_Tables');
  const columns = await grist.docApi.fetchTable('_grist_Tables_column');
  const fields = Object.keys(columns);
  const tableRef = tables.id[tables.tableId.indexOf(tableId)];
  const tableColumns = [];
  for (const index in columns.parentId) {
    if (columns.parentId[index] === tableRef) {
      tableColumns.push(
        Object.fromEntries(fields.map((f) => [f, columns[f][index]])),
      );
    }
  }
  return tableColumns;
}
1 Like