如何使用 Google Apps 脚本在 if 语句中创建事件创建函数 运行 两次?

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

我有一个创建事件的函数。 我有一个 if 语句,如果为真,则为该事件设置特定的标题和描述(等)。

我如何做到这一点,以便当特定的“if 语句”为真时,它会一次产生两个事件,而不是一个事件?

而且这样的话,是不是也可以设置完全一样的Google遇见link? 更重要的是,它们可以有不同的描述吗?

(因此它创建了两个具有相同 Google 会议 link,不同描述的事件)

如何编写 if 语句来生成两个单独的事件?

此外,一旦创建了一个事件,它就会在电子表格中的一个公式中被标记下来。请参阅代码的“event_link”部分。

我们如何制作它以便它为特定的 if 语句标记两个事件 links:

Invite 1 / Invite 2

(它目前将“查看邀请”和 link 标记为单个事件 link)

谢谢!

可以参考这个示例代码:

function createEvent() {
  var calendarId = 'c_9cdqeqqluk7vsesxxxxxx@group.calendar.google.com';

  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.


它有什么作用?

  1. 创建初始活动后,我们将使用 Calendar.Events.patch(resource: Calendar_v3.Calendar.V3.Schema.Event, calendarId: string, eventId: string, optionalArgs: Object). This will return an event resource 创建一个新的 Google Meet 会议,其中包括我们将复制到第二个活动的 conferenceData
  2. 使用创建的第一个事件的 eventIdRow 1 - Column 13 中创建超链接。
  3. 检查interview_type是否为Another Example Interview,设置第一个事件titledescription
  4. 创建第二个事件,并设置与第一个事件相同的 title 和不同的 description
  5. 创建包含 conferenceData 的事件资源请求正文。根据第一个活动的 conferenceData 设置 conferenceData 以复制其 Google 会议会议。使用 Calendar.Events.patch(resource: Calendar_v3.Calendar.V3.Schema.Event, calendarId: string, eventId: string, optionalArgs: Object).
  6. 更新事件
  7. 根据创建的第二个事件添加另一个超链接。使用 Range.offset(rowOffset, columnOffset) 在当前范围内包含 1 列偏移量,然后设置其公式。

输出


附加信息:

我尝试连接 2 个超链接公式,但输出只是一个字符串。它无法在单个单元格中包含 2 个不同的超链接。这就是为什么我将第二个邀请设置为下一栏

示例: