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.
Related
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.
I use an OnEdit function to move data to another sheet whenever a changed in a particular column. But The change should happen with a formula. But, it is not working.
example : When I change A1 to "ok" manually the OnEdit function work, but when the A1 cell change through an If condition to "ok" it's not moving and there is no movement.
Can you help me work the Onedit function without a human interfere?
Issue:
As you can see in the official documentation:
onEdit(e) runs when a user changes a value in a spreadsheet.
That is to day, this trigger does not track changes that are made programmatically:
Script executions and API requests do not cause triggers to run. For example, calling Range.setValue() to edit a cell does not cause the spreadsheet's onEdit trigger to run.
Workaround:
A possible workaround to this limitation is to have a time-driven trigger instead. You could, for example, have a trigger that will fire every minute, disregarding whether the spreadsheet was edited or not.
Then, inside the triggered function you could think of a way to check if the spreadsheet was edited during the last minute (for example, have the previous values stored in another sheet, or in a script property, and compare them to the current ones), and execute your desired actions if that's the case.
Reference:
Simple Triggers: Restrictions
ClockTriggerBuilder.everyMinutes(n)
I have an Apps Script with onEdit(e) function. That function calls some other functions depending on existing and new data. In certain cases, showModalDialog needs to be shown. If I enable function onEdit(e) via trigger onEdit, it launches some function twice (like inserting rows and performing calculations). If I delete that trigger from a trigger list, then showModalDialog cannot be shown and error You do not have permission to call showModalDialog at showDialog(Code:82) at onEdit(Code:1270)
The idea is to make showModalDialog to appear when required and start all other function only once when a user edits data.
I would really appreciate if you help me with a solution.
A function called onEdit(e) is already a trigger on its own: this is called a simple trigger. To show the showModalDialog you will need to create an installable trigger. If you do that on the function onEdit you'll probably end up with two triggers (one simple, one installed) for the same function. IMO, It would be better to rename the function and then use an installable (onEdit) trigger on that function.
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.
I'm playing with triggers.
In a spreadsheet, I have a function that inserts a date in a new row. Runs as a clock trigger every minute. Works fine if the spreadsheet is open or closed (loving that part).
In a standalone script, I setup a trigger for onEdit in the above spreadsheet. All it does is email me the e.value.
It works if I'm in the spreadsheet and just type in some characters in a cell.
What I'd "expect" is each time the date is inserted by the first trigger, it would trip the onEdit. Alas, it does not. Even if I run the insert date function outside of the trigger, my email onEdit trigger does not fire.
Any thoughts?
I ask, as my goal was to have scripts updating a sheet that kicked off an onEdit event.
Jim
UPDATE #1:
Maybe this code example helps?
I simplified this to one script inside a spreadsheet container. Each function works fine on their own and the second works if I do any interactive changes to the spreadsheet.
This is just exploration, so please ignore quota's or the utter nonsense of what I'm testing here :-)
insertClockTrigger: runs every minute, works fine
installableOnEdit: never triggers as an installable onEdit when insertClockTrigger fires
I'm curious why what looks to be an edit done by the insertClockTrigger is not firing an onEdit trigger. Do triggers not trigger triggers (could not help myself!)?
function insertClockTrigger() {
var sheet = SpreadsheetApp.openById('spreadsheet-ID').getSheets()[0];
var nDate = new Date();
sheet.getRange(sheet.getLastRow()+1,1,1,1).setValue(nDate).flush;
}
function installableOnEdit(e) {
GmailApp.sendEmail("some#email.com", "Test trig2", "FISH: "+e.value);
}
Call the onEdit function from the clock trigger function.
Also, you might hit a quota issue with running such a trigger every min.
Think I found an answer.
Henrique Abreu posted:
http://productforums.google.com/d/msg/apps-script/HmBg0p7dOZ8/xMb6wfcfvVQJ
Yes, "on edit" events are not called when the changes are made from
the code, they're are called only for manual changes. This behaviour
is intended by design, as to avoid unnecessary recursion problems.
Also, it just too easy to just call your desired on-edit function
directly from your code. i.e. when you script that inserts a new
ticket does so, it can just plainly call your "fundEmail" function,
there's no need for another trigger.
onEdit should be renamed to onUserEdit. It doesn't receive system changes, only direct user edits.
I haven't tested this, but onChange may do what you want.