Skip to content

Instantly share code, notes, and snippets.

@robotconscience
Last active June 7, 2024 15:29
Show Gist options
  • Save robotconscience/eb0b025910a06e6f06286881f31d607e to your computer and use it in GitHub Desktop.
Save robotconscience/eb0b025910a06e6f06286881f31d607e to your computer and use it in GitHub Desktop.
Import Met data into Airtable

Using

  • Click the "Extensions" tab
  • Add the "Scripting" extension
  • Copy pasta the above, and hit 'Run'
  • Mind the "Create" option — if you select this, it will add ~60 fields to your table
// Ask for an ObjectID
let objectID = await input.textAsync("Input an Object ID")
// get result from objectID
let apiURL = "https://collectionapi.metmuseum.org/public/collection/v1/objects/" + objectID
let results = await fetch(apiURL)
let json = await results.json()
if (json.hasOwnProperty("message")){
output.text("Error fetching: "+json.message)
} else {
// Edit this to the name of a table in your base
let table = await input.tableAsync("Select a table")
let createFields = await input.buttonsAsync(
`Create fields, or just populate existing?`,
[{label: 'Create', variant: 'primary'}, 'Populate existing']
)
let newRecord = {
fields: {
}
}
// Create fields from API — will include EVERYTHING
if ( createFields == "Create" ){
for (let key in json){
if (json.hasOwnProperty(key)){
let found = table.fields.find((el) => (el.name.toLowerCase() == key.toLowerCase()))
if (found == undefined){
try {
await table.createFieldAsync(key, "singleLineText")
output.text("Created field "+key)
} catch (e){
output.text("Error creating "+e)
}
} else {
output.text("Field exists: "+key)
}
}
}
}
// Populate fields that exist (i.e. already there, or created above, or both!)
table.fields.forEach((field) => {
if (json.hasOwnProperty(field.name)){
newRecord.fields[field.name] = (json[field.name].toString())
output.text("Populate "+field.name)
} else {
// output.text("Nope. "+field.name)
}
})
// Insert into table
await table.createRecordAsync(newRecord.fields);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment