gpt4 book ai didi

javascript - 获取重复事件和非重复事件的单个统一实例

转载 作者:行者123 更新时间:2023-12-03 11:45:20 29 4
gpt4 key购买 nike

我正在尝试获取重复事件的一个统一实例,以及我在该实例中所做的更改以反射(reflect)在该重复事件的所有实例中。

calendar.getEvents 返回重复系列中每个事件的单个实例。即,如果我对该实例进行任何更改(例如删除它),则只有该特定实例会被删除。我尝试使用 getEventSeries()deleteEventSeries() 但这些不会反射(reflect)在数组中,所以我每次删除时都会调用 getEvents 。这是最好的方法吗?或者有替代方案吗?

//The object of the following code is to delete all events in a calendar
function delete_events(calendar) {
var fromDate = new Date("January 1, 1900 00:00:00");
var toDate = new Date("January 1, 2100 00:00:00");
var events = calendar.getEvents(fromDate, toDate);

while (events.length > 0) {
var ev = events[0];
var es = ev.getEventSeries();
es.deleteEventSeries();
Logger.log('Deleted: ' + es.getTitle());

events = calendar.getEvents(fromDate, toDate);
}
}

最佳答案

由于 eventsSeries 具有相同的 ID,您只需记住这些 ID 即可简化流程,并在后续迭代中跳过这些事件。

这是一个简单的测试代码:

function test(){
var cal = CalendarApp.getCalendarsByName('some calendar name you own')[0];
delete_events(cal);
}

function delete_events(cal) {
var fromDate = new Date("January 1, 2014 00:00:00");
var toDate = new Date("January 1, 2100 00:00:00");
var events = cal.getEvents(fromDate, toDate);
var IDs = [];
for(var n in events){
Logger.log(events[n].getTitle());
var id = events[n].getId();
if(IDs.indexOf(id)>-1){continue};
try{
cal.getEventSeriesById(id).deleteEventSeries();
IDs.push(id);
Logger.log(events[n].getTitle()+' was deleted, ID = '+events[n].getId());
}catch(err){Logger.log(events[n].getTitle()+' on '+Utilities.formatDate(events[n].getStartTime(),cal.getTimeZone(),'yyyy/MMM/dd')+" was not deleted,you don't have the right to modify it");}
}
}

关于javascript - 获取重复事件和非重复事件的单个统一实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26081968/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com