usernametaken
usernametaken

Reputation: 69

How to make an event creation function run twice within an if statement with Google Apps Script?

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 event_link = sheet.getRange(index, 13); // "Event Link" (automatically hyperlinks the invite link here)

    var splitEventId = event.getId().split('@');
    var url = "https://www.google.com/calendar/u/0/r/eventedit/" + Utilities.base64Encode(splitEventId[0] + " " + calendarID).replace("==", '');
    event_link.setFormula('=HYPERLINK("' + url + '","View Invite")');



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

    var eventId  = event.getId().replace("@google.com","");

  var example_description = 'example description';

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


  var another_example_description = 'another example description';

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

    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;    
 } 

I have a function that creates an event. I have an if statement, if true, that sets a specific title and description (etc.) for that event.

How do I make it so that when that specific "if statement" is true, it makes TWO events in one go as opposed to just one?

And in that case, can it also set the exact same Google Meet link? And more so, can they both have different descriptions?

(So it's creating two events with same Google Meet link, different descriptions)

How can I write that if statement to make two separate events?

Also, once an event is created, it's marked down in the spreadsheet within a formula. See the "event_link" part of the code.

How do we make it so that it marks BOTH event links for that specific if statement like this:

Invite 1 / Invite 2

(It currently marks "View Invite" and links to the single event link)

Thanks!

Upvotes: 0

Views: 102

Answers (1)

Kristkun
Kristkun

Reputation: 5953

You can refer to this sample code:

function createEvent() {
  var calendarId = '[email protected]';

  var calendar = CalendarApp.getCalendarById(calendarId);
  var interview_type = 'Another 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());

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

  var eventId  = event.getId().replace("@google.com","");
  Logger.log(eventId);
  eventResource = Calendar.Events.patch(tmpEvent,calendarId,eventId,{conferenceDataVersion:1});
  Logger.log(eventResource.conferenceData);

  //Add event link
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var index = 1; //temporary data
  var event_link = sheet.getRange(index, 13);
  var url = "https://www.google.com/calendar/u/0/r/eventedit/" + Utilities.base64Encode(eventId + " " + calendarId).replace("==", '');
  event_link.setFormula('=HYPERLINK("' + url + '","Invite 1")');


  var example_description = 'example description';

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

  var another_example_description = 'another example description';

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

    //create additional event
    var event2 = calendar.createEvent(event_title, today, thirtyMinutes, { description: desc })
    .setVisibility(CalendarApp.Visibility.PRIVATE).setColor("11").addPopupReminder(10);
    var event2Id  = event2.getId().replace("@google.com","");
    event2.setTitle('Another Example Title');
    event2.setDescription("EVENT 2");

    
    //Add same Google Meet link
    var tmpEvent2 = {
      conferenceData: eventResource.conferenceData
    }
    Calendar.Events.patch(tmpEvent2,calendarId,event2Id,{conferenceDataVersion:1});

    //Add Meeting link on the adjacent column
    var url2 = "https://www.google.com/calendar/u/0/r/eventedit/" + Utilities.base64Encode(event2Id + " " + calendarId).replace("==", '');
    Logger.log(url2);
    event_link.offset(0,1).setFormula('=HYPERLINK("' + url2 + '","Invite 2")');
  }

}

Note:

  1. I re-organized the procedures done. I moved the creation of the Google Meet conference first before checking for if conditions

  2. I supplied some missing required syntax for creating hyperlinks. sheet and index variables were not defined. I just used a temporary value for index which is 1

  3. In this sample code, 2 events will be created when the interview_type is Another Example Interview with the same Google Meet link.


What it does?

  1. After creating an initial event, we will create a new Google Meet conference using Calendar.Events.patch(resource: Calendar_v3.Calendar.V3.Schema.Event, calendarId: string, eventId: string, optionalArgs: Object). This will return an event resource which includes the conferenceData that we will copy to our second event.
  2. Create a hyperlink in Row 1 - Column 13, using the eventId of the first event created.
  3. Check if the interview_type is Another Example Interview, Set the first event title and description.
  4. Create a second event, and set the same title with different description from the first event.
  5. Create an event resource request body containing conferenceData. Set the conferenceData based on the first event's conferenceData to copy its Google Meet conference. Update the event using Calendar.Events.patch(resource: Calendar_v3.Calendar.V3.Schema.Event, calendarId: string, eventId: string, optionalArgs: Object).
  6. Add another hyperlink based on the second event created. Include 1 column offset in the current range using Range.offset(rowOffset, columnOffset) then set its formula.

Output

enter image description here

enter image description here

enter image description here

enter image description here


Additional Info:

I tried concatenating 2 hyperlink formula, but the output is just a string. It was not able to include 2 different hyperlink in a single cell. That is why I set the second invite to the next column

Example:

enter image description here

Upvotes: 1

Related Questions