Jul 12, 2022 12:03 AM
Hi all,
I’d appreciate some guidance on how can I change the filenames of my attachments. I have a table with 500 records. Every record has a name and multiple images (attachments).
The attachments have identical file names in multiple records, e.g I might have a file called 1.jpg in one record, then a different attachment in a different record with the same name of1.jpg.
I managed to find a script that does that, but it changes the name only for one attachment. Ideally, would be to create a rule that adds to the current filename, the name from another field.
e.g: Gorge 1 + current filename.jpg
// Setup
const mainTable = base.getTable("Test")
const mainQuery = await mainTable.selectRecordsAsync()
// Build new attachment data
let updates = []
for (let record of mainQuery.records) {
let attachments = record.getCellValue("images")
if (attachments.length) {
updates.push({
id: record.id,
fields: {
"images": [{
url: attachments[0].url,
filename: record.getCellValue("Name")
}]
}
})
}
}
// Update records
while (updates.length) {
await mainTable.updateRecordsAsync(updates.slice(0, 50))
updates = updates.slice(50)
}
Thank you in advance,
Victor
Solved! Go to Solution.
Jul 12, 2022 01:06 AM
Hi Victor, I’ve put a scripting extension together here for you to check out that should do what you’re looking for
And here’s the code:
// Setup start
const mainTable = base.getTable("Test")
const mainTable_imageFieldName = "images"
const mainTable_nameFieldName = "Name"
// Setup end
const mainQuery = await mainTable.selectRecordsAsync({
fields:[mainTable_nameFieldName, mainTable_imageFieldName]
})
// Build new attachment data
let updates = []
for (let record of mainQuery.records) {
let name = record.getCellValue(mainTable_nameFieldName)
let attachments = record.getCellValue(mainTable_imageFieldName)
if (attachments.length) {
for (let attachment of attachments){
let attachmentName = name + " - " + attachment.filename
updates.push({
id: record.id,
fields: {
[mainTable_imageFieldName]: [{
url: attachment.url,
filename: attachmentName
}]
}
})
}
}
}
// Update records
while (updates.length) {
await mainTable.updateRecordsAsync(updates.slice(0, 50))
updates = updates.slice(50)
}
Jul 12, 2022 01:06 AM
Hi Victor, I’ve put a scripting extension together here for you to check out that should do what you’re looking for
And here’s the code:
// Setup start
const mainTable = base.getTable("Test")
const mainTable_imageFieldName = "images"
const mainTable_nameFieldName = "Name"
// Setup end
const mainQuery = await mainTable.selectRecordsAsync({
fields:[mainTable_nameFieldName, mainTable_imageFieldName]
})
// Build new attachment data
let updates = []
for (let record of mainQuery.records) {
let name = record.getCellValue(mainTable_nameFieldName)
let attachments = record.getCellValue(mainTable_imageFieldName)
if (attachments.length) {
for (let attachment of attachments){
let attachmentName = name + " - " + attachment.filename
updates.push({
id: record.id,
fields: {
[mainTable_imageFieldName]: [{
url: attachment.url,
filename: attachmentName
}]
}
})
}
}
}
// Update records
while (updates.length) {
await mainTable.updateRecordsAsync(updates.slice(0, 50))
updates = updates.slice(50)
}
Jul 12, 2022 02:28 AM
Wow, thanks a lot, Adam! Didn’t know this community is so helpful! Have a great day!
May 23, 2023 10:18 PM
I used this code to apply to my case, but it failed at first time.
I asked ChatGPT to correct it and add some more details to it.
So if there are multiple attachments and if you want to add incrementing numbers to the files. please use the below code.
Even though i got help from ChatGPT, I want to give kudos for Adam for the initiative and the kindness.
// Setup start
const mainTable = base.getTable("Ratings");
const mainTable_imageFieldName = "Pictures";
const mainTable_nameFieldName = "Name";
// Setup end
// Retrieve records from the main table
const mainQuery = await mainTable.selectRecordsAsync({
fields: [mainTable_nameFieldName, mainTable_imageFieldName]
});
// Build new attachment data
let updates = [];
for (let record of mainQuery.records) {
let name = record.getCellValue(mainTable_nameFieldName);
let attachments = record.getCellValue(mainTable_imageFieldName);
// Check if attachments exist and are not null
if (attachments && attachments.length) {
// Create a new array to store the modified attachments
let modifiedAttachments = [];
for (let i = 0; i < attachments.length; i++) {
let attachment = attachments[i];
let attachmentName = `${name} - ${attachment.filename.substring(0, attachment.filename.lastIndexOf('.'))}-${i + 1}${attachment.filename.substring(attachment.filename.lastIndexOf('.'))}`;
// Create a new object for the modified attachment
let modifiedAttachment = {
url: attachment.url,
filename: attachmentName
};
// Add the modified attachment to the array
modifiedAttachments.push(modifiedAttachment);
}
// Add the modified attachments to the updates array
updates.push({
id: record.id,
fields: {
[mainTable_imageFieldName]: modifiedAttachments
}
});
}
}
// Update records
while (updates.length) {
await mainTable.updateRecordsAsync(updates.slice(0, 50));
updates = updates.slice(50);
}