Last active
April 27, 2020 15:31
-
-
Save on2air/7be08c3a5881df141f13f380dde7e444 to your computer and use it in GitHub Desktop.
Create fully formatted links in Airtable
This file contains hidden or 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
/***** | |
* Title: Better Links | |
* License: MIT | |
* Author: Openside (Team behind On2Air products and BuiltOnAir community) | |
* Sites: | |
* https://openside.com - Openside Consulting Services | |
* https://openside.com/#products - On2Air Products | |
* https://builtonair.com - All things Airtable Community | |
* | |
* Reach out for all your Airtable needs | |
* | |
* Description: Finally, the ability to create Links formatted the way you want. No more ugly full URL links you have to look at and click on. | |
* This takes advantage of the new 'Rich Text' field type | |
* | |
* Instructions: Configure the links array below with the information to generate the links | |
* | |
*/ | |
//-------------START CONFIGURATION ------------------// | |
let links = [ | |
{ | |
table: 'Links', //The table name or ID of where we will be updating | |
view: '', //Optional view used to filter to only update records within this view. Useful if only want to create links for certain records | |
link_field: 'Link', //The field name or ID of the field that contains the URL | |
label: 'Link', //Optional: If you want all links to have the same label, then simply set that value here | |
label_field:'Label',//Optional: To set a dynamic link label, this is the field name or ID containing the value to use for the link. This will be used before 'label' if value is set | |
output_field: 'Formatted Link'//Where the outputed link will be updated. This MUST be a Rich Text field type | |
}, | |
{ | |
table: 'tbleZjBNB6NClnhQN', | |
view: 'WithID', | |
link_field: 'IMDB Raw', | |
label: '', | |
label_field:'Label', | |
output_field: 'IMDB Link' | |
}, | |
//add as many links definitions you want to include for update in this block. They can be in the same table or a different table, but must be in | |
//the base that runs this script. | |
] | |
//-------------END CONFIGURATION ------------------// | |
let stats = [] | |
for(let l = 0; l<links.length; l++){ | |
let info = links[l] | |
let table = base.getTable(info.table) | |
let view = info['view'] ? table.getView(info.view) : null | |
let output = info.output_field | |
let records = view ? await view.selectRecordsAsync() : await table.selectRecordsAsync() | |
let total = records.records.length, refreshed = 0 | |
let updates = [] | |
for(let r=0; r<records.records.length; r++){ | |
let record = records.records[r] | |
let label = info['label_field'] ? record.getCellValueAsString(info.label_field) : info['label'] || 'Link' | |
let link = record.getCellValueAsString(info.link_field) | |
if(!link)continue; | |
let existing = record.getCellValueAsString(output) | |
if(existing)refreshed++ | |
let text = `[${label}](${link})` | |
let data = {} | |
data[output] = text | |
updates.push({id:record.id,fields:data}) | |
} | |
while(updates.length){ | |
let upRecs = updates.slice(0, 50) | |
await table.updateRecordsAsync(upRecs) | |
updates = updates.slice(50); | |
} | |
stats.push({Table: table.name, View: view ? view.name : "-", "Link Field": output, Created: total - refreshed, Refreshed:refreshed}) | |
} | |
output.markdown('## Links Summary') | |
output.table(stats) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
First of all, thanks for this code !
Maybe adding "if (link) { ... }" helps to avoid an output like this, having a label with no link : "[ label ] ( )"
This is in case not every records have a link of course
if(link) { let existing = record.getCellValueAsString(output) if(existing)refreshed++ let text =
${label}let data = {} data[output] = text updates.push({id:record.id,fields:data}) }
Cheers