The Fat Oracle
The Fat Oracle

Reputation: 646

Google Apps Script - get/assign user unique identifier

I am developing a Google Docs Add-on and need to know if the user currently using the add-on has used it before in the current document or any document. I can't figure out which service gives me some kind of unique identifier for the current user that I can use to identify the current user.

The base service's User.getEmail is not good enough as it doesn't give access to the email under certain conditions and is not reliable.

Thoughts on best way to do this?

Upvotes: 0

Views: 692

Answers (3)

The Fat Oracle
The Fat Oracle

Reputation: 646

It looks like the best solution is to roll your own solution using the following to set the userId at an appropriate time:

PropertiesService.getUserProperties().setProperty("userId",<someGeneratedUId>)

then whenever you want to look up if this user is a recognizable user,

var userId = PropertiesServices.getUserProperties().getProperty("userId")

Looks like once set, the UserProperties thus set is available to this particular user (per user) running this add-on everywhere (i.e. all docs, not just the current on) that have this particular add-on enabled/running.

I'll validate this further but seems like this works in my tests.

Upvotes: 0

Vyacheslav Pukhanov
Vyacheslav Pukhanov

Reputation: 1938

Unfortunately, there is no such service. Google wants users' personal information to remain private, so AFAIK you won't usually get even user's email, unless your script is written for Google Apps domain and the user is in the same domain as the script's author.

The only thing you can do in the situation is use something like DocumentApp.getActiveDocument().getId() and check if the script was used in this document, but of course this won't give you an idea if user has used your script in other documents or not.

Also you can show a Prompt Dialog to user, asking him to enter email, but of course there is no way you can check if he entered his real email.

Upvotes: 1

St3ph
St3ph

Reputation: 2288

Unique user identifier you can get for apps script is Session.getEffectiveUser().getEmail()

This will return the email of user for which the script run. This is better than ActiveUser because if you use trigger EffectiveUser will well return the email of the user who install the script.

I use that for several apps to identify license assignment and it works great.

Upvotes: 0

Related Questions