3

In Google Spreadsheet I am having the following issue:

I am using Session.getActiveUser().getUserLoginId() to fetch the current user's email id but other than creator of the spreadsheet it is returning empty string. I have also used getEffectiveUser() but no help.

I have even deployed this to "User accessing the web app" but still it's not working.

How can I fix this?

5

Short answer

getUserLoginID is deprecated. Instead use getMail.

Explanation

From https://developers.google.com/apps-script/reference/base/user

getuserLoginID()

Deprecated. As of June 24, 2013, replaced by getEmail()

getEmail()

Gets the user's email address, if available. If security policies do not allow access to the user's email address, this method returns a blank string. The circumstances in which the email address is available vary: for example, the user's email address is not available in any context that allows a script to run without that user's authorization, like a simple onOpen(e) or onEdit(e) trigger, a custom function in Google Sheets, or a web app deployed to "execute as me" (that is, authorized by the developer instead of the user). However, these restrictions generally do not apply if the developer and the user belong to the same Google Apps for Business domain.

 // Log the email address of the person running the script.
 Logger.log(Session.getActiveUser().getEmail());
3

Wim den Herder posted a workaround based on the observation that a script cannot remove the current user from the list of editors of a protected range. They key part is as follows:

var protection = SpreadsheetApp.getActive().getRange("A1").protect();
protection.removeEditors(protection.getEditors());
var editors = protection.getEditors();
protection.remove();

The first line protects cell A1. The second line attempts to remove all editors of a protected range. Any editors that are left are the spreadsheet owner and the current user. The protection is then removed altogether.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.