Created
April 11, 2023 14:28
-
-
Save gregjotau/f7d00b4aea0151954bb9f151ed9c7c3d to your computer and use it in GitHub Desktop.
Bulk rename attachments 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
let settings = input.config({ | |
title: '📎⇣ Bulk attachment renamer', | |
description: 'rename all attachments in a table with the name based on multiple fields in the same record.', | |
items: [ | |
input.config.table('tableWithAttachment', { | |
label: 'table with attachments to be renamed', | |
description: '', | |
}), | |
input.config.field('attachmentField', { | |
label: 'attachment that will be renamed', | |
parentTable: 'tableWithAttachment', | |
}), | |
input.config.field('imageNamingField', { | |
label: 'field that will be used to name the attachment, make a formula field based on different fields', | |
parentTable: 'tableWithAttachment', | |
}), | |
] | |
}); | |
console.log('Settings:', settings); | |
// Converts settings selections to variables | |
const tableWithAttachment = settings.tableWithAttachment; | |
const imageFieldName = settings.attachmentField; | |
const imageNamingField = settings.imageNamingField | |
const mainQuery = await tableWithAttachment.selectRecordsAsync({ | |
fields: [imageNamingField, imageFieldName] | |
}) | |
console.log('Main query:', mainQuery); | |
// Build new attachment data | |
let updates = [] | |
for (let record of mainQuery.records) { | |
let name = record.getCellValue(imageNamingField) | |
if (!name) { | |
continue | |
} | |
name = name.trim() | |
console.log("name to use:") | |
console.log(name) | |
let attachments = record.getCellValue(imageFieldName) | |
let imageArray = new Array; | |
if (attachments) { | |
for (let attachment of attachments) { | |
let attachmentName = name + "." + attachment.filename.split(".").pop(); | |
imageArray.push({ | |
url: attachment.url, | |
filename: attachmentName | |
}) | |
} | |
} | |
updates.push({ | |
id: record.id, | |
fields: { | |
[imageFieldName.name]: imageArray | |
} | |
}) | |
} | |
console.log('Updates:', updates); | |
// Update records | |
while (updates.length) { | |
await tableWithAttachment.updateRecordsAsync(updates.slice(0, 50)) | |
console.log('Updated records:', updates.slice(0, 50)); | |
updates = updates.slice(50) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment