下面的代码使用Google脚本在中创建一个日历事件。
function createCalendarEvent(){
let adoptionCalendar = CalendarApp.getCalendarById("a16cod711vq2qn7gcd6tcua9i4@group.calendar.google.com");
adoptionCalendar.createEvent('Alabama', new Date('August 20, 2022 00:00:00 GMT-7'), new Date('August 20, 2022 23:59:59 GMT-7'));
}每次都是。我运行代码,它会在我的中创建和添加事件。如果事件已经存在,如何首先进行检查。如果事件已经存在,我们不应该再添加事件。
另一种解决方案是删除我的日历中的所有事件,并再次写入我的所有事件。谢谢。
发布于 2022-08-20 16:29:48
如果现有事件不存在,您可以搜索它,如果它不存在,它将创建一个事件。否则它什么也做不了。小心日期。
我第一次运行这个程序时,我看到的是“创建”。如果我再做一次我就不做了。
我从另一个帖子中得到了搜索选项,所以我无法证明它们。
function addEvent() {
// Explanation of how the search section works (as it is NOT quite like most things Google) as part of the getEvents function:
// {search: 'word1'} Search for events with word1
// {search: '-word1'} Search for events without word1
// {search: 'word1 word2'} Search for events with word2 ONLY
// {search: 'word1-word2'} Search for events with ????
// {search: 'word1 -word2'} Search for events without word2
// {search: 'word1+word2'} Search for events with word1 AND word2
// {search: 'word1+-word2'} Search for events with word1 AND without word2
// {search: '-word1+-word2'} Search for events without word1 AND without word2 (I find this to work logically like an 'OR' statement)
try {
let calendar = CalendarApp.getDefaultCalendar();
let events = calendar.getEvents( new Date('August 20, 2022 00:00:00 GMT-7'), new Date('August 20, 2022 23:59:59 GMT-7'), { search: 'Alabama'} );
if( events.length === 0 ) {
calendar.createEvent('Alabama', new Date('August 20, 2022 00:00:00 GMT-7'), new Date('August 20, 2022 23:59:59 GMT-7'));
console.log("created");
}
}
catch(err) {
console.log(err);
}
}参考文献
https://stackoverflow.com/questions/73427751
复制相似问题