Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save AesisGit/3a5554e1509db97d521d345174934afd to your computer and use it in GitHub Desktop.

Select an option

Save AesisGit/3a5554e1509db97d521d345174934afd to your computer and use it in GitHub Desktop.

Revisions

  1. @phillypb phillypb created this gist Jan 5, 2021.
    133 changes: 133 additions & 0 deletions Create a Google Calendar Event via the Calendar API.gs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,133 @@
    /*
    Function to create a Calendar Event via the API and include Google Meet.
    Enable the API via: Services > Google Calendar API > Add
    https://developers.google.com/calendar/v3/reference/events/insert#parameterss
    */



    function createEvent() {

    // ID of the Calendar to create events in
    var calendarID = 'your email address here';

    // create title for the event
    var summary = "My Test Event";

    // create event description
    var description = "Welcome to my Event created via the Calendar API";

    // create event location
    var location = "Narnia";


    // create the date/time for the event ********************************
    // specify the TimeZone
    var timeZone = "Europe/London";

    // create start date/time
    var startDate = "2021-04-03T13:00:00.000";

    // format for the API
    var start = {
    dateTime: startDate,
    timeZone: timeZone
    };


    // create end date/time
    var endDate = "2021-04-03T14:00:00.000";

    // format for the API
    var end = {
    dateTime: endDate,
    timeZone: timeZone
    };
    // create the date/time for the event ********************************


    // add Google Meet conferencing - delete this section if you do not require
    var conferenceData = {
    createRequest: {
    requestId: "sample123",
    conferenceSolutionKey: {
    type: "hangoutsMeet"
    },
    }
    };

    // add attendees - repeat as necessary to add more
    var attendees = [
    {email: "[email protected]"},
    {email: "[email protected]"}
    ];

    /*
    set option for sending invite emails to attendees
    'all', 'externalOnly' or 'none' accepted
    */
    var sendUpdates = "all";

    // set option for guests inviting others
    var guestsCanInviteOthers = false;

    // set option for guests can modify event
    var guestsCanModify = true;

    // set option for guests seeing others
    var guestsCanSeeOtherGuests = false;

    /*
    set option for Show Me As
    'transparent' = Available
    'opaque' = Busy
    */
    var transparency = "opaque";

    /*
    set option for event visibility
    "default" - Uses the default visibility for events on the calendar.
    "public" - The event is public and event details are visible to all readers of the calendar.
    "private" - The event is private and only event attendees may view event details.
    */
    var visibility = "default";


    // compile all of the above options for the event
    var resource = {
    summary,
    description,
    start,
    end,
    conferenceData,
    attendees,
    location,
    guestsCanInviteOthers,
    guestsCanModify,
    guestsCanSeeOtherGuests,
    transparency,
    visibility
    };

    // create arguments to allow event modifications: conferencing, email guests
    var args = {
    conferenceDataVersion: 1,
    sendUpdates
    };


    // make call to Calendar API to create event
    var request = Calendar.Events.insert(resource, calendarID, args);

    // capture event ID and log it
    var eventID = request.id;
    Logger.log('Created Event: ' + eventID);

    // capture event HTML link and log it
    var eventHTML = request.htmlLink;
    Logger.log('Event HTML Link is: ' + eventHTML);


    }