Mike Johnson
Mike Johnson

Reputation: 11

Google apps script not looping

I am teacher (which means that I don't really know what I am doing) who is trying to make a routine documentation faster for our staff. I have created a google sheet that will embed information from the cell row onto a google doc. I would like to figure out a way that when I run the script it will run it for each row and collect all of it onto a pdf so I print data from multiple students all at once. Here is a link to my google sheets document that I am running this script.

Any ideas on how I could make this work? The script I currently have works for the first row but it will not continue to any additional rows.

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

Upvotes: 1

Views: 303

Answers (2)

Wicket
Wicket

Reputation: 38434

NOTE: The following referred code on this answer was introduced by the OP on revision 3.

The statement block after var row = data[i++]; is not part of the for-loop, the for- loop only include one statement.

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

While JavaScript is very flexible it could be helpful, specially for those who aren't aware of any particular JavaScript styles guideline, to use the same style as it's used on samples included on https://developers.google.com/apps-script

  1. Start statement blocks on the same line

    Example 1:

    function myFuncion(){
    
    }
    

    Example 2:

    for(var i = 0;i < data.length; i++){
    
    }
    
  2. Use line terminators ;

  3. etc.

Upvotes: 0

RichTea
RichTea

Reputation: 115

I'm pretty basic with loops, but shouldn't they be like so:

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

try that?

Upvotes: 1

Related Questions