Created
July 11, 2022 11:49
-
-
Save hujuu/ae5fa8b4116bceadcbbb5ef8b7b72c13 to your computer and use it in GitHub Desktop.
Googleスプレッドシートの画像URLを一括保存
This file contains 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
/** | |
* A列に入力された画像URLリストを元に画像をダウンロードしてGoogle Driveに保存 | |
*/ | |
function downloadImages() { | |
// 作っておいた画像フォルダの情報を取得 | |
var folders = DriveApp.getFoldersByName("画像フォルダ"); | |
if(false === folders.hasNext()) { | |
return; | |
} | |
var folder = folders.next(); | |
// 現在のシートを取得 | |
var sheet = SpreadsheetApp.getActiveSheet(); | |
// 読み取る範囲を指定 | |
var range = sheet.getRange("A1:E"); | |
// 画像URLが入力されている最後の行数を取得 | |
var row = sheet.getLastRow(); | |
for (i = 1; i <= row; i++) { | |
// シートから1行ずつ画像URLを取得 | |
var url = range.getCell(i,1).getValue(); | |
var token = range.getCell(i,4).getValue(); | |
// 画像データを取得 | |
var response = UrlFetchApp.fetch(url); | |
var fileBlob = response.getBlob().setName("QR_" + token); | |
// 取得した画像をGoogle Driveにアップロード | |
folder.createFile(fileBlob); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment