0
votes

I have created an application where I am collecting form responses from various users. I am getting responses with email id in responses spreadsheet. As I don't want to store data in spreadsheet so I am reading data trough responses. I am facing some challenges please guide.

Query 1

while using onFormSubmit(e) I am not able to read submitted form, given code is returning null:

var form = FormApp.getActiveForm();
Logger.log('usename:' + form.getId());  

error " Cannot call method "getId" of null." although if I hard coded value of formid var form = FormApp.openById('<<form_id_xyz>>'); then it is working fine and I can read responses as well. How can I get form responses for multiple users?

Query 2

getRespondentEmail(); is not working in my case. Even I use form id <<form_id_xyz>> and trying to get email id from responses which I have captured at the time of form submission form.setCollectEmail(true); I tried following code in onFormSubmit(e) function but dint get a result:

var formResponse=form.response;
Logger.log('email id of user: ' +  formResponses.getRespondentEmail());

and another way:

Logger.log('email id of user: ' +  form.getRespondentEmail());

and

Logger.log('email id of user: ' +  e.values[1]);

nothing works for me. Kindly guide.

2
if your apps script is created in the form itself, then only FormApp.getActiveForm(); will give you form. Whereas if you are apps script is in drive then FormApp.getActiveForm(); will return null. Check this for your first query. Can you post your code for your second query.Bharathi
ohk, my bad, I am creating forms dynamically from spreadsheet, so how can I capture form ID of submitted forms?usadhikari
Relevent code to capture email id: function onFormSubmit(e) { var form = FormApp.openById('1ZQkN76JbZEpnWG9D0GQZfSWD8YHlP5nbuqIcfJpC9a0'); //Reading responses Logger.log('email : ' + form.getRespondentEmail()); Logger.log('email : ' + formResponses.getRespondentEmail()); Logger.log('email : ' + e.values[0]); }usadhikari

2 Answers

1
votes

Query 1: Hope it's clear in my comment.

Query 2:

Sorry to say, I don't understand your second query problem completely.

However as per your requirement I am suggesting this code.

If you have created a form you should know the form id (I assume) so try this code.

var form=FormApp.openById('your form id here'); 
//this returns your form which you created//

var responses=form.getResponses();
/// this will give you all responses of your form as an array////

///iterate the array to get respondent email id///

for(var i = 0; i < responses.length; i++){
   Logger.log(responses[i].getRespondentEmail());
}
0
votes

I think it's important to note that at the present time the answer to your question is: You can get what they enter, but you cannot get their true verified Email Address. This is explained better in this question and one of the answers details some workarounds such as publishing form as a web script.

The accepted answer displays what email address the user has typed into the form. There is no authentication to this beyond it having an @ symbol thus a user could type [email protected] and it would be viewed in the forms results and scripts.

What's annoying is that google IS capturing the user's true email address because if settings are set to Allow One Response Per User, then the user is limited to one submission -- regardless of what they put as their email account. I'm not sure why Google won't provide a method to view the submitter's login email address since it has been disclosed to the user that this will be disclosed.

Microsoft Forms does capture this.