简体   繁体   中英

Google Apps Script - how to send an email with the contents of an array?

I'm trying to write a script that will send an email every time a certain Google Form is filled in. I have everything working except for I can't figure out how to put the contents of an array that holds the titles and contents of questions & their responses for the most recent set of answers into the body of an email! I can't put code into the ".sendEmail" function like this:

  MailApp.sendEmail("YOURID@nyu.edu", 
                "The form has been updated", 
                for (var j = 0 ; j < array.length ; j++) {
                   array[j];
                } );

I can't figure out a way how to include the content! Here is the full script:

 function onEdit(e) {


   //get form by ID
   var form = FormApp.openById('ID OF YOUR FORM GOES HERE');

  //Get form Responses
  var formResponses = form.getResponses();

  var array = [];
  var formResponse = formResponses[formResponses.length-1];
  var itemResponses = formResponse.getItemResponses();
  for (var j = 0; j< itemResponses.length; j++){
    var itemResponse = itemResponses[j];
    array.push(itemResponse.getItem().getTitle() + ": " + itemResponse.getResponse());
  }




  MailApp.sendEmail("YOURID@nyu.edu", 
                "The form has been updated", 
                for (var j = 0 ; j < array.length ; j++) {
                   array[j];
                } );
}

You need to convert your array into a string. You may simply use the .join() method of Array , which joins all elements of an array into an string, using whichever separator you want. In your case, a simple newline \\n would work.

MailApp.sendEmail("YOURID@nyu.edu", 
            "The form has been updated", 
            array.join('\n'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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