h8rt3rmin8r

url-to-attachment_airtable.js

Feb 2nd, 2021
815
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. // url-to-attachment_airtable.js
  3. //
  4. //    Convert a URL to Image Attachment on Airtable.com
  5. //
  6. //    Source: https://community.airtable.com/t/url-to-attachment/28569/3
  7. //
  8. //    Updated on 20200828 by h8rt3rmin8r
  9. //    Updated on 20210202 by h8rt3rmin8r
  10. //
  11.  
  12. //----------------------------------------------------------------------------
  13. // Set custom variables (THIS REQUIRES MANUAL CUSTOMIZATION)
  14. //
  15.  
  16. let submissionsTable = base.getTable('{{TABLE_NAME}}');
  17. let urlField = submissionsTable.getField('{{URL_FIELD}}');
  18. let attachmentField = submissionsTable.getField('{{ATTACHMENT_FIELD}}');
  19.  
  20. //----------------------------------------------------------------------------
  21. // Execute operations
  22. //
  23.  
  24. let submissionsQuery = await submissionsTable.selectRecordsAsync();
  25. let updates = [];
  26.  
  27. for (let record of submissionsQuery.records) {
  28.     let url = record.getCellValue(urlField);
  29.     let attachments = record.getCellValue(attachmentField);
  30.  
  31.     // If this record already has an attachment, skip it.
  32.     if (attachments !== null) {
  33.         continue;
  34.     }
  35.  
  36.     // Otherwise, attach the image at the URL.
  37.     updates.push({
  38.         id: record.id,
  39.         fields: {
  40.             [attachmentField.id]: [{url: url}]
  41.         }
  42.     });
  43. }
  44.  
  45. //----------------------------------------------------------------------------
  46. // Limit updating process to batches of 50 records at a time
  47. //
  48.  
  49. while (updates.length > 0) {
  50.     await submissionsTable.updateRecordsAsync(updates.slice(0, 50));
  51.     updates = updates.slice(50);
  52. }
  53.  
RAW Paste Data