通过脚本创建新事件时,覆盖现有的Google日历事件 [英] Override existing google calendar event when new event is created via script

查看:80
本文介绍了通过脚本创建新事件时,覆盖现有的Google日历事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google表单根据Google日历上的未结事件安排约会.我已经在日历上划分了时间段,如果事件的标题为"Open",则可以使用该时间段创建事件.接下来,我想知道是否可以使用创建的新约会来覆盖和删除现有的打开事件.

I am using a Google Form to schedule appointments based on open events on my Google Calendar. I've sectioned off time slots on the calendar and if the title of the event is "Open", then the time slot can be used to create an event. I would next like to know if it's possible to override and delete the existing open event with the new appointment that is created.

这是我用来将事件推送到日历中的代码,这些事件以以下形式创建:

Here is the code I am using to push events to my calendar as they are created in the form:

var calendarID = "";
var startDtID = 5;
var endDtID = 5; // Column containing date/time
var nameID = 2; // Column containing name
var emailID = 3; // Column containing email
var phoneID = 4; // Column containing phone
var formTimeStampID = 1; // column containing timestamp

function getLatestAndSubmitToCalendar() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var rows = sheet.getDataRange();
  var numRows = rows.getNumRows();
  var values = rows.getValues();
  var lr = rows.getLastRow();

  var startDt = sheet.getRange(lr,startDtID,1,1).getValue();
  var endDt = sheet.getRange(lr,endDtID,1,1).getValue(); // remove hour and minute for the start/end time
  var desc = "Comments :"+sheet.getRange(lr,phoneID,1,1).getValue(); // set comments as description, add in timestamp and submission
  var title = sheet.getRange(lr,nameID,1,1).getValue()+" - "+sheet.getRange(lr,emailID,1,1).getValue(); // create title using name and email

    createEvent(calendarID,title,startDt,endDt,desc);
}; // run create event function

function createEvent(calendarId,title,startDt,endDt,desc) {
  var cal = CalendarApp.getCalendarById(calendarID);
  var start = new Date(startDt);
  var end = new Date(endDt);

  var event = cal.createEvent(title, start, end, {
      description : desc
  }); // set options for event
};

CalendarEvent类提供了一种通过

The CalendarEvent class has a method to delete events through deleteEvent(), but I am confused as to how I can determine the right event to be deleted. How can I match up the date and time of the current event with that of the one that already exists so I can override it?

推荐答案

您需要先调用calendar.getEvents(),然后循环浏览要删除的事件,然后对.delete()进行调用他们每个人.以下是API链接: https://developers.google.com/apps-script/reference/calendar/calendar#getEvents%28Date,Date,Object%29

You'll need to call calendar.getEvents() first, then loop through the event(s) you want to delete, and call .delete() on each of them. Here's the API link: https://developers.google.com/apps-script/reference/calendar/calendar#getEvents%28Date,Date,Object%29

类似这样的东西:

var now = new Date();
var twoHoursFromNow = new Date(now.getTime() + (2 * 60 * 60 * 1000));
var events = CalendarApp.getDefaultCalendar().getEvents(now, twoHoursFromNow,
    {search: 'meeting'});

for(int i = 0; i < events.length; i++){
    if this is the right event (compare title, times, etc?){
        events[i].deleteEvent();
    }
}

在这种情况下,您可能需要比较说明.还要注意,您可以将选项传递给getEvents调用;您可以在搜索过程中进行过滤的一个参数是搜索参数,就像我上面所做的一样.它将过滤该文本上返回的事件.

In this case, you'll probably want to compare the descriptions. Also note that you can pass options to the getEvents call; one you can use to filter during the search is the search parameter like I did above. It filters the returned events on that text.

理想情况下,您将希望使用以下代码:

Ideally, you'll want to use this: https://developers.google.com/apps-script/reference/calendar/calendar#getEventSeriesById%28String%29

通过ID获取事件;我以假设您没有在任何地方存储ID的方式回答.如果这样做的话,情况会更好,并且可以肯定,您正在替换正确的事件.

That gets the event by the ID; I answered with the assumption you didn't have the ID stored anywhere though. If you do, all the better, and you'll be certain you're replacing the right event.

这篇关于通过脚本创建新事件时,覆盖现有的Google日历事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆