Updating HTML interface from Server Script - google-apps-script

I have written a standard google web app script with server code and a html interface. The web interface prompts the user for a month and year then creates a google document (by copying a template document) for every day in the month selected.
I have got the code working but I would like to provide some sort of status as the code is running. It takes about 4 mins. I put out a message before calling the server code and a message when it completes but I would like to provide progress updates. i.e. "Docs Created 1", "Docs created 2" etc.
I guess what I am really asking is can google server code update something on the web page while it is running?
Thanks.
Will

No, it cannot (AFAIK).
But since it is taking 4 minutes (which is dangerously close to the 6 minute limit), I suggest you create only one document on your function, then return to the client-side. The client-side will then update the status on the screen and call your server-side function again, which will do the second document, return and so on, until you finish.
By doing this you not only updates your client-side but also avoid getting close to the maximum execution limit. The downside, of course, is that you'll add up some seconds to your total execution time. But for long tasks I don't much problem, actually informing the user of your script progress will make it feel faster in comparison.

Related

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.

Monitoring a chatroom with Chrome extensions

I want to monitor a chatroom with a selfwritten chrome extension. Because I don't know anything about the scripts behind the chatroom system itself, I thought about a simple timer and export script.
My idea is a periodical timer (let's say every second, because it has to react as fast as possible) calling a function, which reads the complete HTML of the current tab (with chrome.pageCapture.saveAsMHTML) and sends the hole HTML to an external REST service (via XMLHttpRequest()).
I know that this approach is very ressource consuming, but that doesn't matter as all this will run on a dedicated computer. Of course I thought about using chrome.webRequest.onCompleted to trigger the export but, as already mentioned, i have no idea about the technical interna of the chatroom.
Unfortunately I can't find any API to create a time base on seconds, but only on minutes (chrome.alarms.create). Or is there a more elegant way to do this job?
Any hints appreciated.
More elegant way would be to use a MutationObserver, at least as a source of a "there are some changes" event. But maybe the chat is implemented in such way that getting the changes (and then sending only changes, not the whole page) will be convenient too.

Make an item display only for certain period of time on the webpage

Hi guys I am constructing a task distribution management system for my team in which I want to add a functionality that:
When I create a task, I will have an option to choose "how long is this task valid for being taken". For example, when creating the task I put "2 hours" in the
<input id="valid-for">
, then this task will only be displayed on the dashboard for 2 hours from the time it was created and then after 2 hours -> "display: none".
I've searched the web for the mechanism of achieving this feature but I didn't get a satisfied answer probably because I don't know the right terminology to google. I tried to use AJAX and use TIME_STAMP type attribute in MySQL but didn't know how to proceed. Could anybody tell me how to achieve this feature by the use of MySQL, jQuery or any other technics that could fulfill this feature? No code necessary I just need some explanation.
Thanks guys!
Without knowing any more details, here is how I would consider writing the code:
In the database, have a start time and a use-by time.
In your browser page, you can run a script periodically, say every minute (this is called polling). In this case, you can use Ajax to call back to the server for updates.
At the server end, check for new tasks as well as expired tasks. Then send the results back to the Ajax caller.
Back at the browser, update the dashboard accordingly.
I would be inclined to remove the task on the browser rather than simply hide it.

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.

catching Exceeded maximum execution time?

Is there anyway of catching this GAS error: "Exceeded maximum execution time"
I mean catching with try ... catch(e) // so far it's not working for me.
Thanks
As written in the comments to your question, thats not possible. But, however, you can set a flag in scriptDB or properties when execution starts and clear that flag when execution comes to a normal end, so you can find out during the next run wether your script came to a regular end when it was run last time and try to take corrective actions if not.
The answer above is correct; it's not possible. An easy alternative to the workaround that pbhd mentioned would be to simply track the runtime of your script (e.g. comparing results of new Date().getTime() at regular intervals) and run whatever you'd include under your catch statement right before you hit the maximum execution time. The maximum is 6 minutes (reference).
That way, you don't have to catch the error -- you can preempt it.
During normal testing, it is possible to accidentally create an infinite (or very long running) loop that consumes the daily execution time limit 100%.
Even if you realize what you have dome wrong immediately, you cannot immediately re-try with Google scripts for another 24 hours - thus slowing down ongoing development significantly and maybe forcing the developer to do some other work , taking his focus/"attention stream" away from the current problem. This is almost always bad.
My product ("IBM OLIVER CICS test/debug" - see Wikipedia article) solved this problem - and many others - around 37 years ago - by having a time limit on any particular transaction and intercepting the resulting time out, allowing options of:-
continuation or
examine/modify variables
"manual" re-try (for the same time) or
abort.
Google could implement this approach just as easily - by "pausing" if execution time is looking too heavy. I had a similar solution to other resources in OLIVER - such as excessive API calls ("possible macro loop") and excessive memory usage.
It seems it takes an "old timer" like me to provide solutions to problems that have existed "since the beginning of time" (and certainly before PC's were thought of).
Googles current "solution" (i.e. absolute limits) only helps Google keep its own servers from being swamped. It would be easy for them to do what OLIVER did all those years ago. By the way there should be no "IBM" prefix on the Wikipedia article - it was my own product and some clown Wikipedia editor altered it to include the prefix.
(By the way, Google do not prevent other scripts on same s/s running - that maybe only use minimal amounts of extra time ( i.e. Scripts on the same spreadsheet still work) . I tried renaming the original script as an experiment but it was stopped after a very short time with "exceeded execution time" error.
GIZ-A-JOB Google - you know its worth it!