Aug 14, 2023 04:36 PM
Hi Im trying to Compress multiple fields with one script, I tried searching for a solution but I haven't came across to anything.
I was able to find one but i'm only able to compress one field but not multiple fields
Solved! Go to Solution.
Aug 14, 2023 10:04 PM
ChatGPT?
Prompt: Im using this script to compress an image from a field in airtable and output the new image to a different field in airtable. I would like you to change it to do the same thing except it will input and output 6 fields.
// Modify these to your own values.
let tinyPngApiKey = 'xxxxxxxxxx';
let airtableAttachmentSources = ['Instructor Sample Artwork #1', 'Instructor Sample Artwork #2', 'Instructor Sample Artwork #3', 'Instructor Sample Artwork #4', 'Instructor Sample Artwork #5', 'Instructor Sample Artwork #6'];
let airtableAttachmentDestinations = ['Optimized Artwork 1', 'Optimized Artwork 2', 'Optimized Artwork 3', 'Optimized Artwork 4', 'Optimized Artwork 5', 'Optimized Artwork 6'];
let airtableColumnsToLog = ['Optimized Artwork 1', 'Optimized Artwork 2', 'Optimized Artwork 3', 'Optimized Artwork 4', 'Optimized Artwork 5', 'Optimized Artwork 6'];
// Don't change these unless you know what you're doing.
let table = base.getTable(cursor.activeTableId);
let view = table.getView(cursor.activeViewId);
let queryResult = await view.selectRecordsAsync();
for (let record of queryResult.records) {
for (let i = 0; i < airtableAttachmentSources.length; i++) {
let recordAttachmentUrl = record.getCellValue(airtableAttachmentSources[i])[0]['url'];
console.log('Compressing ' + record.getCellValue(airtableColumnsToLog[i]));
let request = await remoteFetchAsync('https://api.tinify.com/shrink', {
body: JSON.stringify({ 'source': { 'url': recordAttachmentUrl } }),
headers: {
Authorization: 'Basic ' + btoa('api:' + tinyPngApiKey),
'Content-Type': 'application/json'
},
method: 'POST'
});
const json = await request.json();
// Checks that the API didn't fail.
if (request.status == 201) {
let percentReduced = Math.round((1 - json.output.ratio) * 100);
let kbReduced = (json.input.size - json.output.size) / 1024;
console.log('Panda just saved you ' + percentReduced + '% (' + Math.round(kbReduced) + 'KB).');
let fieldUpdates = {};
fieldUpdates[airtableAttachmentDestinations[i]] = [{ url: json['output']['url'] }];
await table.updateRecordAsync(record.id, fieldUpdates);
}
}
}
Aug 14, 2023 10:04 PM
ChatGPT?
Prompt: Im using this script to compress an image from a field in airtable and output the new image to a different field in airtable. I would like you to change it to do the same thing except it will input and output 6 fields.
// Modify these to your own values.
let tinyPngApiKey = 'xxxxxxxxxx';
let airtableAttachmentSources = ['Instructor Sample Artwork #1', 'Instructor Sample Artwork #2', 'Instructor Sample Artwork #3', 'Instructor Sample Artwork #4', 'Instructor Sample Artwork #5', 'Instructor Sample Artwork #6'];
let airtableAttachmentDestinations = ['Optimized Artwork 1', 'Optimized Artwork 2', 'Optimized Artwork 3', 'Optimized Artwork 4', 'Optimized Artwork 5', 'Optimized Artwork 6'];
let airtableColumnsToLog = ['Optimized Artwork 1', 'Optimized Artwork 2', 'Optimized Artwork 3', 'Optimized Artwork 4', 'Optimized Artwork 5', 'Optimized Artwork 6'];
// Don't change these unless you know what you're doing.
let table = base.getTable(cursor.activeTableId);
let view = table.getView(cursor.activeViewId);
let queryResult = await view.selectRecordsAsync();
for (let record of queryResult.records) {
for (let i = 0; i < airtableAttachmentSources.length; i++) {
let recordAttachmentUrl = record.getCellValue(airtableAttachmentSources[i])[0]['url'];
console.log('Compressing ' + record.getCellValue(airtableColumnsToLog[i]));
let request = await remoteFetchAsync('https://api.tinify.com/shrink', {
body: JSON.stringify({ 'source': { 'url': recordAttachmentUrl } }),
headers: {
Authorization: 'Basic ' + btoa('api:' + tinyPngApiKey),
'Content-Type': 'application/json'
},
method: 'POST'
});
const json = await request.json();
// Checks that the API didn't fail.
if (request.status == 201) {
let percentReduced = Math.round((1 - json.output.ratio) * 100);
let kbReduced = (json.input.size - json.output.size) / 1024;
console.log('Panda just saved you ' + percentReduced + '% (' + Math.round(kbReduced) + 'KB).');
let fieldUpdates = {};
fieldUpdates[airtableAttachmentDestinations[i]] = [{ url: json['output']['url'] }];
await table.updateRecordAsync(record.id, fieldUpdates);
}
}
}
Aug 15, 2023 09:47 AM
Thank you so much! This worked!