'Is it possible to set getRespondentEmail value in script-created response for google form?

Created a issue here and here for this. Consider adding a star(on the top left), if you'd like this issue resolved as well


I'm working on some qa stuff for our workflow.

As a part of it I want to automate submission of test forms to check if they processed correctly.

The thing is, the form is collecting emails, and I have no idea how to set them from script.

I've created a minimalistic(really!) example of form, which is collecting EMAIL ONLY.

https://docs.google.com/forms/d/1Ve12XWUxYH9U1cBPwAmgLbWdA8S3lnEkqgosTbqlOLg/edit?usp=sharing

The code I'm running is

function createDummyFormResponse() {
  var form = FormApp.openById('1Ve12XWUxYH9U1cBPwAmgLbWdA8S3lnEkqgosTbqlOLg');
  var response = form.createResponse();
  
  response.submit();      
}

Doesn't work with error

5:08:26 PM  Error Exception: Invalid data updating form.
            createDummyFormResponse@ qa.gs:45

Apparently I don't expect it to work, cause I haven't set respondent email anywhere. I thoroughly enough(I hope) studied Form and Response reference and I see nothing about setting respondent email in script-created form.

Is it possible at all and does anyone know how it can be done?



Solution 1:[1]

Currently it will use the users Email, but you can instead just add a text field called Email and then you can pre-populate it.

So if you want to fix it, turn off the Collect email addresses. Then the field Email will be removed. Add a new field called Email and type text. Read the answer here and then you should be able to prefill that field.

enter image description here

Solution 2:[2]

The original poster has created a feature request on Google issue tracker:

Created a issue here for this and here [closed].

Consider adding a star(on the top left), if you'd like this issue resolved as well

Solution 3:[3]

For now, the only workaround is as proposed by @Neven Appropriate code(disables email collecting and create similar Email Text input on top of form):

function disableEmailCollect(formId) {
    var form = FormApp.openById(formId);
    form.setCollectEmail(false);

    if(form.getItems()[0].getTitle() !== 'Email') { // Just to check we haven't done that already
      var validation = FormApp.createTextValidation().setHelpText('Please provide your contact email.').requireTextIsEmail().build();  // Init Email Validation
      var newItem = form.addTextItem().setTitle('Email').setValidation(validation).setRequired(true);
  
      form.moveItem(newItem.getIndex(),0); // Move to top
    }  
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Neven Subotic
Solution 2
Solution 3 roma