I just want to preface this post with saying I have little to no experience with coding. I’ve been recently tasked to download several QR codes from a google sheets document that have been generated using this formula below. But because they are generated from a url I can’t seem to save them as images unless I screenshot them.
=IMAGE("https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=" & ENCODEURL(B3))
I’ve been searching around the internet to try and find a script to workaround this and I found this script below from another thread that also wanted to save QR codes from a generated url in google sheets:
function myFunction() {
var sheet = SpreadsheetApp.getActive().getActiveSheet();
var data = sheet.getRange("C3:C121" + sheet.getLastRow()).getValues().flat();
for (var i = 0; i < data.length; i++){
var url = "https://api.qrserver.com/v1/create-qr-code/?size=150x150&data="+encodeURIComponent(data[i]);
var blob = UrlFetchApp.fetch(url).getBlob();
DriveApp.createFile(blob).setName(data[i]);
}
}
I adjusted the ranges accordingly to my own document but I’m not sure why it doesn’t work. It just keeps loading forever.
I’ve also tried this formula from ChatGPT:
function saveExistingQRCodes() {
var spreadsheetId = " "; // my spreadsheet ID
var sheetName = " "; // my spreadsheet name
var folderId = " "; // the folder I want to save the QR codes to
var ss = SpreadsheetApp.openById(spreadsheetId);
var sheet = ss.getSheetByName(sheetName);
if (!sheet) {
Logger.log(`Sheet with name "${sheetName}" not found.`);
return;
}
var data = sheet.getRange("C3:C121").getValues().flat();
var folder = DriveApp.getFolderById(folderId);
Logger.log(`Folder with ID "${folderId}" accessed successfully.`);
for (var i = 0; i < data.length; i++) {
var url = data[i];
if (url && url.startsWith('http')) {
try {
var blob = UrlFetchApp.fetch(url).getBlob();
var fileName = "NANW" + String(i + 2).padStart(5, '0') + ".png";
folder.createFile(blob).setName(fileName);
Logger.log(`Saved QR code from URL in row ${i + 3} as ${fileName}`);
} catch (e) {
Logger.log(`Failed to fetch QR code from ${url}: ${e.message}`);
}
} else {
Logger.log(`Row ${i + 3} does not contain a valid QR code URL.`);
}
}
}
But it also has an error: TypeError: url.startsWith is not a function
So I’m stumped and I’m hoping someone can help me!
Thank you in advance!
Kelley is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.