Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submitting a Google Form via Google Apps Script fails if Form is set to collect email

When submitting a form via Apps script, if the form is set to collect the user e-mail for responses, then the code fails with error:

"Sorry, the form response could not be submitted. Please wait a few minutes and try again."

I think Google may have changed something as the code that worked last year is not working this year.

The error points at the "submit" line in the code. OR sometimes, the code runs, but still nothing appears in the sheet, or in the "responses" in the form itself.

If I turn off the option to collect e-mail, it runs fine and I see the submission in the Google sheet, just without an e-mail address.

I setup a test form, attached it to a new google sheet for responses, and pasted in my code:

function codeVoteDoVoteByForm()
{
  
  // Get the Yes/No Google form
  var myForm = FormApp.openById('File ID Here')

  // Get a list of questions on it
  var questions = myForm.getItems()
 
  // Get question 0 (only one on this form) and mark it as multiple choice
  var qt = questions[0].asMultipleChoiceItem()
  
  // Set the users vote
  var qr = qt.createResponse("I am here")
  
  //Create the response
  var FormResponse = myForm.createResponse()
  var testemail = FormResponse.getRespondentEmail()
  // Submit the response
  FormResponse.withItemResponse( qr );
  var myResponse = FormResponse.submit()
  
  var responseString = myResponse.getTimestamp()
  return "Vote recorded at " + responseString
  
}

My thought is that Google changed something, so now when running a script it's not able to get the users e-mail address for the formresponse, but I can't find any documentation to confirm this.

Any thoughts?

like image 387
Lee Schiebel Avatar asked Apr 30 '18 19:04

Lee Schiebel


Video Answer


2 Answers

I think that the problem is when you are sending a reponse to a form that have collect email or authenticated user restrictions, without adding an email or authenticating the user. Unfortunely, I think that is not possible to send this sort of information with the API, so in my case:

  • I have disabled the restrictions, next
  • I have sent the response to the form, and finally
  • I enable the restrictions again

    //disable restrictions temporaly
    form.setLimitOneResponsePerUser(false);
    form.setRequireLogin(false);
    form.setCollectEmail(false);
    
    var questions = form.getItems();
    
    var formResponse = form.createResponse();
    
    for (var i=0; i<questions.length; i++){
    
      var question = questions[i].asMultipleChoiceItem();
      var response = question.createResponse(correctAnswers[i]);
    
      formResponse.withItemResponse(response);
    }
    
    formResponse.submit();
    
    form.setLimitOneResponsePerUser(true);
    form.setRequireLogin(true);
    form.setCollectEmail(true); 
    
like image 54
Xavi Torrens Avatar answered Oct 21 '22 01:10

Xavi Torrens


This function will first check the question type:

function autoFormResponse_(formId) {
  var form = FormApp.openById(formId);  
  //disable restrictions temporaly
  form.setCollectEmail(false);
  // answer questions
  var questions = form.getItems();  
  var formResponse = form.createResponse(); 
  var required_list = [];
  for (var i=0; i<questions.length; i++){            

    // release required questions
    var question = getQuestionItemAs_(questions[i]);
    if (question.isRequired())
    {
      question.setRequired(false);
      required_list.push(question);
    }   
  }  
  // submit
  formResponse.submit();   
  // restore required questions
  form.setCollectEmail(true);
  for (var i = 0; i < required_list.length; i++)
  {
    required_list[i].setRequired(true);
  }

  // return last response
  var responses = form.getResponses();
  return responses[responses.length - 1];
}

Helper function:

function getQuestionItemAs_(item)
{
  var type = '' + item.getType();    
  switch (type) {
    // https://developers.google.com/apps-script/reference/forms/item-type
    case 'CHECKBOX':         return item.asCheckboxItem();
    case 'CHECKBOX_GRID':    return item.asCheckboxGridItem();
    case 'DATE':             return item.asDateItem();
    case 'DATETIME':         return item.asDateTimeItem();
    case 'DURATION':         return item.asDurationItem();
    case 'GRID':             return item.asGridItem();
    case 'IMAGE':            return item.asImageItem();
    case 'LIST':             return item.asListItem();
    case 'MULTIPLE_CHOICE':  return item.asMultipleChoiceItem();
    case 'PAGE_BREAK':       return item.asPageBreakItem();
    case 'PARAGRAPH_TEXT':   return item.asParagraphTextItem();
    case 'SCALE':            return item.asScaleItem();
    case 'SECTION_HEADER':   return item.asSectionHeaderItem();
    case 'TEXT':             return item.asTextItem();
    case 'TIME':             return item.asTimeItem();
    case 'VIDEO':            return item.asVideoItem();
    default:                 return false;  
  }  
}
like image 2
Max Makhrov Avatar answered Oct 21 '22 01:10

Max Makhrov