{"id":786,"date":"2024-03-20T11:54:12","date_gmt":"2024-03-20T11:54:12","guid":{"rendered":"https:\/\/www.alexandrewsdesign.co.uk\/wordpress\/?p=786"},"modified":"2024-03-20T12:27:39","modified_gmt":"2024-03-20T12:27:39","slug":"google-app-script-image-downloader","status":"publish","type":"post","link":"https:\/\/www.alexandrewsdesign.co.uk\/wordpress\/?p=786","title":{"rendered":"Google App Script: Image Downloader"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.alexandrewsdesign.co.uk\/wordpress\/wp-content\/uploads\/2024\/03\/garden-copy.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"150\" src=\"https:\/\/www.alexandrewsdesign.co.uk\/wordpress\/wp-content\/uploads\/2024\/03\/garden-copy.jpg\" alt=\"\" class=\"wp-image-792\" srcset=\"https:\/\/www.alexandrewsdesign.co.uk\/wordpress\/wp-content\/uploads\/2024\/03\/garden-copy.jpg 800w, https:\/\/www.alexandrewsdesign.co.uk\/wordpress\/wp-content\/uploads\/2024\/03\/garden-copy-300x56.jpg 300w, https:\/\/www.alexandrewsdesign.co.uk\/wordpress\/wp-content\/uploads\/2024\/03\/garden-copy-768x144.jpg 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/a><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Introduction<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Purpose<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Script<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>function downloadImages() {\n  \/\/ Get the active spreadsheet\n  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();\n  var sheet = spreadsheet.getSheetByName(\"URLDownload\"); \/\/ Use the sheet named \"URLDownload\"\n  \n  \/\/ Get the range of URLs in column C\n  var urls = sheet.getRange(\"C:C\").getValues();\n  \n  \/\/ Create a folder in Google Drive to store images\n  var folderName = \"DownloadedImages\";\n  var folder;\n  var existingFolders = DriveApp.getFoldersByName(folderName);\n  if (existingFolders.hasNext()) {\n    folder = existingFolders.next();\n  } else {\n    folder = DriveApp.createFolder(folderName);\n  }\n  \n  \/\/ Loop through each URL and download the image\n  urls.forEach(function(url) {\n    if (url&#91;0] !== \"\") {\n      try {\n        var response = UrlFetchApp.fetch(url&#91;0]);\n        var blob = response.getBlob();\n        var fileName = url&#91;0].substring(url&#91;0].lastIndexOf(\"\/\") + 1); \/\/ Get the file name from the URL\n        folder.createFile(blob.setName(fileName));\n        Logger.log(\"Downloaded: \" + fileName);\n      } catch (e) {\n        Logger.log(\"Error downloading: \" + url&#91;0] + \", Error: \" + e);\n      }\n    }\n  });\n  \n  Logger.log(\"Images downloaded and moved to folder: \" + folder.getName());\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">How it Works<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Initialization<\/strong>: The script begins by getting the active spreadsheet and identifying the specific sheet named &#8220;URLDownload&#8221; where the URLs are stored.<\/li>\n\n\n\n<li><strong>URL Retrieval<\/strong>: It then retrieves the URLs listed in column C of the &#8220;URLDownload&#8221; sheet.<\/li>\n\n\n\n<li><strong>Folder Creation<\/strong>: Next, the script checks if a folder named &#8220;DownloadedImages&#8221; already exists in Google Drive. If it does, it uses the existing folder; otherwise, it creates a new folder to store the downloaded images.<\/li>\n\n\n\n<li><strong>Image Downloading<\/strong>: The script iterates through each URL retrieved from the spreadsheet. For each URL:\n<ul class=\"wp-block-list\">\n<li>It attempts to fetch the image using the <code>UrlFetchApp.fetch()<\/code> method.<\/li>\n\n\n\n<li>If successful, it creates a blob object representing the image data.<\/li>\n\n\n\n<li>It extracts the filename from the URL and creates a corresponding file in the designated Google Drive folder using the <code>folder.createFile(blob.setName(fileName))<\/code> method.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Logging<\/strong>: Throughout the process, the script logs information using the <code>Logger.log()<\/code> method. It records the status of each download operation, including successful downloads and any encountered errors.<\/li>\n\n\n\n<li><strong>Completion<\/strong>: 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.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Usage<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To utilize the Image Downloader script, follow these steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open the Google Sheets spreadsheet containing the URLs of the images you wish to download.<\/li>\n\n\n\n<li>Ensure that the URLs are listed in column C of the sheet named &#8220;URLDownload.&#8221;<\/li>\n\n\n\n<li>Run the <code>downloadImages()<\/code> function within the script.<\/li>\n\n\n\n<li>Monitor the execution and review the logs to track the progress and status of each image download.<\/li>\n\n\n\n<li>Once the script completes execution, check the designated Google Drive folder (&#8220;DownloadedImages&#8221;) to access the downloaded images.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u2026 <a href=\"https:\/\/www.alexandrewsdesign.co.uk\/wordpress\/?p=786\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":792,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-786","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-portfolio-slug"],"_links":{"self":[{"href":"https:\/\/www.alexandrewsdesign.co.uk\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/786","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.alexandrewsdesign.co.uk\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.alexandrewsdesign.co.uk\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.alexandrewsdesign.co.uk\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.alexandrewsdesign.co.uk\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=786"}],"version-history":[{"count":4,"href":"https:\/\/www.alexandrewsdesign.co.uk\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/786\/revisions"}],"predecessor-version":[{"id":797,"href":"https:\/\/www.alexandrewsdesign.co.uk\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/786\/revisions\/797"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.alexandrewsdesign.co.uk\/wordpress\/index.php?rest_route=\/wp\/v2\/media\/792"}],"wp:attachment":[{"href":"https:\/\/www.alexandrewsdesign.co.uk\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.alexandrewsdesign.co.uk\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.alexandrewsdesign.co.uk\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}