I just want to preface this post by saying I have little to no experience with coding. I’ve been recently tasked with downloading several QR codes from a Google Sheets document that have been generated using the 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.
From the question
I adjusted the ranges accordingly to my own document, but I’m not sure why it doesn’t work. It just keeps loading forever.
It’s not clear what you mean by “keeps loading forever.” If you mean that you are calling the Google Apps Script function in a cell formula and it shows Loading, remember that custom functions can’t execute methods like DriveApp.createFile because they require authorization to be executed. Another limitation to remember is that custom functions have a 30-second execution time limit, so it’s not a good idea to include a for
statement or another kind of loop to do a lot of slow tasks.
Regarding the error with url.startsWith
, it happens because the value assigned to url
is not a string.
Replace the expression
url && url.startsWith('http')
by
url && (typeof myVar === 'string' || myVar instanceof String) && url.startsWith('http')
Related
- Check if a variable is a string in JavaScript