Home > Back-end >  How to log users email in Google Apps Script
How to log users email in Google Apps Script

Time:08-02

I'm trying to make a script that logs the date and time, and a user's email address when a button is pressed in Google Sheets. I can get the date and time values printed but whenever the button is pushed it just returns "Logger" instead of an email address. I'm also struggling to figure out how to change which cell the value is returned in, as the sheet is designed to log many users. Here's what I have so far, any help is appreciated.

edit, I've gotten the script to insert the user's email into a specified cell. Now I'm just wondering how to make it so it inserts the date and email into the next available row.

function setDate() {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses 1").getRange("N2").setValue(new Date());
var email = Session.getActiveUser().getEmail();
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses 1").getRange("O2").setValue(email);
}

CodePudding user response:

Refer to getActiveUser()

If security policies do not allow access to the user's identity, User.getEmail() returns a blank string. [...] However, these restrictions generally do not apply if the developer runs the script themselves or belongs to the same Google Workspace domain as the user.

  • Related