【发布时间】:2019-04-14 03:06:13
【问题描述】:
我正在尝试设置成绩簿,我想在具有各种 vlookup 的工作表中循环浏览学生列表以创建单独的评分表。然后我想为每个学生制作一个pdf。在大约 6 个学生之后,我一直在服务器请求上超时。错误消息报告“返回代码 429”。
我以为我可以使用它,但它只会为每个学生创建一个 pdf,其中包含第一个学生的信息和姓名。我尝试了在互联网上找到的各种代码 sn-ps,但由于请求数量的原因,这些代码也会超时。
function PrintReport() {
SpreadsheetApp.getActiveSpreadsheet().toast('Writing the reports.','Printing Reports');
var email = Session.getActiveUser().getEmail();
var TodayDate = new Date();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ClassName = ss.getName()
var url = "https://docs.google.com/spreadsheets/d/SS_ID/export?".replace("SS_ID", ss.getId());
var StudentList = ss.getSheetByName('Mark Sheet');
var StudentCount = StudentList.getRange("A2").getValue();
var SLData = StudentList.getRange(7,1,StudentCount).getValues();
var ReportSheet = ss.getSheetByName('Student Report');
var url_ext = 'exportFormat=pdf&format=pdf' // export as pdf / csv / xls / xlsx
+ '&size=letter' // paper size legal / letter / A4
+ '&portrait=false' // orientation, false for landscape
+ '&fitw=true&source=labnol' // fit to page width, false for actual size
+ '&sheetnames=False&printtitle=True' // hide optional headers and footers
+ '&pagenumbers=false&gridlines=false' // hide page numbers and gridlines
+ '&fzr=false' // do not repeat row headers (frozen rows) on each page
+ '&gid='; // the sheet's Id
var token = ScriptApp.getOAuthToken();
var blobs = [];
var sheetID = ss.getSheetByName('Student Report').getSheetId();
var url_base = ss.getUrl().replace(/edit/,'');
for (var i = 0; i < StudentCount; i++){
ReportSheet.setActiveSelection("B1").setValue(SLData[i][0].toString());
SpreadsheetApp.flush();
var response = UrlFetchApp.fetch(url + url_ext + sheetID, {
headers: {'Authorization': 'Bearer ' + token
}
});
//convert the response to a blob and store in our array
blobs[i] = response.getBlob().setName(SLData[i][0] + '.pdf');
}
ReportSheet.setActiveSelection("B1").setValue(SLData[0][0]);
//create new blob that is a zip file containing our blob array
var zipBlob = Utilities.zip(blobs).setName(ClassName+': Student Reports'+'.zip');
var subject = ClassName+ ": Individual Reports";
var body = "How to print multiple files: Just select all the items you wish to print in Finder and tap Command-P (or choose File>Print). If your system can print those items, it will.";
// If allowed to send emails, send the email with the PDF attachment
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(email, subject, body, {
htmlBody: body,
attachments:[zipBlob]
});
SpreadsheetApp.getActiveSpreadsheet().toast('Reports are all done, please check you email.','Reports',5);
}
它应该创建一个带有学生姓名的 pdf 压缩文件。下载并解压 zip 文件后,pdf 文件中包含每个学生的标记。
如果我将“响应”代码移到循环之外,它会生成多个 PDF,其中只有一个学生的数据。
更新 修改为下面的代码,但现在它为每个学生制作了一个 pdf,但每个人都有最后一个学生的信息(标记和姓名)。
function PrintReport() {
SpreadsheetApp.getActiveSpreadsheet().toast('Writing the reports.','Printing Reports');
var email = Session.getActiveUser().getEmail();
var TodayDate = new Date();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ClassName = ss.getName();
var StudentList = ss.getSheetByName('Mark Sheet');
var StudentCount = StudentList.getRange("A2").getValue();
var SLData = StudentList.getRange(7,1,StudentCount).getValues();
var ReportSheet = ss.getSheetByName('Student Report');
var sheetID = ss.getSheetByName('Student Report').getSheetId();
var blobs = [];
hideAllSheetsExcept('Student Report');
for (var i = 0; i < StudentCount; i++){
ReportSheet.setActiveSelection("B1").setValue(SLData[i][0].toString());
SpreadsheetApp.flush();
//convert the response to a blob and store in array
blobs.push(ss.getBlob().getAs('application/pdf').setName(SLData[i][0] + '.pdf'));
//creates a pdf of the last student's information for everyone!
}
ReportSheet.setActiveSelection("B1").setValue(SLData[0][0]);
//create new blob that is a zip file containing our blob array
var zipBlob = Utilities.zip(blobs).setName(ClassName+': Student Reports'+'.zip');
var subject = ClassName+ ": Individual Reports";
var body = "How to print multiple files: Just select all the items you wish to print in Finder and tap Command-P (or choose File>Print). If your system can print those items, it will.";
// If allowed to send emails, send the email with the PDF attachment
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(email, subject, body, {
htmlBody: body,
attachments:[zipBlob]
});
SpreadsheetApp.getActiveSpreadsheet().toast('Reports are all done, please check you email.','Reports',5);
UnHideEverything();
}
【问题讨论】:
-
我认为在您的情况下,了解
StudentCount的价值对于思考您的解决方案很重要。那我能问一下StudentCount的价值吗? -
当然。 StudentCount 只是我通过使用表格中的公式生成的班级学生人数,该公式仅计算包含学生姓名的非空白单元格(即五个学生姓名 - StudentCount = 5)。 SLData 只是将所有学生的姓名保存在一个数组中。
-
所以你说只有五个学生就超时了?多长时间了?
-
它在大约 7 名学生后达到请求限制......我需要让它与 40 多个学生一起工作。限制响应非常快......我会说在几秒钟内,所以它没有达到 6 分钟的脚本运行时间限制。
-
@Mr. M. Perzan 感谢您的回复。请问“错误信息报告“返回代码429。”的错误发生在哪一行?
标签: pdf google-apps-script google-sheets-api