Google Apps Script onEdit() trigger not executing - google-apps-script

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.

Related

Issue with Google Scripts Time-Based Trigger in Sheets addon always Failing

I'm having an issues with a "Sheets Addon" i am working on. I know how to create triggers in a normal script, but what I'm trying to do is create a trigger within an Addon that will call back to a function within that same addon. I want this to be a timed based trigger, every 1 or 2 hrs as the options.
If I create the trigger via sheets scripts, it all works fine, but using the same exact code for the addon.. it fails 100% of the time, but with no specific error, or at least not that i can get to easily.
I'm assuming there is something different to be add to the code to make a new trigger code work within an addon, but not sure what it is. any help would be greatly appreciated.
EDIT 12/4/2019::
example snippet... if i place this into the sheets script, and run it (from the sheet and not from the addon) it works fine. The trigger gets added and calls to the function with no issues. However when i run it from the sheet via the addon, the trigger still gets created but error our when processing.
function testCreateTrigger() {
ScriptApp.newTrigger('testRunTrigger')
.timeBased()
.everyHours(1)
.create();
}
One thing i'm noticing is that the Trigger for the sheet (non-addon) is created with Project name, Deployment, Event, Function ... all populated, but when i run it via the addon, the Deployment is blank.
Also installed the google salesforce addon which has a schedule feature which adds as a trigger, but that addon somehow creates the trigger with a "Blank" value for Project, Deployment, and Function ..but still knows what function within the addon/script to call to process. I think that's what I'm missing, how to create a trigger, from a function, within an addon.

google triggers stopped executing

I have been using a number of triggers based on form submission to a google sheet. Since yesterday the triggers have stopped working.
I have
unlinked and re-linked the form and sheet.
created a new response sheet.
recreated my scripts,
tried macros ( its a simple operation to sort the google sheet responses and send the top most response to another sheet )
the form submit trigger doesnt work. so to test I setup time based triggers. they seemed to work but stopped at 5 am this morning.
I can run the script manually and works fine but triggers do not run.
There was nothing in the logs. each trigger just stopped firing
Can anyone advise ?
Many thanks
I'd like to confirm the issue
Problem
installable trigger onFormSubmit from GoogleSheet stopped executing.
installable trigger onMinute from the same Spreadsheet still working and showing logs.
Logs are empty here:
https://script.google.com/home/projects/PROJECT/executions
Solution?
My attempts to resolve the issue:
Delete and create trigger again
Change the source code
Create 2 same triggers.
Possible other tests:
Create trigger from another account
Create a trigger with no code to test if it could execute at all.
Conclusion
In the end, one of my newly created triggers started the execution again.
I do not know what fixed the problem: changing the function text OR creating/deleting triggers OR some external fix on the Google side.
If some have more issues, please report the issue here:
https://issuetracker.google.com/savedsearches/566222

Apps Script Making onEdit and onOpen triggers work only for the current user

Probably the answer to this is simple but I am having a tough time getting my head around it.
I have a spreadsheet used by many users. I want custom onEdit and onOpen functions to run only for the user who triggers then. For example - if person B opens the spreadsheet, then only his custom onOpen trigger should run, not the custom onOpen triggers for the other users. Likewise if the user edits a cell only his custom onEdit should run.
Currently how I have set it up everyone has his own triggers but they all run the same script every time there is a trigger by any user, meaning the trigger runs 4x for no reason and it's slowing down the spreadsheet, and it also wastes script time usage for the other users who are not opening or editing the sheet.
You can have separate functions for each user and have code dynamically check for current user and run the function of current user only.
See here for getting current user. Link
Assuming the functions are the same but you just aren't wanting it to run for some users, you could use PropertiesService to store a user property to identify if it should run or not. You have to make some UI method to set the property and then you would check for the property at the beginning of each of your functions. Technically the functions would run but you could return out of them if you don't want to finish the logic.

Google Sheets: installable trigger seems to disable simple trigger script

I have a Google sheet with a function that enters a timestamp in a column if the column to its left is updated. It is triggered by the simple onedit trigger, and it only processes for a small set of cells in the sheet. It has worked fine for months with many people.
Then I added a script to do some data cleanup on a separate tab of the same doc. I use an installable trigger, so that I can schedule the cleanup to run once each week.
Unfortunately, while the scheduled installable trigger cleanup routine now works, the original onedit simple trigger timestamp routine does not. To be more precise, the timestamp routine works for me, the author of the script, but it does not work for anyone else.
Any idea how to address this?
Thank you!
the timestamp routine works for me, the author of the script, but it does not work for anyone else.
Issue can be found in Stackdriver logs. Go to Script Editor>View> Executions>Clear all filters. Check the failed logs. Most possible reason is You restricted access to those people. If the "user at the keyboard" is not permitted to do something manually, He cannot do that through scripts either. Another possible reason is Anonymous edits cannot show popups/dialogs; If you added such code to your function, Your function will fail.
Possible Solution(s):
Provide the necessary permission or
Create a installable onEdit trigger, so that the function will run under your authority. In this case, if editors are able to access your editor, they may be able to execute scripts as you. Protecting all sheets with unprotected ranges may help this, but does not provide complete security. Reducing authorised access scope might also help
Another way to do this is by creating a service account and use oauth library to access the sheet.Sample here and here.

Clock Trigger updates spreadsheet, Installable Trigger onEdit not running

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.