Clock Trigger updates spreadsheet, Installable Trigger onEdit not running - google-apps-script

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.

Related

OnEdit Function without Manual EDIT

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)

Time-Driven triggers not working properly

We're working with Spreadsheets and Apps-Script to do some automation, therefore we've set up some time based triggers, some hourly, some daily based, all set up in the apps-script dashboard. It happens more and more often, that the triggers simply don't run. They're just not executed, so I don't see any failure in the dashboard (because the code is valid, though).
I thought, it was about the .getActiveSpreadSheet() method which might not work, when the sheet is not open or visible (which it would be in the most cases), but I recently have a sheet where I open it by id (openById), and the trigger still works just as he pleases.
Simple example code is:
function testTrigger(){
var sheet = SpreadsheetApp.openById($sheetid).getSheetByName("ha")
sheet.getRange("A1").setValue(new Date());
Logger.log("test");
}
And before I used
function testTrigger(){
var sheet = SpreadsheetApp.getActiveSpreadsheet.getSheetByName("ha")
sheet.getRange("A1").setValue(new Date());
Logger.log("test");
}
The trigger for this function is set to fire every minute. As I can see in the sheet, the last time it ran was 12:35:48, but it's now 12:40. So why isn't it working?
By the time I'm writing this, it suddenly fired at 12:40:48. But now it's 12:44 again, so there are already 3 runs missing! Why does this keep happening?
And: Would SpreadsheetApp.getActiveSpreadsheet.getSheetByName("ha") even work with a closed sheet? Has the sheet to be open AND active in the browser? We were using Spreadsheets and Apps-Script for months now, I used getActiveSpreadsheet.getSheetByName("ha") every time and it worked. It seems, the time driven triggers are buggy atm, I have no other conclusion (since the code and everything else is valid).
Is there any way to find out, why a trigger didn't fire (beside a code error, which I would of course see in the dashboard).
And would these problems also occur, if I would use programmatical triggers?
Thanks in advance!
Here's an image of the problem (nothing changed in the code or in the trigger during these runs):
To answer some your questions
As specified by the documentation for time-driven triggers "The time may be slightly randomized — for example, if you create a recurring 9 a.m. trigger, Apps Script chooses a time between 9 a.m. and 10 a.m"
If instead you create manually a ClockTriggerBuilder with the parameter nearMinute(minute), this "Specifies the minute at which the trigger runs (plus or minus 15 minutes)". In other words: You cannot expect from the Apps Script triggers to run precisely at the time you would like them to.
SpreadsheetApp.getActiveSpreadsheet() works if you have a bound script, rather than an alone-standing script. In any case, to avoid error sources, it is wise to use SpreadsheetApp.openById(id) instead of SpreadsheetApp.getActiveSpreadsheet()
You can supervise your triggers und executions and My triggers, however from your description it seems like the trigger is fired correctly, just not exactly at the time you would desire.

Google Apps Script onEdit() trigger not executing

I have been implementing a spreadsheet for about a year and a half now that constantly runs a number of trigger based GAS functions to check data, send emails based on that data, and update a staff calendar.
All of the time driven triggers are executing normally. I have not had an issue with them. However, the spreadsheet driven triggers (onEdit, onChange) are not working at all. I left work for a two week vacation and I did not change the script at all. When I came back, it no longer worked.
I deleted the trigger, recreated it, and switched between onEdit and onChange; the trigger will still not fire. The function that is triggered on edit is dependent on the event for it to execute properly. I test ran the function outside of the trigger and it failed where I expected it to (where the event dependant variables are) so there was no surprise there.
As far as I can tell, the spreadsheet driven triggers just won't fire at all. Does anyone know of any issues on Google's end, are spreadsheet driven triggers working for any of you guys? As far as I can tell the trigger just stopped working all together.
If I explicitly nest my function inside the onEdit() function in the script, it will execute, it won't if I just create a trigger, however it throws an error saying that the script does not have the permissions to execute MailApp.sendEmail() even though the app is already authorized to send emails as me.
Even more interesting, the functions executed with time driven triggers are still able to send emails and update my calendar.
It seems like the spreadsheet driven triggers have quit working all together.
Just to be clear I did not make the mistake of naming my function onEdit or onChange.

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.

Trigger onEdit Trigger through programmatic edit

I've built a spreadsheet that tracks form responses using the onSubmit trigger. This works well. I then want to display part of the spreadsheet on another spreadsheet, and have built a script that does this.
In order to ensure the second spreadsheet is dynamically updated and synchronised with the first, I have tried to use the onEdit installable trigger; however, it does not work when it is programmatically edited, only if I manually edit spreadsheet 1. Any solutions?
Triggers do not fire on the spreadsheet changes made by scripts. I think the idea is that the script making changes can also follow through on any consequences of those changes. In your case, I see three solutions:
If you just want to "display part of the spreadsheet on another spreadsheet", then importrange command suffices, you don't need a script to do that.
The function triggered by form submission can modify the target spreadsheet itself. To do this, you need an installable trigger running on form submission, since simple triggers cannot access other spreadsheets.
If you really want to trigger a function in a script attached to another spreadsheet, you can run a time-based trigger that will check the last-updated time of the spreadsheet.
Example of #3: a function that can be set to run every 5 minutes, to detect changes of any kind.
function checkForUpdates() {
var updated = DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId()).getLastUpdated();
if (new Date() - updated < 300000) {
// updated in the last 5 minutes, do something
}
}