繁体   English   中英

谷歌应用程序脚本不循环

[英]Google apps script not looping

我是老师(这意味着我真的不知道我在做什么),他正在努力为我们的员工更快地制作常规文档。 我创建了一个谷歌工作表,它将单元格行中的信息嵌入到谷歌文档中。 我想找出一种方法,当我运行脚本时,它会为每一行运行它并将所有内容收集到 pdf 中,这样我就可以一次打印多个学生的数据。 这是我正在运行此脚本的google 表格文档的链接

关于如何使这项工作有任何想法? 我目前拥有的脚本适用于第一行,但不会继续用于任何其他行。

 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!!!! 🎉");
}

我对循环非常基本,但它们不应该是这样的:

for (i = 0; i < data.length ; i++){ <--- the curly brace here.

试试看?

注意:此答案中的以下引用代码是由 OP 在修订版 3 中引入的。

var row = data[i++];后面的语句块不是 for 循环的一部分,for 循环只包含一个语句。

for (i = 0; i < data.length ; i++) 
    var row = data[i++];

虽然 JavaScript 非常灵活,但使用与https://developers.google.com/apps-script中包含的示例相同的样式可能会有所帮助,特别是对于那些不了解任何特定 JavaScript 样式指南的人

  1. 开始语句块在同一行

    示例 1:

     function myFuncion(){ }

    示例 2:

     for(var i = 0;i < data.length; i++){ }
  2. 使用行终止符;

  3. 等等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM