google-apps-scriptgoogle-forms

How can I submit a Google Form with Apps Script when the form is set to collect emails via Responder Input?


The script I am using iterates through the form questions, gets the item type, and builds a response based on the expected item response type. But the Email question presented to the end user in the form when it's set to Responder Input does not appear in the returned list of items when I call form.getItems(). So I don't know how to include the responder's email in the form response I send to the API.

function submitForm(){
  var form = FormApp.openById(formId);
  var items = form.getItems();

  //Iterating through the form items array and removing any PAGE_BREAKS
  for (var i = 0; i < items.length; i++){
    Logger.log(items[i].getType())
    Logger.log(items[i].getTitle())
    if(items[i].getType() == FormApp.ItemType.PAGE_BREAK){
      items.splice(i,1);
    }
  }

  var currentDateTime = Utilities.formatDate(new Date(), 'America/New_York', 'MM/dd/yyyy hh:mm:ss');

  var answers = [[`sample response`,`sample response`,[`sample1`,`sample2`],`sample response`,`sample response`,`sample response`,new Date(),`sample response`,`sample response`,`sample response`,`sample response`,`sample response`,new Date(),`sample response`,`sample response`,`sample response`,`sample response`,new Date(),`respondersEmail@gmail.com`]];

  //Iterating through the array of rows. 
  for (var i = 0; i < answers.length; i++) {
    var formResponse = form.createResponse();
    
    //Iterating through the form items array
    for(var j = 0; j < items.length; j++){

        var item = items[j];

        //Checking the item type and then formatting the associated response from the array of rows.
        switch (item.getType()) {

          case FormApp.ItemType.DATE:
            var itemResponse = item.asDateItem().createResponse(answers[i][j]);
            break;
        
          case FormApp.ItemType.TEXT:
            var itemResponse = item.asTextItem().createResponse(answers[i][j]);
            break;

          case FormApp.ItemType.MULTIPLE_CHOICE:
            var itemResponse = item.asMultipleChoiceItem().createResponse(answers[i][j]);
            break;

          case FormApp.ItemType.CHECKBOX:
            var itemResponse = item.asCheckboxItem().createResponse(answers[i][j]);
            break;

          case FormApp.ItemType.PARAGRAPH_TEXT:
            var itemResponse = item.asParagraphTextItem().createResponse(answers[i][j]);
            break;
          
          case FormApp.ItemType.LIST:
            var itemResponse = item.asListItem().createResponse(answers[i][j]);
            break;
        }
      
      //Adding the item response to the form response. 
      formResponse.withItemResponse(itemResponse);       
    }

    //Submitting the form response to the Google Form. 
    formResponse.submit();
  }
}

Solution

  • A form response created with code cannot include the respondent's email address. The Forms Services doesn't include a method to do that. Instead, add a question to hold the email address.


    To get the respondent email address from a form response submitted using the Google Forms responder viewer, use the getRespondentEmail() method of the Class FormResponse. The link includes a code example. Below is the same code without comments

    const form = FormApp.openById('abc123456');
    const formResponses = form.getResponses();
    for (const formResponse of formResponses) {
      console.log(`Respondent Email: ${formResponse.getRespondentEmail()}`);
    }
    

    Related