Google Apps Script and Trigger when new Calendar Event is added - google-apps-script

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.

Related

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

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.

How to make Custom Apps Script Function trigger on edit?

So I created a Custom Google Apps Script project, and then deployed it as an add-on (for my domain only). However I soon realized that I need to change the trigger to be only onEdit. However that option isn't there for this Apps Script project. How do I get around this? In the picture there is only time-based trigger and calendar trigger. While for a script that's not deployed as an add-on there is an option to make it trigger onEdit
By using the "add trigger" button on the project triggers page we could only create on edit triggers for Bound to spreadsheet (Google Sheets) projects.
The alternative is to create the trigger by using code. For details please checkout https://developers.google.com/apps-script/guides/triggers/installable
it's worthy to note that the documentation in https://developers.google.com/apps-script/overview on the Types of Scripts lists three
Standalone
Bound to G Suited Documents
Web Apps
In this case the screenshot that is included in the question corresponds to a standalone project.

Google Calendar Trigger Does Not Fire

The objective is to transfer data from Google Calendar to a Google Sheet whenever the calendar is edited. For this we need a script that transfers the data, and need to set up the Calendar trigger.
Thanks to Oleg Valter and his excellent and exhaustive explanation how to create the script on this page:
Google calendar events to Google spreadsheet automatic refresh onEdit
the data transfer already works fine when the script is started from the Google sheets. I use a modified version of Oleg's script, which works well, and I am not sure if it would make sense to re-post it here (since that is not the problem).
I have tried to create the trigger with Oleg's funtion first:
/**
* Installs Calendar trigger;
*/
function calendarTrigger() {
var trigger = ScriptApp.newTrigger('callback name here')
.forUserCalendar('calendar owners email here')
.onEventUpdated()
.create();
}
but the trigger did not fire when I have added a new event on the calendar. Checked the triggers connected to the project, and the trigger created by the above function was there. Checked the Executions and nothing showed up.
Then I have manually deleted the trigger, and manually crated a new one following the instructions under "Managing triggers manually" section of this page:
https://developers.google.com/apps-script/guides/triggers/installable#google_apps_triggers
The trigger was created again, which basically looked the same as the one created by the above function, but it still did not fire when tested.
Now I am stuck and have no idea what else to try. It supposed to work, but it is possible that I have missed setting up something else that might be required for this to work.
I am using a simple Google account connected to a single email address (not a business suite). Never had any problems with the other triggers related to the sheets before that I could not resolve (sometimes the platform refuses to work as it should, but then later things get back to normal).
I would highly appreciate any advice about what else to try, or if you know what the problem is, even better.
Thanks in advance,
Zoltan
Well, after some trial and error based on ziganotschka's kind answer (thanks again) found the problem. It is caused by a bit of confusion about what the
.forUserCalendar('calendar owners email here') and the
Calendar ID supposed to be.
On my Calendar account there are several calendars, but two are of interest for us here:
"Main Calendar" which is the default calendar associated with the account. If everything else is deleted, then this will remain.
another calendar that I have added for the very purpose to be connected to the spreadsheet, and named it "Auto Billing" (only this is checked and displayed).
The Main Calendar has this on the Settings page (changed my email address):
Integrate calendar
Calendar ID
myAddress#yahoo.com
The Auto Billing calendar has this on the
Settings page (changed the ID):
Integrate calendar
Calendar ID
svj8tu1o458a9s1488xxxxxxxx#group.calendar.google.com
My intent was to use a custom created calendar for this purpose instead of using the default Main Calendar, because this might be full with all kinds of events.
Therefore when the trigger is created manually
svj8tu1o458a9s1488xxxxxxxx#group.calendar.google.com
should be added in the field "Calendar owner email" instead of the calendar owner's email address.
When the trigger is created from script using the above snippet, then
.forUserCalendar("svj8tu1o458a9s1488xxxxxxxx#group.calendar.google.com")
should be used.
In the script that transfers the data from the Calendar to the sheet the
var Calendar = CalendarApp.getCalendarById("svj8tu1o458a9s1488xxxxxxxx#group.calendar.google.com");
should be used. This way the trigger will work when events are edited on the Auto Billing calendar.
Thanks for the guidance Ziganotschka, and also big thanks to Oleg for the original script. Hope this debugging will help others as well.
Google Calendar trigger is a feature for the Google Calendar
It does not work for "myaddress#yahoo.com" calendars

Triger a function on a change in doc

I want to run a Google App Script function every time a change is made to my Google Doc. I have found onChange and onEdit triggers which are only available for Google Spreadsheet. I am sure there must be an edit/change trigger for Google Doc as well.
Unfortunately there are no onEdit(), onChange() triggers for Google Docs.
The triggers available for Google Docs are the following:
onOpen() triggers both simple and installable;
time driven triggers;
onInstall() simple triggers.
What you can do instead is to use a time driven trigger so in this way even though the changes are not directly triggering the execution, the trigger will still run how often you want it to.
Last but not least, you can file a Feature Request on Issue Tracker where you specify the details needed.
Reference
Apps Script Trigger;
Google Issue Tracker.

Google Script How to make time based trigger on form

I have recently taken up on writing in Google Apps Script.
Currently I have created a function that is "attached" or "linked to" a Google Form. I did this by going to the editing screen for my form and accessing the script editor from there.
The code creates a new spreadsheet and links the form to that spreadsheet. The reason for this is that every week I would like to create a new spreadsheet for the form to submit to.
The issue I am running into is that when I try to manually make a trigger I do not see the option for it. The steps I've taken are as follows Resources -> Current Project's Triggers -> Click to add a new one.
The only options I see are "on form submit" and "on open"
Is there a way to programmatically add a time based trigger?
There is a way for creating the time based trigger. When you want to create a trigger at Feb. 1, 2017, 18:00 using GAS, you can do that by following script. 'test' is a function you want to execute.
var tr = new Date(2017,2-1,1,18,00);
ScriptApp.newTrigger('test').timeBased().at(tr).create();
Reference URL is https://developers.google.com/apps-script/reference/script/script-app#newTrigger(String).