Is there a way to pass a value back to a HTML page while a server side function is running?
In the past when I was using the UI service, early in the function I would just create a label in the UI like:
app.createLabel('Working, hang on!');
And it would appear while the script was running, letting them know the script was running. When it got to certain parts of the script I would then be able to update/create a new message like:
app.createLabel('Halfway there, hold on.');
Or
app.createLabel('Fetched values, processing them');
etc., to tell them where we are if the script was running for a long time. An added bonus was that it could also update them on what direction the script was taking, so for example if a room booking script found that the first room was unavailable, it could pass a message back such as:
app.createLabel('First room was busy, checking other rooms');
And so on, and so forth.
Is there a way to do this with the HTML service? It seems my only options are success and failure handlers updating the HTML page when the function is complete, which don't seem to be what I want, as it's not a 'Real Time' message.
You can still use ui.alert() in sheets and docs, and sheets also has toast().
To keep the message within a sidebar or dialog that you've created using the HTML Service, you can set up a separate poller function on the client, to retrieve messages from the server every once in a while. Your long-running function could queue up a message via a function call on the server, which would be picked up by the poller on its next cycle, and displayed to the user.
See How to poll a Google Doc from an add-on for an example of a poller.
Related
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.
According to Google app script time-trigger documentation https://developers.google.com/apps-script/guides/triggers/installable#limitations,
Time-driven triggers cannot run more frequently than once per hour. How can I use app script to write an add-on that process large amount of data?
If I run a trigger, it will only allow me to trigger once. I can't chain the triggers so it can continuously run.
You cannot do so using triggers because they would run at most once per hour in the context of an add on.
What you can do is to initiate such processing client-side while possibly showing progress on a sidebar. The client js in the sidebar can chain server calls.Of course being all client-side means the user must have opened the file and if they close the browser tab or navigate away, the chain calls will stop. That can be handled by alerting the user and making them aware that a "sync" happens and sometimes takes a while. Currently there is no other way to handle this unless your client call and time trigger call another server which does the processing, like appengine or gce.
I would like to have a spreadsheet respond to incoming doPost(e) request, by presenting a user interface.
I do get a server error when I do that with the published script. (call ss.show(userinterface)
Is this possible to do anyway?
I just want to get a notification when data us pushed to the spreadsheet using an external applicatoin. Adding a row is okay, but I would like to trigger the Htmlservice popup to give information about that data.
No its not possible to show ui from there, only from an interface created from doGet and responding to browser events from the user that created it.
You have some options:
1 keep open a sidebar that regularly polls the status (from a script property for example every 10s).
2 i havent tried this from a post, but try doing ss.toast("cheers"). Doubt would work thou
I have a Google Apps Script which uses a JSON web API to get a list of StarCraft2 tournaments from a site (using UrlFetchApp) and create corresponding Google Calendar events in a variety of Calendars (one Calendar per region/league combination).
I have a trigger set to every 5 minutes, that emails me immediately on errors, and if the script completes normally and has added any events or run into any errors I caught, it emails me as well.
Most of the time, there are no changes necessary (I cache the JSON info using ScriptDb to avoid calling out to CalendarApp unless I need to) and the script is silent. However, overnight I received several emails which contained the messages:
The resource you requested could not be located. (line 154)
Service error: CalendarApp: Error body {font-family: arial,sans-serif}
G o o g l e Error Cannot access the calendar you requested (line 155)
TypeError: Cannot call method "getEvents" of null. (line 155)
Lines 154, 155 and 156 are:
var calendar = CalendarApp.getCalendarById(calendars[region][league]);
var events = calendar.getEvents(new Date(1900, 1, 1), new Date());
events.forEach(function (event) { event.deleteEvent(); });
At this point in the script I've already attempted to add any new events and I'm about to query each of my Calendars to delete any events which occurred in the past.
These errors appear to be an issue with the availability of CalendarApp, but the Dashboard does not indicate any issues with the service.
My project key is: MWhWmFSrL1dK3RJui6Aphb12cA4KNgw7R
I have many scripts that use CalendarApp and all of them have occasional errors in their execution, wether in manual mode or time triggered, it seems (but I know this is an empirical feeling) that this service is subject to errors when the traffic is too high or the internet connection is too slow (a few month ago my office had a very poor internet connection and the script used to generate a lot of errors -while it didn't when executed from home-, now that the situation is better the same script runs smoothly everywhere).
Anyway, whatever the real cause might be (and only a Google engineer could answer), the solution I used was to put the Calendar calls in a try-catch loop that tries the connection a few times and since then I have no error notification anymore.
On another script that I didn't change yet, I keep getting errors from time to time but since it doesn't concern critical information I don't really mind... note that I just checked right now and I haven't receive any notification for 5 days ! (must be a good season for calendars ;-)
HiMy problem is following: I would like to create small web page, on which it will be possible to create event in Google Calendar, but with some restrictions. In my case this calendar could be edited by my flatmates to reserve washing machine. This reservation cannot overlap and also all of us has limited number of days when we can use it. I have created private calendar, and I have created script which validate requests, and if everything is ok add event to calendar. This script is executed as me (because only I have permission to edit this calendar).
But I have problem with fetching information which user execute this script (me or one of my flatmate). Class Session contains 2 methods getActiveUser() and getEffectiveUser() but active user does not work (I guess because privacy protection policy). But if I create another script which is executed as user accessing the web I can get active user.
Does anybody know if is it possible to communicate somehow between this 2 scripts embedeed on the same site? I want to pass email of active user from one script to another. Or maybe do you know better solution how to solve this problem?
Regards
Adam
As you noticed, you need to set the script to run as the user accessing it to get his email. Then, instead of accessing the calendar directly (which you obviously can't) you can call another script published, but running as yourself allowing anonymous access that will receive this request from the "viewing" script and create the calendar events for it.
After you publish this "background" script, get its url and use it on a UrlfetchApp.fetch call to it. Then pass the parameters as url paremeters (or on payload if you prefer to use post instead of get).
The background script may even use ContentService to give nice return values to the calling script.
Sure, you can do so, but it's not as simple as you'd like. Make your admin level script run a web service that responds to the other script. It can probably be hacked as you can't authenticate the users, but comon, this is a washing macine!