Help

Re: New: create tables, create fields, and update field options from the Scripting App

4854 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Kevin_Wilde
Airtable Employee
Airtable Employee

We recently added the ability to create tables, create fields, and update field options from the scripting app. The methods to perform each of these actions are the same as in the Blocks SDK.

The field types that are allowed to be created via base.createTableAsync and table.createFieldAsync are:

  • barcode
  • checkbox
  • currency
  • date
  • dateTime
  • duration
  • email
  • multilineText
  • multipleAttachments
  • multipleCollaborators
  • multipleRecordLinks
  • multipleSelects
  • number
  • percent
  • phoneNumber
  • rating
  • richText
  • singleCollaborator
  • singleLineText
  • singleSelect
  • url

The field types which cannot be created are:

  • autoNumber
  • button
  • count
  • createdBy
  • createdTime
  • externalSyncSource
  • formula
  • lastModifiedBy
  • lastModifiedTime
  • multipleLookupValues
  • rollup

These three new methods are currently only supported in the scripting app, not in automation “Run a script” actions.

Finally, here is an example that showcases these methods. The source code can be found below.

61156e2c2cfc7764851315

output.markdown(`# Base Creator`)
 
const addTableOrField = await input.buttonsAsync('Do you want to create a new table or add fields to an existing table?', [
   {label: 'Create new table', value: 'table'},
   {label: 'Add fields to existing table', value: 'fields'},
]);
 
if (addTableOrField === 'table') {
   const tableName = await input.textAsync('Name of table to create');
   output.markdown('Each table needs at least one field. This field will be used as the _primary_ field for the table.')
   const primaryFieldName = await input.textAsync('Name of primary field');
   const primaryFieldType = await selectFieldTypeAsync('Type of primary field');
   let tableId = await base.createTableAsync(tableName, [
       {name: primaryFieldName, type: primaryFieldType, options: getFieldTypeOptions(primaryFieldType)}
   ])
   output.markdown(`**Created table "${tableName}" (${tableId})**`);
 
   const moreFields = await input.buttonsAsync('Would you like to create additional fields now?', ['Yes', 'No']);
   if (moreFields === 'Yes') {
       await createMultipleNewFieldsAsync(tableId);
   }
} else {
   const table = await input.tableAsync('Which table do you want to add fields to?');
   await createMultipleNewFieldsAsync(table.id);
}
 
/**
* @param {string} label
*/
async function selectFieldTypeAsync(label) {
   return input.buttonsAsync(label, [
       // There are more field types beyond these!
       {label: 'Text', value: 'singleLineText'},
       {label: 'Multiline Text', value: 'multilineText'},
       {label: 'Number', value: 'number'},
       {label: 'Email', value: 'email'},
       {label: 'URL', value: 'url'},
   ]);
}
 
/**
* @param {Field['type']} fieldType
*/
function getFieldTypeOptions(fieldType) {
   switch (fieldType) {
       case 'singleLineText':
       case 'multilineText':
       case 'email':
       case 'url':
           return null;
       case 'number':
           return {precision: 5}
       default:
           throw new Error(`Unexpected field type ${fieldType}`)
   }
}
 
/**
* @param {string} tableId
*/
async function createMultipleNewFieldsAsync(tableId) {
   const table = base.getTable(tableId)
   let numFields;
   while (numFields === undefined) {
       numFields = parseInt(await input.textAsync('How many new fields?'));
       if (isNaN(numFields)) {
           output.text('Please enter a number.');
           numFields = undefined;
       }
   }
 
   for (let i = 0; i < numFields; i++) {
       const fieldName = await input.textAsync(`Name of additional field ${i+1}/${numFields}`);
       const fieldType = await selectFieldTypeAsync('Type of field');
       const fieldId = await table.createFieldAsync(fieldName, fieldType, getFieldTypeOptions(fieldType));
       output.markdown(`**Created field "${fieldName}" (${fieldId})**`);
   }
}

13 Replies 13
Patrick-Kennedy
7 - App Architect
7 - App Architect

Fantastic - thank you for posting this!

Francois_NOEL
4 - Data Explorer
4 - Data Explorer

I was wondering if you also intended to add in the scripting app the possibility of duplicating an existing table?
I don’t see this possibility anywhere (but maybe I looked wrong?)

Rapha
4 - Data Explorer
4 - Data Explorer

Hello @Kevin_Wilde ,

About the list of fields which cannot be created nor updated, is it a security reason to not implement this or is it a developer priority choice on your side?

Just to know if we could expect this one day or not :slightly_smiling_face:
Thanks

Sergei_RasKar
6 - Interface Innovator
6 - Interface Innovator

Table and Fields are great!
But what about the VIEWS??!!

Ability to programmatically views is really anticipated!