我正在尝试使用假期日历应用程序脚本,但我只想包含全天事件,无论人们使用的是默认事件还是 OOO 事件。这里有一个类似的问题:Ignore not All-Day events in the Google App Script Vacation Calendar但此答案仅适用于默认事件,并且所有 OOO 类型事件都被过滤掉
Calendar.Events.list() 返回的 Event 类似乎不包含此信息,但根据我更改的另一个线程以检索 CalendarEvent 对象,并且如果日历事件是默认事件类型,则它可以正常工作。但是,如果该事件是 outOfOffice 事件,那么即使是这样,所有内容都会全天返回为 not,这意味着我的 OOO 事件都不会被添加。有谁知道如何让它正确读取全天与定时的 OOO 事件?
相关代码是这个
shouldImportEvent
函数:
function shouldImportEvent(user, keyword, event) {
// Filters out events where the keyword did not appear in the summary
// (that is, the keyword appeared in a different field, and are thus
// is not likely to be relevant).
if (event.summary.toLowerCase().indexOf(keyword) < 0) {
return false;
}
// Only import events that are all day
let calEvent = CalendarApp.getCalendarById(user.getEmail()).getEventById(event.id)
if (!calEvent.isAllDayEvent()) {
// all outOfOffice events are returning here
return false;
}
if (!event.organizer || event.organizer.email == user.getEmail()) {
// If the user is the creator of the event, always imports it.
return true;
}
// Only imports events the user has accepted.
if (!event.attendees) return false;
let matching = event.attendees.filter(function(attendee) {
return attendee.self;
});
return matching.length > 0 && matching[0].responseStatus == 'accepted';
}
不如我希望的那么干净,但我最终使用了开始和结束时间并检查了持续时间,效果很好:
// Only import events that are all day
let calEvent = CalendarApp.getCalendarById(user.getEmail()).getEventById(event.id);
let durationHours = (calEvent.getEndTime() - calEvent.getStartTime()) / 1000 / 60 / 60;
if (durationHours < 7) {
return false;
}