Last active
September 1, 2024 00:16
-
-
Save kopischke/35b0511d86504e466e50f08e19404c84 to your computer and use it in GitHub Desktop.
Code snippets for Script actions in Airtable Automations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Utility functions for Airtable Automations | |
/** | |
* @param {string} url | |
* @param {Base} [forBase=base] | |
*/ | |
function getTableFromURL(url, forBase) { | |
let root = forBase ? forBase : base | |
let found = url.match(/^https?:\/\/airtable.com\/(?<tableID>[^\/]+)/) | |
return root.getTable(found.groups.tableID) | |
} | |
/** | |
* @param {string} id | |
* @param {Table} inTable | |
*/ | |
async function getRecordByID(id, inTable) { | |
let records = await inTable.selectRecordsAsync() | |
return records.getRecord(id) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Code snippets for Airtable Automations | |
// Update record fields, by default only when they have no value yet | |
const update = {} | |
const fields = [ | |
{name: '__name__', value: '__value__'}, | |
{name: '__name__', value: '__value__', always: true} | |
] | |
fields.forEach(f => { | |
if (f.always || !record.getCellValue(f.name)) update[f.name] = f.value | |
}) | |
if (Object.keys(update).length) await table.updateRecordAsync(record.id, update) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment