Tony
Tony

Reputation: 115

Google forms appscript email sends only one form value

I tried creating an appscript to send an email.

Form Url: https://docs.google.com/forms/d/1kpH01RQRshKw_CEAgs9aM8AjNyrvBKDhJYC856BwO6M/edit?pli=1#settings

Appscript i'm trying as below

function onSubmit(e){
  var items = e.response.getItemResponses();
  var reporter = e.response.getRespondentEmail();

  var message = '';
  for (var i =0; i<=items.length;i++) {
    var items = items[i];
    var question = items.getItem().getTitle();
    var answer = items.getResponse();
    message += (i + 1).toString() + '. ' + question + ': ' + answer + '\n';
}
 FormApp.getActiveForm();

  var email = "[email protected]"; // it is working untill here and can send email
  var title = 'Event RSP';
  var content = 'This is a RSVP Request\n\n' + message;
  MailApp.sendEmail(email, title, content,{cc:reporter});


}

However, my email is triggering only 1 resultenter image description here

I need all the options in the form to be sent in email. Could you help me on what I'm doing wrong?

Upvotes: 0

Views: 109

Answers (1)

Giselle Valladares
Giselle Valladares

Reputation: 2261

You are not iterating inside all the questions inside the form, I created a simplified sample, you can test it out and apply the changes to your code or use the sample.

I created the code based on the Class FormResponse and the Class GmailApp.

Here is the code:

function rsvp_sender() {
  var form = FormApp.getActiveForm();
  var formResponses = form.getResponses();

  // Target the last submission of the Google Form
  var i = formResponses.length-1;

  var formResponse = formResponses[i];
  var itemResponses = formResponse.getItemResponses();

  //Empty list to add all the iterations of all the questions
  // inside the form. 
  let questions = [];

    // Starts a for loop to get all the questions 
    // and responses in the loop and their answers. 
    for (var j = 0; j < itemResponses.length; j++) {
      var itemResponse = itemResponses[j];
      
      // Add the information of the questions 
      // and responses to the empty list.
      questions += (i + 1).toString() + '. The answer to the question "' 
      + itemResponse.getItem().getTitle() + '" was "' + itemResponse.getResponse() 
      + '"\n'
    }

    // completes the body of the email 
  content =  'This is a RSVP Request\n\n'+ questions
  GmailApp.sendEmail('[email protected]','Event RSP', content);
}

And the email looks like this:

enter image description here

For this case, I manually added the trigger, Like this:

enter image description here

I tested filling out the form with multiple users (inside of workspace and Gmail.com,) and the email was sent without issues. Even if the other users didn’t have the trigger installed.


Update to hide the Responses Number (The 16):

To remove the number for 16. The answer to the question... you need to change:

 questions += (i + 1).toString() + '. The answer to the question "' 
      + itemResponse.getItem().getTitle() + '" was "' + itemResponse.getResponse() 
      + '"\n'

for:

 questions += 'The answer to the question "' 
      + itemResponse.getItem().getTitle() + '" was "' + itemResponse.getResponse() 
      + '"\n'

Update to number the questions:

Replace:

questions += (i + 1).toString() + '. The answer to the question "' 
      + itemResponse.getItem().getTitle() + '" was "' + itemResponse.getResponse() 
      + '"\n'

for:

questions += (j + 1).toString() + '. The answer to the question "' 
      + itemResponse.getItem().getTitle() + '" was "' + itemResponse.getResponse() 
      + '"\n'

Upvotes: 1

Related Questions