Pass variable from Simple trigger into Installable trigger - google-apps-script

I know that installable triggers run as the author of the script. But some services, like email service will work only from installable trigger.
I need to use simple trigger to check if current user has access to edit the specific range and installable trigger to send email.
I have code like this:
var MY_VARIABLE;
function onEdit() {
MY_VARIABLE = 'onEdit was here!'
}
function myOnEdit() {
// installable trigger
Browser.msgBox(MY_VARIABLE);
// send email, use email service
}
The result is undefined.
And I'm willing to have 'onEdit was here!'

if you write something like:
function onEdit() {
MY_VARIABLE = 'onEdit was here!';
myOnEdit();
}
function myOnEdit() {
Browser.msgBox(MY_VARIABLE);
}
it will work. But if "myOnEdit" is not call at the same time than "onEdit" the script will forgot everything. Each time the stript is triggered (onEdit or when you manually launch a function) the environment is reset and the content of "MY_VARIABLE" is forgotten.
What Wim den Herder proposed on his comment is the good solution, you need to store "MY_VARIABLE" value somewhere Google Apps Script can remember through the differences instances of the script. That's what the propertyService allow. One last thing, propertyService needs authorisation to be run. So you can't really use a function that's called "onEdit" for it to be run at every cell modification, you need to rename that function (for whatever you want) and set a onEdit trigger on it.

Related

How do you use Logger.log(e) with Google Forms? (Using the NEW Apps Script Editor)

In the legacy Apps Script Editor you could log e from Google Form submissions.
How is this done in the NEW Apps Script Editor?
function myFunction(e) {
Logger.log(e);
}
To make sure we are on the same page:
If you are going to run a function like this in any editor you are going to get null:
because simply e is not defined and it is only returning data upon trigger executions of this function. But this function is executed by some events depending on the type of trigger you are using. Therefore, you are not going to see anything (that is not null) in the console if you manually execute this function.
After the function is triggered by specific events:
In any editor again, you can go to the execution page to see the details of the execution. In the new editor, you go to Executions:
and you can see a list of all the executions of this particular function. For example, if your function is a simple onEdit trigger e.g. onEdit(e), you will see this upon editing a cell in the spreadsheet:
You can also see the type of the execution, whether it was executed by the script (Editor) or by a trigger (Simple Trigger).
But anyway, trigger functions are not supposed to be executed manually. As the name suggests, trigger functions are triggered upon events. It wouldn't make sense to use a trigger function and need to manually execute it. It would be a regular function then.

onEdit() function does not triggered when change was made by automatic script

my onEdit() function calling to other function when there is a change in the sheet.
if the user makes the change all works fine.
but if the change was made by google-form (the form fill some cells with the answers it gets) onEdit() does not trigger.
do I miss something?
Yes, you forgot to read the documentation for the simple triggers:
onOpen(e) runs when a user opens a spreadsheet, document, or form that he or she has permission to edit.
onEdit(e) runs when a user changes a value in a spreadsheet.
onInstall(e) runs when a user installs an add-on.
doGet(e) runs when a user visits a web app or a program sends an HTTP GET request to a web app.
doPost(e) runs when a program sends an HTTP POST request to a web app.
and installed triggers:
Even though installable triggers offer more flexibility than simple triggers, they are still subject to several restrictions:
They do not run if a file is opened in read-only (view or comment) mode.
Script executions and API requests do not cause triggers to run. For example, calling FormResponse.submit() to submit a new form response does not cause the form's submit trigger to run.
Installable triggers always run under the account of the person who created them. For example, if you create an installable open trigger, it will run when your colleague opens the document (if your colleague has edit access), but it will run as your account. This means that if you create a trigger to send an email when a document is opened, the email will always be sent from your account, not necessarily the account that opened the document. However, you could create an installable trigger for each account, which would result in one email sent from each account.
A given account cannot see triggers installed from a second account, even though the first account can still activate those triggers.
Consider what would happen if your onEdit(e) was activated by programmatic changes, such as if your onEdit function alters the spreadsheet values...
In your situation, where you want form submission to activate your on edit function, You will need to install a form submission trigger (there is no simple trigger for form submissions).
An example function to receive your form submission trigger:
function giveMeAnInstalledFormSubmitTrigger(formSubmitEventObject) {
if(!formSubmitEventObject) throw new Error("You called this from the Script Editor");
var newEventObject = /* do something with the formSubmitEventObject */;
// Call the on edit function explicitly
onEdit(newEventObject);
}
You can read more about the event objects that triggered functions receive in the Apps Script documentation: https://developers.google.com/apps-script/guides/triggers/events
onEdit() will not be triggered by another script editing a sheet or by a form submission.
You can use onFormSubmit(e) instead depending on what your function does it would be a good idea to use the e parameter of the trigger.
https://developers.google.com/apps-script/guides/triggers/events#form-submit
What i have done is that i created an extra sheet with a cell that is the same where the primarecell is(the cell that changes). Leave it for now. I open the scriptapp and write down the code(the code that should happen if the cell changes). Then i wrte down "if" and take the two value i both cells and make an trigger that goes on every minute. With that said, if The cells are the same the "if" is looping on "true". But if something change on the primary cell, it will be "false" and you use "else". Under"else" you should have what ever function you want when soemthing change in that cell. + You must have a funtion where you insert the new value form the primarycell to the "check cell" if u want this too function as loop. Otherwise the "if" code will loop "false" every minute becuse the both cells are not the same.
My english and explaining is not that good:(

Google Sheets Installable Trigger not firing for non-owner users

I am attempting to make an installable trigger fire when a spreadsheet is edited, and run a function called DutySwitch(e).
The function works fine when run from my account (owner). But nothing happens (even in the logs or execution transcript) when another user edits the sheet.
This is the installable trigger. I need the installable trigger's ability to run the authorized service of creating calendar events.
This is the beginning of the called function:
function DutySwitch(e) {
Logger.log("triggered");
var sheets = SpreadsheetApp.openById("10h-8rKINy56Ap5OtSum1U7jDlvrqiLaasxhYP1LkGcM");
var dutySwitchSheet = sheets.getSheetByName('Duty Switches');
I have tried deleting, saving, and recreating the trigger. I have tried revoking and returning permissions.
Any help would be greatly appreciated.
What you need for your use case is Simple Trigger's onEdit() trigger.
Just make sure to share the spreadsheet to non-owners. Did this to my spreadsheet, and the onEdit() works even for non-owners.
Don't forget the 'can edit' access type when sharing (File->Share) to non-owners.

Script stopping, may be due to onEdit() SpreadsheetApp call and then DocumentApp call

this script stops just at the entry of the last function : addToDocument().
Until this point all is working. I presume a problem due to onEdit() and DocumentApp call?
Note that separately, my addToDocument() works perfectly.
function onEdit() {
// simple timestamp -- when a single "t" is entered in a cell, replace it with a timestamp
// see https://productforums.google.com/d/topic/docs/rC6MpQDC7n4/discussion
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var cell = SpreadsheetApp.getActiveRange();
if (cell.getValue() == "t") {
cell.setValue(new Date());
}
formatDate() // Some Date formatting using : 'SpreadsheetApp' call
mefCond() // Some conditonnal formatting using : 'SpreadsheetApp' call
doCounts() // Some numéricals opérations, using : 'SpreadsheetApp' call
//At this point the scripts enter in the following function,
//and stops on the first line. Nothing being executed.
addToDocument() // Time stamp on a document using : 'DocumentApp' call
}
Any ideas ?
Thanks for reading,
Eric :-)
When OnEdit is run manually it runs with a different set of permissions but while Triggers themselves have specific restrictions as mentioned here. From that page see..
They can modify the file they are bound to, but cannot access other
files because that would require authorization.
Please refer to this for the authorization modes and what you can do under each of them. May be line 2 below is affecting you...
The solution for you I believe is to convert your simple trigger into an installable trigger. Here are details how to install a trigger for your spreadsheet. Nothing will changes with respect to your onEdit() function, you just have to run installation code snippet once to create an installable trigger.
function createSpreadsheetEditTrigger() {
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger('onEdit')
.forSpreadsheet(ss)
.onEdit()
.create();
}
And here are the details on permissions and other details. In here it clearly mentions that you can access other services..
For example, the installable open trigger for Google Sheets activates
whenever the spreadsheet is opened by any user who has edit access,
just like the simple onOpen() trigger. However, the installable
version can call services that require authorization. The installable
version runs with the authorization of the user who created the
trigger, even if another user with edit access opens the spreadsheet

Can I set a trigger to fire when a function is executed?

I'm trying to set a trigger in Spreadsheet App, but I didn't figure out how to set a trigger to fire when a function is executed, even if it was executed from a onEdit(e) event. For an instance, I want to check if a content was changed when I run a function.
I observed that when something is changed or edited by a custom function in spreadsheet it does not trigger the onEdit() / onChange() functions. Is there any way to do this, or better, to set a trigger when a function is executed, even if in background?
Nope, writing something on sheet via appscript function cannot trigger onEdit/onChange trigger available in appscript. Also, even when you copy paste the data in spreadsheet, it will not trigger the onEdit trigger.
In such case, Like #Zaq said in his answer, write that function's or trigger's code in your basic function itself.
Alternatively, you can create a new sheet in your spreadsheet with only one cell and whenever your function runs, write 'called' or something in it. And check in your trigger function continuously (i.e. 1 min trigger) whether the value is 'called' or not. If yes, run your trigger and reset the value else do nothing.
No, there is no trigger that fires when an Apps Script function runs.
If you want something to happen when a certain function runs, you can do it by including a call such as doSomething() in the body of that function.