0
votes

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("[email protected]", "Test trig2", "FISH: "+e.value);
}
3

3 Answers

0
votes

Call the onEdit function from the clock trigger function.

Also, you might hit a quota issue with running such a trigger every min.

0
votes

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.

-1
votes

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.