Google App Script: Image Downloader
Introduction
The Image Downloader script is designed to automate the process of downloading images from URLs listed in a Google Sheets spreadsheet. This script utilizes Google Apps Script, a JavaScript-based platform that extends Google Workspace applications like Google Sheets, to fetch images from URLs and store them in a designated folder within Google Drive.
Purpose
The primary purpose of this script is to streamline the task of downloading multiple images from URLs listed in a Google Sheets spreadsheet. It eliminates the need for manual downloading and organizing of images by automating the process.
The Script
function downloadImages() {
// Get the active spreadsheet
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName("URLDownload"); // Use the sheet named "URLDownload"
// Get the range of URLs in column C
var urls = sheet.getRange("C:C").getValues();
// Create a folder in Google Drive to store images
var folderName = "DownloadedImages";
var folder;
var existingFolders = DriveApp.getFoldersByName(folderName);
if (existingFolders.hasNext()) {
folder = existingFolders.next();
} else {
folder = DriveApp.createFolder(folderName);
}
// Loop through each URL and download the image
urls.forEach(function(url) {
if (url[0] !== "") {
try {
var response = UrlFetchApp.fetch(url[0]);
var blob = response.getBlob();
var fileName = url[0].substring(url[0].lastIndexOf("/") + 1); // Get the file name from the URL
folder.createFile(blob.setName(fileName));
Logger.log("Downloaded: " + fileName);
} catch (e) {
Logger.log("Error downloading: " + url[0] + ", Error: " + e);
}
}
});
Logger.log("Images downloaded and moved to folder: " + folder.getName());
}
How it Works
- Initialization: The script begins by getting the active spreadsheet and identifying the specific sheet named “URLDownload” where the URLs are stored.
- URL Retrieval: It then retrieves the URLs listed in column C of the “URLDownload” sheet.
- Folder Creation: Next, the script checks if a folder named “DownloadedImages” already exists in Google Drive. If it does, it uses the existing folder; otherwise, it creates a new folder to store the downloaded images.
- Image Downloading: The script iterates through each URL retrieved from the spreadsheet. For each URL:
- It attempts to fetch the image using the
UrlFetchApp.fetch()method. - If successful, it creates a blob object representing the image data.
- It extracts the filename from the URL and creates a corresponding file in the designated Google Drive folder using the
folder.createFile(blob.setName(fileName))method.
- It attempts to fetch the image using the
- Logging: Throughout the process, the script logs information using the
Logger.log()method. It records the status of each download operation, including successful downloads and any encountered errors. - Completion: Once all images have been downloaded (or attempted to be downloaded), the script logs a message indicating that the images have been downloaded and moved to the designated folder.
Usage
To utilize the Image Downloader script, follow these steps:
- Open the Google Sheets spreadsheet containing the URLs of the images you wish to download.
- Ensure that the URLs are listed in column C of the sheet named “URLDownload.”
- Run the
downloadImages()function within the script. - Monitor the execution and review the logs to track the progress and status of each image download.
- Once the script completes execution, check the designated Google Drive folder (“DownloadedImages”) to access the downloaded images.
Conclusion
The Image Downloader script offers a convenient solution for automating the process of downloading images from URLs listed in a Google Sheets spreadsheet. By leveraging Google Apps Script and Google Drive, it simplifies the task of managing and organizing downloaded images, saving users time and effort.
