How to sent an alert when somone open the Spreadsheet using Google Script -
situation:
i have spreadsheet 15 worksheets , 20 users logging in , modifying it.
i have following script should send email user email adress open spreadsheet.
script function needed:
- send email when user open spreadsheet.
problem:
session.geteffectiveuser() getting the email owner of script , not getting user email.
the session.geteffectiveuser() since july23 should re-autorization user. perform following note re-approve script other user script , google account
test case:
this script have trigger when open spreadhseet.
function loginshift(){ var ss = spreadsheetapp.getactivespreadsheet(); mailapp.sendemail("xxxxxx@gmail.com", "login shift" , "login username: " + session.geteffectiveuser()); }
- when open spreadsheet owner of script, same sent me email email address open spreadsheet.
- when open spreadsheet different user not owner of script, script run , send email using owner mailbox , not mailbox of user open spreadsheet.
i have other script session.geteffectiveuser() onedit , working fine. don't know why script not getting email address geteffectiveuser.
there features described in documentation:
...
installable triggers
...
- when spreadsheet or form editor opened. (note trigger not activate when user opens form respond, rather when editor opens form modify it.) unlike simple trigger onopen, installable trigger can act user installed trigger.
...
the behavior mention in question expected according documentation.
update
if have simple trigger (oninstall
, onopen
, onedit
) follows:
function onopen() { logger.log('getactiveuser(): ' + session.getactiveuser()); logger.log('geteffectiveuser(): ' + session.geteffectiveuser()); }
when executed owner of script, result follows:
getactiveuser(): owner_of_the_script geteffectiveuser(): owner_of_the_script
when executed another user (not owner of script), result follows:
getactiveuser(): geteffectiveuser(): user_activates_the_trigger
important: read limitations of simple triggers in documentation.
if have installable trigger follows , add function event onopen
of spreadsheet:
function myonopen() { logger.log('getactiveuser(): ' + session.getactiveuser()); logger.log('geteffectiveuser(): ' + session.geteffectiveuser()); }
when executed owner of script, result follows:
getactiveuser(): owner_of_the_script geteffectiveuser(): owner_of_the_script
when executed another user (not owner of script), result follows:
getactiveuser(): geteffectiveuser(): owner_of_the_script