jjones312
jjones312

Reputation: 695

Google Apps Script - .getActiveUser() not returning user access form

Expanding on project I'm working on and I'm trying to get the user name and email of the person submitting a Google Form. In order to log the name and email into a spreadsheet for use in a workflow. NOTE: This is a Google Apps for Business domain. The form has the following settings: 1. Require domain login to view this form. 2. Automatically collect respondent's domain username. (even that still logs just my name).

Code used to get user info. It works if I'm submitting the form.

//Grabs the Requestor's email and return it.
function giveEmail() {
  var eMail = Session.getActiveUser().getEmail();  
  return eMail;
}

//Gets the Requestor's First & Last Name and returns as whole name
function getRequestor() {
  var userFirst = UserManager.getUser(Session.getActiveUser()).getGivenName();
  var userLast = UserManager.getUser(Session.getActiveUser()).getFamilyName();
  var wholeName = userFirst +" "+ userLast;  
  return wholeName;
}

Based on the following link: Authorization for Google Services

The section "Permissions and types of scripts" indicates that "Forms" script runs as "user at the keyboard".

When I have others in the domain access the form and submit, it logs my information. The script that is attached to the form (from within edit of form Tools > Script Editor) runs via a trigger. Trigger runs function, from form, on form submit.

Am I miss understanding how this works? Can I actually get the user info for other in the domain who are accessing for the form?

Upvotes: 0

Views: 3296

Answers (1)

Serge insas
Serge insas

Reputation: 46794

Session.getActiveUser() is not the same as getRespondentEmail() that is a method of the FormResponse class.

You should use the latter to get the email of the user that answered the form. The activeUser is actually the person that created the trigger.

Upvotes: 1

Related Questions