创建多个Google表格PDF会引发429错误 [英] Creating Multiple Google Sheets PDFs throws 429 error
问题描述
我被困住了.我希望有人能找到一种方法,可以从 Google表格中生成一堆 PDF .
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.
我为公司列表运行相同的报告,并为每个报告生成PDF快照,并将其保存在云端硬盘中.我每次都会收到一个 429错误,唯一的区别是在得到错误之前,我生成了多少 PDF .
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.
生成的PDF的数量与我放入Utilities.sleep
函数的时间直接相关,但仍仅占报告总数的一小部分(即,更多的睡眠=之前生成的 PDF 稍微多一些)错误).
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.
感谢您对如何避免以下429条的见解:
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
以下是相关代码:
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);
}
推荐答案
429错误表示请求过多.显而易见的解决方案是使用 sleep().在 SO帖子中对此进行了演示.
429 error means too many requests. The obvious solution then is to slow down on the request by creating a time delay between calls using sleep(). This has been demoed in this SO post.
这篇关于创建多个Google表格PDF会引发429错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!