Help

How do you Compress multiple fields using script

Solved
Jump to Solution
1509 2
cancel
Showing results for 
Search instead for 
Did you mean: 
Roughly42Cats
4 - Data Explorer
4 - Data Explorer

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

 

// Modify these to your own values.
let tinyPngApiKey = 'xxxxxxxxxx';
let airtableAttachmentSource = 'Instructor Sample Artwork #1';
let airtableAttachmentDestination = 'Optimized Artwork 1';
let airtableColumnToLog = 'Optimized Artwork 1';


// 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) {
let recordAttachmentUrl = record.getCellValue(airtableAttachmentSource)[0]['url'];

console.log('Compressing ' + record.getCellValue(airtableColumnToLog))
 
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).');

await table.updateRecordAsync(record.id, {
[airtableAttachmentDestination]: [{url: json['output']['url']}],
})
}
}
 
Please, let me know what I can do differently. I appreciate any help:)
1 Solution

Accepted Solutions
Jordan514
4 - Data Explorer
4 - Data Explorer

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);
}
}
}

See Solution in Thread

2 Replies 2
Jordan514
4 - Data Explorer
4 - Data Explorer

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);
}
}
}

Roughly42Cats
4 - Data Explorer
4 - Data Explorer

Thank you so much! This worked!