g-suite Google Calender Apps script On Event Save/Update - google-apps-script

First of all, i am new to Google Workspace and this is the first time i am developing an addon.
I am trying to develop a Google Workspace(gsuite) Addon using Google Apps Script,
My main objectives are:
Whenever i create/save/update an event in Google Calender, i want to make an alert asking for ok and cancel, if i click cancel the event should not be created and if i click ok then the event should be created and the script function should call my API endpoint with the following parameters:
{
summary: summary_of_the_event,
created: event_created_or_updated_date,
googleCalendarId: calender_id
}
Here is the reference to Apps script Google calender:
https://developers.google.com/apps-script/reference/calendar/calendar-app
I appreciate the help.

Some thoughts about calendar add-ons
CalendarApp features the installable calendar trigger with the specification:
Calendar triggers fire when a user's calendar events are updated (created, edited, or deleted).
Installble triggers can be used in Google Workspace add-ons
Howver, this installable trigger only fire after the event has been created / updated, so it can help you to cancel event creation, but rather no manually delete the event a posteriori.
Mind also that the when the trigger fires, it does not give you any information about the created / updated event - you have to manually find this event e.g. by querying for the least created / updated event in user's calendar.
Google Workspace Calendar add-ons feature the manifest trigger for updating calendar events calendar.eventUpdateTrigger
Thereby:
This trigger only fires if the user makes one or more of the following edits:
Adds one or more attendees.
Removes one or more attendees.
Adds or switches to a different conferencing solution.
Google Workspace Calendar add-ons also feature the manifest trigger calendar.eventOpenTrigger
It fires when you open an event (this is what you want!).
However, this work only for already existing events - not when a user opens the Calendar UI interface to create a new event
Instead of letting a user create / update events through the regular Calendar UI, you can use Card Service to create a custom add-on interface where the user can specify that he wants to create an event
Within the card interface, it is easier to implement an ok / cancel functionality (as a card) and then (in case "ok" is clicked) create an event for the user programmatically.

Related

Installable trigger notify without change in the event

We are working on Google Calendar Add-on and need some help regarding Add-on development. We are using the triggers to notify the us whenever there is any create/update/delete happen on the google calendar. So, there is any create/update/delete event that occurred into the user calendar then the trigger should notify our service with data but in our case, it is not working as expected due to which incident occurred. It is giving 1000's of notification without actually changing in the event.
We are using Triggers->Installable Triggers for the notification service.

How to refresh appmaker table on change in calendar event of a calendar(dynamic) on current day?

I have created a app in appmaker. In that there is a table which should get refreshed (i.e a particular appscript function (client-side function)should run to refresh the table) whenever any change in calendar events of a particular calendar on the current day takes place.
How to set such type of trigger?
I did some R&D and found that there is something called watch function in calendar API but I'm not sure if it is useful for my problem.
By table I am referring to appmaker table widget.
If setting the appscript calendar trigger, Client refresh may be a problem. Because in my ui above the table there is a dropdown to select the calendarId which refreshes the table by running the below code:
var calendarId=app.pages.Events.descendants.Dropdown1.value;
google.script.run.withSuccessHandler(function() {
app.datasources.Events.load();
}).getTodayEvents(calendarId);
/* In getTodayEvents() current day events of the calendarId are
fetched from that calendar and stored in DB */
Can this happen from server side through trigger?
Also trigger should be based on the calendarId currently selected in dropdown.
So the calendarId needs to be dynamic in the trigger.
A calendar watch requires an webhook that will process some logic when there is an event modification(create, update, delete). Unfortunately, App Maker does not yet support such a thing and here is a discussion about it Calling App Maker server scripts from outside of App Maker UI. On top of that, you would require to interact with the client side rather than the server, so definitely not.
In this case, the best thing you can do is set an interval that will refresh the ui every certain seconds or minutes.
setInterval(function(){
app.datasources.myDatasource.load();
},60000); //60 seconds
A more advanced way of approaching this would be using firebase and cloud functions. I have implemented firebase on some projects with reliable results. You can read more about firebase and cloud functions on the below links:
https://firebase.google.com/docs/web/setup
https://cloud.google.com/functions/docs/
This may or may not be applicable to your use case but for non-app-maker script projects I'd do the following:
Go to your GSuite Developer Hub via https://script.google.com/home/my
Select the desired script project
On the right is a box titled "Project Details", click the menu icon (top-right) and select "Triggers"
Click the "Add trigger" button (bottom-right)
In the pop-up modal select the function you wish to run, and set the event source to "Calendar".
Note that this "Calendar" trigger can only detect updates to your primary calendar. Changes to secondary calendars you create will not be tracked.

Gmail Add-on trigger criteria

Can someone accurately describe the trigger criteria for Gmail Add-on scripts? Apparently, the trigger is not invoked each time the user navigates between Gmail conversations.
The only documentation I can find is https://developers.google.com/gmail/add-ons/how-tos/building#note1, which states
the only contextual trigger type available is unconditional, which triggers for all emails regardless of content.
I interpreted this to mean that the trigger is invoked every time the user navigates to a different gmail conversation, however, that is not the case:
The first time I navigate to a some Gmail conversation, the add-on trigger fires. And when I navigate to another conversation using the the “Newer” or “Older” angle-bracket button, the trigger is again invoked for the new conversation. But when I navigate back to the first page using an angle bracket button, the add-on trigger does not fire. (Easy enough to show this by displaying a timestamp when each UI card is created.) There seems to be some kind of internal caching going on — is there any way to disable this, or otherwise run my add-on script each time the user navigates between Gmail conversations?
you can use ActionResponseBuilder.setStateChanged(), to clear the cache
link: https://developers.google.com/gmail/add-ons/how-tos/interactions
Currently, Google Apps Script doesn't have triggers for Gmail events so the trigger will not invoke every time, the user navigates to a different Gmail conversation. Whenever a new message open it will invoke trigger, contextual trigger. You can also tryout time-driven trigger which invokes after each time interval. To use time-driven trigger open your project on https://script.google.com/. In Apps Script Editor, navigate to Edit -> All your triggers. If there is no trigger then setUp one and Save. See this example Create Time-driven trigger in Gmail-add on
Refer this link for more information How do I detect when I view an email in gmail with google-apps-script script? I think this might be helpful.

onFormSubmit trigger removes when update G-Suite add-on

I want create a standalone add-on for google form which should handle form-submit event with creation a new text file in google drive with content of the form response. And then distrubute this add-on for other users.
My goal is user can install the add-on on google form and it just should work, without additional setup from add-on menu.
While testing I figured out that there are Simple and Installable triggers are available. (onFormSubmit is an installable trigger)
Installable triggers clear each time when add-on is updated on G-Suite Marketplace. Therefore it removes my onFormSubmit trigger, and because of that the installed add-on stops working after update.
onFormSubmit trigger is added inside onInstall Simple trigger this way:
function onInstall() {
var form = FormApp.getActiveForm();
var trigger = ScriptApp.newTrigger('respondToFormSubmit')
.forForm(form)
.onFormSubmit()
.create();
}
I thought to bypass this issue by handling onFormSubmit inside Simple trigger onEdit of the linked spreadsheet. Actually it could check a spreadsheet for new rows and then create a new file. But Simple triggers are without autorization to private services (like google drive), so it's not possible to create a file on google drive inside a simple trigger.
Is there way to keep onFormSubmit trigger after publish a new version of add-on, without its manual re-install?
Well, these all reasonable, but then why onInstall event is not raised again? Or may be is there an event onUpdate to handle add-on update?
UPDATE
Here I read:
Add-on triggers will stop firing in any of the following situations:
If the add-on is uninstalled by the user
If the add-on is disabled in a document (if it is re-enabled, the trigger will become operational again)
If the developer unpublishes the add-on or submits a broken version to the add-on store
I just set next version number in the publish form, click "Update web store draft", and confirm update in new window. Am I doing everything right?
The trigger stops firing even without code modifications, just with new version of add-on. And there are no any log errors in stackdriver console. Could it be a bug?

Google Apps Script and Trigger when new Calendar Event is added

I want to have a Goggle Apps Script be run when a new Calendar Event is added to a Google Calendar. How can I setup the trigger? I know what I want to script (change color for an event based on the text in the Title). I just don't see where to setup the trigger to run my script.
You can use the new Google Calendar Event Triggers. You can create the code with a script similar to the following to trigger your function:
ScriptApp
.newTrigger('findNewEvents')
.forUserCalendar(Session.getActiveUser().getEmail())
.onEventUpdated()
.create()
Then you can loop through the events in your calendar to find any events recently created and pass them to your function.
Go to https://script.google.com/home/triggers (also available in your script editor, go to Edit -> All your triggers).
Create or edit the trigger for your script.
For event source use "From calendar", put "Calendar updated" to calendar details (see the image below) and specify the calendar owner email.
This feature is not supported yet as of now. You can see that no resolution has been provided in this issue tracker. You will have to rely on your own workaround implementation or open a new feature request.