如何使用 Google Apps 脚本为每个事件创建(在特定的 if 语句中)添加唯一的 Google Meet link

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 + "");
                    }

如何为每个事件添加新生成的 Google Meet link,该事件仅为嵌套在以下内容的 if 语句生成:

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

我在另一个答案中找到了这个解决方案,但我似乎无法为我的用例配置它,因为它不会破坏我设置的当前事件创建设置并使其为每个事件创建,以及将其保存在触发特定类型事件创建的 if 语句中:

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;

谢谢

可以参考这个示例代码:

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

先决条件:

您需要在 Apps 脚本中启用 Advanced Calendar Service

enable advanced services

Select Google Calendar API in Step 4.


它有什么作用?

  1. interview_type 设置为 'Example Interview' 时,我们将创建一个 event resource 并在其主体中包含 createRequest。它将需要 2 个输入:
  • conferenceSolutionKey type 设置为 hangoutsMeet
  • requestId 这是 客户端为此请求生成的唯一 ID 。客户端应为每个新请求重新生成此 ID。如果提供的 ID 与上一个请求相同,则该请求将被忽略。

Note:

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

  1. 创建事件资源后,我们需要通过删除 CalendarEvent.getId() 方法返回的 iCalUID 中的 "@google.com" 子字符串来获取事件 ID。

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. 最后,我们需要使用Calendar.Events.patch(resource: Calendar_v3.Calendar.V3.Schema.Event, calendarId: string, eventId: string, optionalArgs: Object):更新我们新创建的事件。

输出: