0
votes

I'm building a form for students to enter in and I want to Auto input their email address and I'm unsure how to do this. I'm come up with the following but I'm very new to this and may be way off. I'm unsure how to implement it and I'm getting a null response. Do I need a form question which says email ? how do I get the user's email to be recorded but in a hidden way?

function formEnterEmail(e) {
  var userEmail = Session.getActiveUser().getEmail();
  var sheet = SpreadsheetApp.getActiveSheet();
  var lastRow = sheet.getLastRow();
  // Set the status of the new ticket to 'New'.
  // Column F is the Status column
  sheet.getRange(lastRow, getColIndexByName("Email")).setValue(userEmail);

function getColIndexByName(colName) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var numColumns = sheet.getLastColumn();
  var row = sheet.getRange(1, 1, 1, numColumns).getValues();
  for (i in row[0]) {
    var name = row[0][i];
    if (name == colName) {
      return parseInt(i) + 1;
    }
  }
  return -1;
}
}
2

2 Answers

0
votes

If you are building a form that you will be emailing to the students, then when you set up the form you can have it log all their email addresses for you without you having to write any script at all.

Here is a great way to get started writing a new form... go to this page: http://www.google.com/drive/apps.html?usp=ad_search

and scroll down to the "Forms" section then click on "Create".

This should walk you through everything you will need to set up the form, and the results will be put into a spreadsheet for you, along with the students' email addresses. You can keep the resulting spreadsheet private so that you will be the only one who can view all the responses.

I hope that helps.

0
votes

You can collect the user's email address in Google Forms only if you are using Google Apps for Business or Google Apps for Education, and then only from users in your domain. If you don't meet those requirements, then your only option is to request that users fill in the information, or to email a URL to a pre-filled-form.

This isn't really "hidden", as the live form will report that the user id is being collected.

If you're creating your form from a script, you can control the email collection feature by using Form.setCollectEmail(true). However, you can also set this up without using any code.

In the form editor, look for "Form Settings". They should appear above all the questions. Set the check boxes that require domain login, and username collection.

screenshot

The spreadsheet that collects responses will automatically contain a "Username" column:

another screenshot