我是一位老师(这意味着我真的不知道自己在做什么),他正在努力为我们的员工更快地编写例行文档。我已经创建了一个Google表格,它将单元格行中的信息嵌入到Google文档中。我想找出一种方式,当我运行脚本时,它将针对每一行运行该脚本并将其全部收集到pdf中,因此我可以一次打印多个学生的数据。这是我正在运行此脚本的link to my google sheets文档。
关于如何进行这项工作的任何想法?我目前拥有的脚本适用于第一行,但不会继续到其他任何行。
var data = sheet.getRange(4,1,sheet.getLastRow(),
sheet.getLastColumn()).getValues();
//var data = sheet.getRange(4,1,1,sheet.getLastColumn()).getValues();
var i;
for (i = 0; i < data.length ; i++)
var row = data[i++];{
// setting up the temporary document
var docid = DriveApp.getFileById(templateid).makeCopy().getId();
var doc = DocumentApp.openById(docid);
var body = doc.getBody();
// I have removed the parts that gather data and the replace text.
ss.toast("Creating your ALE document");
Utilities.sleep(sleepINT);
var file = DriveApp.getFileById(doc.getId());
var newfolder = DriveApp.getFolderById("1oQ8evDj8dlHoDdX01DvZqjcIkSq31oen");
var oldfolder = DriveApp.getFolderById("1IzY9PiobBC-O87AxCkU32j2n2wUU1zUE");
newfolder.addFile(file);
oldfolder.removeFile(file);
ss.toast("PDF has been created!");
Utilities.sleep(sleepINT);
var usernamefordoctitle = sheet.getRange(4,1,1,1).getValues()
var name = doc.getName();
doc.setName(month + " ALE for " + studentName);
ss.toast("Document has been named");
Utilities.sleep(sleepINT);
doc.saveAndClose();
var pdffolder = DriveApp.getFolderById ("1iwBEUZpFwz_eaCVKemYozF5FA0_t0YGv");
var pdfFILE = DriveApp.getFileById(doc.getId()).getAs('application/pdf');
pdfFILE.setName(doc.getName() + ".pdf");
var theFile = DriveApp.createFile(pdfFILE);
pdffolder.addFile(theFile);
ss.toast("Document has been saved to Google Drive");
Utilities.sleep(sleepINT);
var pdfEMAIL = DriveApp.getFileById(doc.getId()).getAs('application/pdf').getBytes();
var message = "This is your" + month + "ALE documents for your AG student."
var emailTo = emailAddress;
var subject = "Your " + month + " ALE for " + studentName + " ";
var message = "The attached pdf document is your " + month + " monthly ALE document for " + studentName + ".";
MailApp.sendEmail(emailTo, subject, message, {attachments: pdfFILE});
ss.toast("Email has been sent");
}
Utilities.sleep(sleepINT);
ss.toast(" FINISHED!!!! ");
}
答案 0 :(得分:1)
我对循环很基本,但是它们不应该这样:
for (i = 0; i < data.length ; i++){ <--- the curly brace here.
尝试吗?
答案 1 :(得分:0)
注意:OP上revision 3引入了以下有关此答案的代码。
var row = data[i++];
之后的语句块不属于for循环,for循环仅包含一个语句。
for (i = 0; i < data.length ; i++)
var row = data[i++];
虽然JavaScript非常灵活,但对于那些不了解任何特定JavaScript样式指南的人来说,使用与https://developers.google.com/apps-script
中包含的示例相同的样式可能会有所帮助Start语句位于同一行上
示例1:
function myFuncion(){
}
示例2:
for(var i = 0;i < data.length; i++){
}
使用行终止符;
等。