I would like to better clarify the question I have written. I am attempting to write an add-on that does the following:
When this add-on is installed in a document, I want it to send the user an email every time the document is opened, with the information of who opened the document. I have much more detail about my previous attempts below, but the big question is this: Is there a way for me to accomplish this?
Thanks to everyone who has helped.
I am working to write a Google Apps Script add-on that, once installed in a document, will execute a function to send an e-mail to the effective user every time the document is opened. What I am currently doing is the following:
function onInstall (e)
{
onOpen (e)
}
function onOpen (e)
{
if (/*Some Code*/)
{
DocumentApp.getUi().createAddonMenu()
.addItem('Start Notification', 'myFunction')
.addToUi();
}
else
{
myFunction ();
}
}
In that if statement, I have tried a few different things:
e.authMode == ScriptApp.AuthMode.NONE
did not seem to work quite right- although I did get the menu the way it was intended, the function did not execute when someone else opened the document. I think this is because for them, the authorization mode is also NONE, so it would merely run the if, not the else. So I tried this instead:
e.authMode == ScriptApp.AuthMode.NONE && Session.getEffectiveUser().getEmail() == ""
But that didn't work either. Again, I was able to see the add-on menu, and when I clicked the button to activate the code I can verify that 'myFunction' did indeed run, but it didn't execute myFunction() when someone else opened the document. I'm open to ideas and suggestions!
Here is myFunction ()
function myFunction() {
var doc = DocumentApp.getActiveDocument().getName();
var userEmail = Session.getEffectiveUser().getEmail();
var openerEmail = Session.getActiveUser().getEmail();
if (openerEmail != userEmail) {
GmailApp.sendEmail(userEmail, doc + ' opened', openerEmail + " opened the document.");
}
}
And one more addition, per the second response given: I was originally using this code, and pasting it into all of my documents. Because this is only used for users within my domain, it worked perfectly fine. Is there any way around this?
function myFunction() {
var email = Session.getActiveUser();
if (email != 'myEmail@domain.org') {
GmailApp.sendEmail('myEmail@domain.org', 'Document opened', email + " opened your document.");
}
}
function triggerBuilder ()
{
var doc = DocumentApp.getActiveDocument();
ScriptApp.newTrigger (myFunction)
.forDocument(doc)
.onOpen()
.create();
}
I would just run the trigger builder to make the installable trigger, or do it manually.
Some more info: I tested the following code, and found that when run, I was able to see the information when I myself opened the document, but not when someone else did. It seems that the onOpen(e) method may not have worked at all, as it did not appear in the Project Executions page either.
function onInstall (e)
{
onOpen (e);
}
function onOpen (e)
{
if (Session.getEffectiveUser().getEmail() == "")
{
DocumentApp.getUi().createMenu("Notification Test")
.addItem("Run function", 'myFunction')
.addToUi();
}
else
myFunction ();
}
function myFunction ()
{
Logger.log ("This is verification that the document was opened.");
Logger.log("Current user: " + Session.getActiveUser().getEmail());
}