Created
February 25, 2020 00:13
-
-
Save on2air/97ee258d88490daa31af55940867b385 to your computer and use it in GitHub Desktop.
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: Common Functions for Scripting | |
* 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: Some common functions that can help in developing scripts | |
* | |
* | |
* | |
*/ | |
//----------------------------------- | |
//Function: getCollablId | |
//Description: looks up the collaborator id based on email | |
//----------------------------------- | |
const getCollabId = (email) => { | |
email = email.trim().toLowerCase() | |
let collabs = base.activeCollaborators | |
for(let collab of collabs){ | |
if(collab.email.toLowerCase() === email){ | |
return collab.id | |
} | |
} | |
return null | |
} | |
//----------------------------------- | |
//Function: cast | |
//Params: | |
//table - The table model that is being updated (needed to get the field) | |
//field - The field name or id that will be updated | |
//value - the string value to be converted based on the field type | |
//Description: Converts a String value to the | |
//corresponding value needed to perform an update | |
//----------------------------------- | |
const cast = ( table, field, value ) => { | |
let fieldMeta = table.getField(field) | |
let type = fieldMeta.type | |
return castType( type, value ) | |
} | |
//----------------------------------- | |
//Function: castType | |
//Params: | |
//type - the fieldType retrieved from table.getField(field).type | |
//value - the string value to be converted based on the field type | |
//Description: Converts a String value to the | |
//corresponding value needed to perform an update | |
//----------------------------------- | |
const castType = ( type, value ) => { | |
if(type === 'singleSelect'){ | |
value = {name:value} | |
}else if(type === 'multipleSelects'){ | |
let items = value.split(',') | |
let values = [] | |
items.forEach( i => { | |
values.push({name: i.trim()}) | |
}) | |
value = values | |
}else if(type === 'number' || type === 'percent' || type === 'currency'){ | |
value = parseFloat(value) | |
}else if(type === 'multipleRecordLinks'){ | |
let items = value.split(',') | |
let values = [] | |
items.forEach( i => { | |
values.push({id: i.trim()}) | |
}) | |
value = values | |
}else if(type === 'singleRecordLink'){ | |
value = {id:value} | |
}else if(type === 'multipleAttachments'){ | |
let items = value.split(',') | |
let values = [] | |
items.forEach( i => { | |
values.push({url: i.trim()}) | |
}) | |
value = values | |
}else if(type === 'checkbox'){ | |
value = value === 1 || value === true || value === 'true' || value === '1' || value === 'yes' || value === 'on' | |
}else if(type === 'barcode'){ | |
value = {text: value} | |
}else if(type === 'rating' || type === 'duration'){ | |
value = parseInt(value) | |
}else if(type === 'singleCollaborator'){ | |
value = {id: getCollabId(value)} | |
}else if(type === 'multipleCollaborators'){ | |
let items = value.split(',') | |
let values = [] | |
items.forEach( i => { | |
values.push({id: getCollabId(i)}) | |
}) | |
value = values | |
}else if(type === 'date' || type === 'dateTime'){ | |
value = value.trim().toLowerCase() === 'now' ? new Date().toISOString() : value | |
} | |
return value | |
} | |
/** | |
* Function: toButtons | |
* Description: Create buttons based on an array of items (ie tables, views, or fields) | |
*/ | |
const toButtons = ( label, items, skip = [], buttonsStart = [], buttonsEnd = [] ) => { | |
let buttons = [] | |
for(let i=0; i<items.length; i++){ | |
let item = items[i] | |
if(!skip.includes(item.name)){ | |
buttons.push( item.name ) | |
} | |
} | |
buttons = [...buttonsStart,...buttons,...buttonsEnd] | |
return input.buttons(label, buttons) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment