Skip to content

Instantly share code, notes, and snippets.

@palumbo
Created June 9, 2022 23:55
Show Gist options
  • Save palumbo/d751841c3e61764f058fb60253bc47fe to your computer and use it in GitHub Desktop.
Save palumbo/d751841c3e61764f058fb60253bc47fe to your computer and use it in GitHub Desktop.
The code I used to download images into a Google Sheets cell or download images into a Google Drive folder.
function insertImage() {
let sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
let lastRow = sheet.getLastRow();
for (let i = 0; i < lastRow-1; i++) {
let url = sheet.getRange(2+i,1).getValue();
let image = SpreadsheetApp.newCellImage().setSourceUrl(url);
sheet.getRange(2+i,2).setValue(image);
}
}
function downloadImage() {
let sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
let lastRow = sheet.getLastRow();
let folder = DriveApp.getFolderById("1W4pv0RW7DB_yh8pqChc8YPNyq0ETQ_1m");
for (let i = 0; i < lastRow-1; i++) {
let url = sheet.getRange(2+i,1).getValue();
let blob = UrlFetchApp.fetch(url).getBlob();
folder.createFile(blob);
}
}
@itoldusoandso
Copy link

This works thank you.

  • Tried to add a trigger to run it when records change, but that was resulting in repeated downloads of the same images (maybe because I wanted to have images grouped in folders?)
  • I added a few tweaks like I don't want to re-download all the images if any of the record changes or new record is added.
  • I didn't want to have the download run on all records, just the ones I select at any point of time.
  • I wanted the images to be grouped in subfolders in google drive
    I am also skipping download images to google sheet, just straight to drive. (Google sheet field only accepts single attachment by the way, can't be an array seems).

No point to copy past here the updated code from GPT, everybody can do that.
But I wanted say thanks for your initial script this helped and it's amazing. That allows me to skip the Zapier because it's not always playing nicely along with google.

And this is free.

@palumbo
Copy link
Author

palumbo commented Jan 14, 2024

@itoldusoandso Happy to hear this code go you started and you were able to build a solution without using expensive 3rd party connections.

@Abbazabba305
Copy link

Thank you so much for the code. It has been a life saver. I'm only having one issue and it is the names of the files. Do you have any idea on how to have it save the images with the name of the link it used to pull the image from?

@itoldusoandso
Copy link

For me it downloads the file exactly as it is saved in Airtable. Airtable exports automatically to Google Sheets via automation by creating temp download links and then this GS Script downloads the files with the original file name. What do you mean save the name of the link. I doubt you can save file name with forward slash or http:// .

@DruFine
Copy link

DruFine commented Mar 13, 2025

@itoldusoandso The things that you tweaked the code for sound exactly like what I need. I'm working on an automation using Make that posts to Instagram from a Google Sheet that was populated by a form filled out on a Wix site. Would you mind sharing your tweaked code with me?

@palumbo Thanks for making the YouTube video and sharing the code!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment