创建多个 Google Sheets 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:
请求失败 docs.google.com/a/[redacted]/spreadsheets/d/[redacted]/... 返回代码 429.截断的服务器响应:<!DOCTYPE html><html lang="en"><head><meta name="description" content="Web 文字处理、演示文稿和电子表格"><meta name="viewport" c...(使用 muteHttpExceptions 选项检查完整响应代码>
相关代码如下:
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 Sheets PDF 会引发 429 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!