Do Google Calendar Triggers execute in order? - google-apps-script

I run a google calendar trigger for every user (10k users) which notifies some external resources when something changes in the users calendar.
However I have some issues because it seems like the triggers don't always execute in order. When for example one user moves an event several times in a second it could be that the notification to an external resource of the last event movement hits first before the others do.
When I look into the Google Stackdriver logging I also could see that the same behavior when logging the trigger. But of course the issue could also be the logger itself.
How can I fix this when my business logic relies on the correct order of changes for a calendar? I know that there is also the Push Notification for Calendar but there is no watch for a single calendar and this will also mean that I need to watch 10k of users which I don't think it's very practical.
So how can I solve this? Or am I wrong about the trigger execution order and the problem lies on the external resources execution order of requests?

Related

How to implement? Spreadsheet with link - Go To Link Grab Data - Update Spreadsheet Row

I am looking for technology suggestions. Or if this can be done in native google sheets (note the site I am looking to access is behind a username and password).
I have a google sheet that looks like this
birth date
link
data_element_from_website
12/31
https://something.com/3920230
1/31
https://something.com/1920238
lets say on https://something.com/3920230 there is a HTML element 123
Twice a day I want to be able to refresh the data, this could be done by going into the spreadsheet and clicking/doing something.
Can this be done?
What if https://something.com/3920230 is behind a login (authentication). Note: I could be logged in to the website in a different tab... I don't think that would make a difference though...
Assuming you have a script that you would like to run twice a day, you can use Apps Script Time-driven triggers:
A time-driven trigger (also called a clock trigger) is similar to a cron job in Unix. Time-driven triggers let scripts execute at a particular time or on a recurring interval, as frequently as every minute or as infrequently as once per month. (Note that an add-on can use a time-driven trigger once per hour at most.)
If it's just some formulae, you can change the recalculation settings to be either On change, On change and every minute or On change and every hour under the File > Spreadsheet settings menu item and clicking on the Calculation tab.
If you want to get data from public site(no login) you can use ImportXML. You can google a lot about it, for example
https://answerbun.com/personal-finance-money/get-revenue-details-in-google-sheets-using-google-finance/
If the page you want can only be accessed using login, this becomes a rather complicated task, especially using only apps script libraries (not much scraping lib support here)

How to get a notification if Google Script's "My Executions" shows that a script hasn't run in specified time

I have a Google Script running every 5 minutes. It works except randomly there are sudden crashes and the script/trigger won't survive that. So, for many different reasons, the script stops running after some weeks of continuing runtime. At this point, I would need a notification.
How can I get a notification if a function (script) hasn't run in say 2 hours? Doesn't matter what the technique/notification is, but probably email would be great for the notification. I don't think the solution can be in the script code itself, because the script randomly (every few weeks) crashes at Google's side and there's nothing I can do about that.
Google Apps Script installable triggers automatically set an email notification. You could edit when it should be sent (i.e. send immediately) and you could add more notifications. NOTE: This can only be done manually.
The above will work for "normal" failures but it there is a service outage or other platforms failures the notification might not be sent.
One option is to log the executions somewhere then set a second trigger to check that the first trigger ran every time that it should do it as expected. NOTE: The failure that prevented that the first trigger run might also prevent that his second trigger run i.e. a service outage so you might want to set other monitoring measures accordingly the the severity/priority of this failure and your project budget.

Creating a task scheduler in GAS?

So I'm just getting back into scripting with GAS and I'm trying to make a scheduler/reminder system for tracking periodic maintenance that has to be done.
The idea is a form that fills info to a spreadsheet (easily done), and then iterate through the sheet and create events on a dedicated calendar for every 'job', with recurrence set to how often the job needs doing. ie: every 3 months, clean the drains.
I have this part of it working. What I'm wondering is... is it possible to have a script call a function at a specified date and time? Like instead of a calendar event that emails you a reminder about the event on the day of (or whenever), maybe call a function so that I can email them some details about the job (again, pulling from the sheet, which is simple enough) and maybe throw it at Pushbullet API to push to the user's phone/browser. I've played with PB's api, and everything, the ONLY part of this that I don't have working is the scheduled future script firing.
If this isn't possible, would anyone be able to recommend an alternative?
Edit: For clarification, I'd be wanting to set up a one-time execution of a function on say ... June 14th. In terms of recurrence, I can just have it set the next scheduled run during the execution.
If I understood correctly, you need to use the Trigger!
Yes, it’s possible. Use time-driven triggers. For details see https://developers.google.com/apps-script/guides/triggers/installable

Sort events in google scripts

Is it possible to intercept the range sort event? For example when user select a range, select sort range, click on sort, I need intercept the sort event. The business problem that I am trying to resolve is, if user doesn't include a particular col for sort, I want to either warn the user or include the required col in the sort.
Please let me know. Thanks.
Currently this is not possible. The events you can use to trigger an Apps Script are quite limited, and they don't behave the way traditional Javascript events do, because they run on the server, not in the users browser. Even if a Sort event was made available, your code likely wouldn't be triggered until the default action was completed and the sorting finished.
You could experiment with the Edit event, that will be triggered any time the spreadsheet values are edited, which may include sorting (I've never tested this). But as I mentioned above, you can't block the edit from occurring, just respond to it's results.
https://developers.google.com/apps-script/guides/triggers/
You could tie the sort you would like to performed to a custom button or script menu command, but you cannot captue a sort event or even disable sort in any sheet where a user has edit rights.
To create a button, follow Insert > Drawing, make a shape (rounded rectangle recommended), save it on the sheet, and use the drawing's drop down menu to assign the script by its name.

Google Apps Script Triggers not firing - but only sometimes

I have a few scripts that I want to run overnight so I've set up script triggers to execute these. Its all pretty straightforward stuff and they run fine. But occasionally, say once a month, one of them fails to execute. No error messages are sent to me by Google or anything like that.
Has anyone else experienced this? Has anyone got any ideas I can check for this error? Obviously if its the actual trigger that's not firing in the first place then the code won't actually run and therefore it won't be able to run any checks.
I'll also flag this with Google themselves in google-apps-script-issues forum.
I have a trigger set to fire every five minutes and a few times a month it will fail. Often it will say something about the function not being found.
You can be notified of an execution failure by setting up an "execution failure notice". In Resources > Current Script's Triggers, after each trigger will be a "notifications" link. Clicking it will open the "Execution failure notifications" window.
You have some choices about timing of the email notification of the failure.
Maybe: I suspect that, after I edit my code, sometimes the triggers miss fire. But I have never double checked this.
Suggestions:
1) add some logging and at the end of the script, grab the log and email it back to yourself.
2) Add a try catch around your code and email any errors that you catch back to yourself.
Google scripts are throttled in various ways and your script may be hitting up against one of these limits. There are limits on the number of emails you can send and limits on execution time of the script in addition to others.