S
S
Sergey Sokolov2017-11-17 14:47:45
Google Apps Script
Sergey Sokolov, 2017-11-17 14:47:45

How in Google Apps Script to distinguish the launch of a function by the trigger of your own and someone else's account?

Google Spreadsheet is edited by multiple users. On the edit event, under certain conditions, you need to send a letter MailApp.sendEmail () on behalf of the account that made the edit.
To do this, as I understand it, it is required, firstly, that the trigger is not simple, but an installable trigger - at the same time, permission is given to send letters from your account.
Secondly, it will be necessary to install such a trigger for each account on whose behalf it is possible to send a letter. Because such a trigger is executed on behalf of the account that installed it, and accordingly. the letter leaves on behalf of this account.
Now, with several users, the trigger will be executed on each edit several times .times: on behalf of each of those who set the trigger.
Question: how to determine in the called function that the user whose trigger called it and the user who made the edit are the same?
There was an idea to save the UniqueId of a trigger installed to itself - in Properties. Only I haven’t found yet how to determine when calling a function what kind of trigger it was called in order to catch exactly “your own”. I tried with three accounts - ActiveUser shows the emails of each of those who set the trigger. It remains to catch "yourself".

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Samokhin, 2017-11-17
@sergiks

The triggered Id can be obtained via e.triggerUid .

A
Alexander Ivanov, 2017-11-17
@oshliaer

You may have already read the article " Efficient User vs Active User " which makes your question rhetorical. Unless, of course, you have a gmail account, and not G Suite.
Confirming your words, you need to create one standalone script. Allow each user to register a trigger - each to himself. With this setting, during the execution of the script, for each owner of the script, Active User will have his email address, otherwise it will be empty.
But there is a pitfall here. It doesn't always work. At some point, Google revealed to owners (not G Suite) the addresses of Effective Users. It was unpleasant. Imagine I give you link access, you open a spreadsheet, and my onOpen trigger already knows who you are (well, at least your email address). What was to be done - cut off and not give.
How can you solve your problem? There are no effective methods for solving this problem. They are either bulky or too cloudy to last long.
I used the mail monitor for a while, but the user needs to subscribe to changes in the spreadsheet. Maybe in the new API there is an opportunity to sign the user automatically?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question