usernametaken
usernametaken

Reputation: 69

How to add a unique Google Meet link for every event creation (within a specific if statement) using Google Apps Script

var event_title = "";
var desc = "";
var today = new Date();
var thirtyMinutes = new Date(today);
thirtyMinutes.setMinutes(today.getMinutes() + 30);

var event = calendar.createEvent(event_title, today, thirtyMinutes, { description: desc }).setVisibility(CalendarApp.Visibility.PRIVATE).setColor("11").addPopupReminder(10);



 var example_description = 'example description';

                    if (interview_type == 'Example Interview') {
                        event.setTitle('Example Title');
                        event.setDescription(example_description + "");
                    }

How do I add a newly generated Google Meet link for each event that is generated ONLY for the if statement that is nested within:

if (interview_type == 'Example Interview') {
event.setTitle('Example Title');
event.setDescription(example_description + "");
}

I found this solution in another answer but I can't seem to configure it for my use case where it doesn't disrupt the current event creation settings that I have set up and to make it generate a new one for every single event creation, as well as to keep it within that if statement that triggers a specific type of event creation:

var event = {
  "summary": summary,
 "start": {
   "dateTime": start.toISOString()
     },
  "end": {
    "dateTime": end.toISOString()
      },
       "conferenceData": {
          "createRequest": {
           "conferenceSolutionKey": {
             "type": "hangoutsMeet"
         },   
           "requestId": id
          }
     }
  };

 event = Calendar.Events.insert(event, 'primary', {
   "conferenceDataVersion": 1});
 return event.hangoutLink;

Thank you

Upvotes: 0

Views: 990

Answers (1)

Kristkun
Kristkun

Reputation: 5963

You can refer to this sample code:

function createEvent() {
  var calendarId = 'c_9cdqeqqluk7vsessartxxxxxx';

  var calendar = CalendarApp.getCalendarById(calendarId);
  var interview_type = 'Example Interview';
  var event_title = "";
  var desc = "";
  var today = new Date();
  var thirtyMinutes = new Date(today);
  thirtyMinutes.setMinutes(today.getMinutes() + 30);

  var event = calendar.createEvent(event_title, today, thirtyMinutes, { description: desc })
    .setVisibility(CalendarApp.Visibility.PRIVATE).setColor("11").addPopupReminder(10);
  Logger.log('Event ID: ' + event.getId());

  var example_description = 'example description';

  if (interview_type == 'Example Interview') {
    event.setTitle('Example Title');
    event.setDescription(example_description + "");

    //Add Meet Conference
    var tmpEvent = {
      conferenceData:{
        createRequest:{
          conferenceSolutionKey:{
            type: "hangoutsMeet"
          },
          requestId: charIdGenerator()
        }
      }
    }

    //remove @google.com from the iCalUID of the event
    var eventId  = event.getId().replace("@google.com","");
    
    Logger.log(eventId);
    Calendar.Events.patch(tmpEvent,calendarId,eventId,{conferenceDataVersion:1});
  }

}

function charIdGenerator()
 {
     var charId  ="";
       for (var i = 1; i < 10 ; i++) 
       { 
           charId += String.fromCharCode(97 + Math.random()*10);
       } 
     //Logger.log(charId)
     return charId;    
 } 

Pre-requisite:

You need to enable Advanced Calendar Service in Apps Script,

To enable advanced services:

enter image description here

Select Google Calendar API in Step 4.


What it does?

  1. When the interview_type is set to 'Example Interview', We will create an event resource having a createRequest in its body. It will require 2 inputs:
  • conferenceSolutionKey type which is set to hangoutsMeet
  • requestId which is a client-generated unique ID for this request. Clients should regenerate this ID for every new request. If an ID provided is the same as for the previous request, the request is ignored.

Note:

I just copied some function I found online that generates a random string, and used that string as the requestId

  1. Once the event resource was created, We need to get the event id by removing the "@google.com" sub-string in the iCalUID returned by CalendarEvent.getId() method.

Note:

CalendarEvent.getId() gets the unique iCalUID of the event. Note that the iCalUID and the event id used by the Calendar v3 API and Calendar advanced service are not identical and cannot be used interchangebly.

  1. Lastly, we need to use Calendar.Events.patch(resource: Calendar_v3.Calendar.V3.Schema.Event, calendarId: string, eventId: string, optionalArgs: Object): to update our newly created event.

OUTPUT:

enter image description here

Upvotes: 1

Related Questions