I'm stuck. I'm hoping someone out there has figured out a way to generate a bunch of PDF's off of Google Sheets.
I run the same report for a list of companies and generate PDF snapshots of each report which are saved in Drive. I get a 429 error every time with the only difference being how many PDFs I generate before getting the error.
The number of PDFs generated is directly correlated to how long I put in the Utilities.sleep
function but still only a fraction of the total number of reports (i.e more sleep = slightly more PDFs generated before error).
I've made several attempts at using exponential backoff, but my attempts have been futile.
Any insights on how to avoid the following 429 are appreciated:
Request failed for docs.google.com/a/[redacted]/spreadsheets/d/[redacted]/… returned code 429. Truncated server response: <!DOCTYPE html><html lang="en"><head><meta name="description" content="Web word processing, presentations and spreadsheets"><meta name="viewport" c... (use muteHttpExceptions option to examine full response
Here's the relevant code:
var foldersave = DriveApp.createFolder('New Reports').getId()
var request = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken(), muteHttpExceptions: true}};
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var gid = sheet.getSheetId();
var values = ss.getActiveSheet().getRange(2, 8, sheet.getLastRow()-1, 1).getValues();
for (var i = 0; i < values.length; i++) {
//set up the new report
var org = sheet.getRange(5,4);
org.setValue(values[i])
//set the PDF parameters
var pdfOpts = '&size=0&fzr=false&portrait=true&fitw=true&gridlines=false&printtitle=false&sheetnames=false&pagenum=CENTER&attachment=false&gid='+gid;
var printRange = '&c1=0' + '&r1=0' + '&c2=4' + '&r2=42';
var url = ss.getUrl().replace(/edit$/, '') + 'export?format=pdf' + pdfOpts + printRange;
//create the PDF
var response = UrlFetchApp.fetch(url, request).getBlob().setName(values[i] + ".pdf");
var dir = DriveApp.getFolderById(foldersave);
var file = dir.createFile(response);
Utilities.sleep(5000);
}