-
-
Save nocodesupplyco/c1a8bf58b66cc98f0a5a1a1404b79b6d to your computer and use it in GitHub Desktop.
Get Image Size w/ Airtable Script
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
| // Define settings fields and descriptions | |
| const config = input.config({ | |
| title: "Get Image Size", | |
| description: | |
| "Get the size of images for each record in a specific view. Script created by Base Scripts — use at your own discretion.", | |
| items: [ | |
| input.config.table("tableSelect", { | |
| label: "Table", | |
| description: "Select the table where there are images to check.", | |
| }), | |
| input.config.view("viewSelect", { | |
| label: "View", | |
| description: "Select the view where there are images to check.", | |
| parentTable: "tableSelect", | |
| }), | |
| input.config.field("imageSelect", { | |
| label: "Image Field", | |
| description: "Select the attachment field that contains the images to check.", | |
| parentTable: "tableSelect", | |
| }), | |
| input.config.field("imageSizeSelect", { | |
| label: "Image Size Field", | |
| description: "Select the number field where the image size should be output.", | |
| parentTable: "tableSelect", | |
| }), | |
| ], | |
| }); | |
| // Use the actual objects from config (safer than names) | |
| const table = config.tableSelect; | |
| const view = config.viewSelect; | |
| const imageField = config.imageSelect; // attachment field | |
| const imageSizeField = config.imageSizeSelect; // number field | |
| // Pull records with just the needed field | |
| const query = await view.selectRecordsAsync({ fields: [imageField] }); | |
| // Helper: round to 2 decimals | |
| const round2 = (n) => Math.round(n * 100) / 100; | |
| let updates = []; | |
| for (const record of query.records) { | |
| const attachments = record.getCellValue(imageField); | |
| // Default: null (clears the size if no image) | |
| let kbValue = null; | |
| if (attachments && attachments.length > 0) { | |
| // FIRST image's size in KB (change to total if you prefer) | |
| const first = attachments[0]; | |
| // bytes → KB | |
| kbValue = round2(first.size / 1024); | |
| // If you want TOTAL size of all attachments instead, use: | |
| // const totalBytes = attachments.reduce((sum, att) => sum + (att.size ?? 0), 0); | |
| // kbValue = round2(totalBytes / 1024); | |
| } | |
| updates.push({ | |
| id: record.id, | |
| fields: { | |
| [imageSizeField.id]: kbValue, // use field ID, more reliable | |
| }, | |
| }); | |
| // Batch in chunks of 50 | |
| if (updates.length === 50) { | |
| await table.updateRecordsAsync(updates); | |
| updates = []; | |
| } | |
| } | |
| // Flush remaining updates | |
| if (updates.length) { | |
| await table.updateRecordsAsync(updates); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment