Sequencing large scripts in Google Apps Script - google-apps-script

We use Google Sheets to store and manipulate event-based data from the users of our web application. Recently, I've been using Google Apps Script to automate this data manipulation process, and now I'm looking for some advice.
My scripts were working well for a while and then late last week, I started receiving the error, 'Exceeded Maximum Execution Time'. I received this error on two of my scripts that worked fine the day before, with no edits in between. I'm working with a lot of data and have even hit the 2,000,000 cell limit on my spreadsheet a couple times.
My goal is to be able to sequence my seven scripts together so that I can fire them all with one trigger. How can I do this without hitting the max execution time error? Sometimes, my spreadsheet will crash when I just try to delete one of the sheets within it. Could the size of my spreadsheet be causing the scripts to fail?
I've done my best to follow best practices outlined in Google's documentation, but can anyone recommend best practices for spreadsheet and script organization?

You might be interested in a tool written by Bruce Mcpherson for massively parallel processing in Google Apps Script.
http://ramblings.mcpher.com/Home/excelquirks/htmlservice/parallel
http://ramblings.mcpher.com/Home/excelquirks/htmlservice/parallel/implement

Here you can find the documentation related to quotas on apps script, as you can see the script runtime is 6 mins after that, the server will stop the script and throw an exception as, as you are experiencing.
If you are already applying Google's best practices, try to find where are your bottle necks.
-Do you need to read the complete range of data or just a part of it?
-Is there any information that you could store (e.g. in cache or properties) so you won't have to calculate or read again?.
-Are you tracking the time for each script?
-Are you reading the whole range of data on every script? or reading it once and passing it as parameter to the other scripts?
hope this helps.

Related

Google sheets import macros on multiple spreadsheets

Google recently added a feature where you can create custom shortcuts by linking it to Google App Script code. It seems pretty good and useful, except it seems the macros need to be setup spreadsheet by spreadsheet instead of in general across all spreadsheets.
I don't know about you guys, but I have multiple multiple Spreadsheets... close to 1,000 Spreadsheets that I edit sometimes. It would be impossible to import the macros one by one spreadsheet. Does anybody know how to do it for the "environment" as opposed to just one spreadsheet?
Seems like a great improvement for Google sheets, but kinda useless for me because I use so many spreadsheets.
Edit: I want to be able to write one macro and use it in any spreadsheet. I'm confused on whether this can be done because on one hand I read the docs and it said macro can't be distributed, but on the other hand other people have said you can do it by using GAS.
One of the comments below is absolutely correct, it is like the personal.xlsb excel workbook concept. In my opinion, shortcuts are useful only if they work all the time, it is a habit thing. It only is useful when it is consistent.
Google built-in shortcuts are useful because they work across all spreadsheets. If you had to set it up for each individual spreadsheet, I bet you no one would use them.
I wish google build in a developer mode or something that make them work for each spreadsheet without doing anything. I understand there are security concerns. But Google can figure it out.
If someone knows how to do it with GAS please post.

Service using too much computer time for one day

This indicates that the script exceeded the total allowable execution time for one day. It most commonly occurs for scripts that run on a trigger, which have a lower daily limit than scripts executed manually. The limit as far as I am aware is 6 hours per day. None of my scripts run for that long.
Everything was fine earlier. Suddenly, over the last 3-4 days, all the scripts have started failing. I have no clue what's happening. My only hunch is that one of my scripts is going into an infinite loop. But still, if that happens, it will fail soon and collectively, all scripts would not run for 6 hours.
Please help to resolve this issue. I have a G suite id but Google mentioned that I get help at stackoverflow and that they cannot help.
The exception message should also tell you a lot about what is happening. If you are not sure which script is causing the issues you can try to go to the Google Apps Script Manager. In the menu bar on the left you will see a section called My Executions. If you click that you can see a list of scripts that ran and how long they ran for. This page also allows you to sort by duration and filter the results to hopefully help you figure out what is going on.
It possible that you have reached your quota. Having a Google Suite account allows you up to 6 hrs/day of Trigger Total Runtime. Also make sure you are not going over the limit on single script runtime as outlined here further down on the Quota For Google Services page.
For Google Suite Accounts the quota is:
Script runtime: 6 min/execution
Custom function runtime: 30 sec/execution
I had a similar problem. It's a little maddening because getting an error for the script leads one to believe it is a possible syntax problem. More likely, the script never ran in the first place, which is why you likely don't have a failed execution status for the trigger. What may have happened instead is that you have a script or scripts that are still firing for documents that no longer exist.
Navigate to "My Triggers" in the G Suite Developer Hub: https://script.google.com/home/triggers
Click on any time-based events and select "executions."
Search the "status" column for the words "Timed out," and check the duration.
Delete the trigger(s) that is timing out, or otherwise resolve why it is timing out (such as restoring the deleted document).
In my case, I had deleted several old spreadsheets, but one of my add-ons (Supermetrics) was still firing triggers. I had incorrectly assumed that when I deleted my spreadsheets the triggers would have gone with them. Instead they were firing and timing out after about six minutes, and then four hours later they would try to execute again. I added up the duration for all of these triggers and, sure enough, it exceeded my quota.
Delete those old triggers, and you'll likely be back within your quota limits the next day when it resets. This is what worked for me, but I imagine it's not a one-size fits all solution.

Google App Script trigger is not firing

I am a Google App Script user since last 2 years. I see here in India during peak time i.e during evening the 1 minute trigger is not firing at proper time. Is there any server side issue or there is some other issue. Saying about the worse case scenarios, today my script trigger has not been fired since past 2 hours. I have correctly done logging. No log is printed means it does not even go to that method. Can anybody tell me what might be the exact reason behind it.
It might be beneficial to show us the code behind your script, given that there is a method that is potentially being skipped over. Have there been no errors thrown to you via the Google Apps Script notifications?
It seems, perhaps, your issue may fall into place with this currently reported error: https://code.google.com/p/google-apps-script-issues/issues/detail?id=2708
We're likely not going to be able to give you a precise answer to your problem if we can't examine what you're currently working with. I wish you luck in your endeavor, though.

Google Apps Script alternative to ScriptProperties

Can someone suggest an alternative to ScriptProperties as a means of storing and retrieving short-term (textual) data.
I have a long-standing application built in GAS. Because it kicks off a number of 'threads' I store some information about each thread, ultimately to determine when all the threads have finished and thus when the application can release various 'locks'.
I'm using ScriptProperties to store this information. It used to work perfectly but has been beset by troubles over the last 6 months or so. The problems are mainly:
1. Application load times increase massively as more and more data collects in script properties (it isnt possible to clean down scriptproperty data)
2. ScriptProperties are (now) unreliable under concurrent access
3. ScriptProperties can't be read or written to in rapid bursts
Someone has suggested using ScriptDb. Can anyone confirm this (or any other) viable solution?
Many thanks
Can you have a look at CacheService ? I have used with lot of success.
CacheService is only best-effort and is not intended for guaranteed storage (but it is fast!). If you need to be sure it will be there, use ScriptDb.

Any specifications/docs around optimization of Google Apps Script, to avoid timeouts and "hangs"

From my experience so far, it seems that if you write a script that makes lots of expensive calls close together, the functionality just "hangs", or you get inconsistent responses, and have to refresh the browser because sheets stop updating etc.
Are there any docs or specs that clarify this, as releasing an app fr real-world use is not possible if users can expect it to work most of the time, and produce random results every now and then...
There's specifications/docs here and here.