我正在编写一个脚本,以便在一个月内从每个日历中删除每个事件。我的代码:
function myFunction() {
var year = 2018;
var month = 11;
var fromDate = new Date(year,month,1,0,0,0);
var toDate = new Date(year,month,28,0,0,0);
var calendars = ['cal1', 'cal2', 'cal3','cal4','cal5','cal6'];
for (var x = 0; x < calendars.length; x++) {
var calendar = CalendarApp.getCalendarsByName(calendars[x])[0];
var events = calendar.getEvents(fromDate, toDate);
for(var i = 0; i < events.length; i++){
var ev = events[i];
ev.deleteEvent();
}
}
}
运行代码时,出现以下错误:
TypeError:无法调用未定义的方法“ getEvents”。 (第13行,文件 “代码”)
我不是在日历对象上运行getEvents()吗?为什么这段代码尝试使用该函数时会给我一个错误?
答案 0 :(得分:0)
尝试一下:
我删除了deleteEvents,因为我不想删除事件。但是这段代码为给定时间段内的所有事件生成了日历名称,事件标题,事件开始时间和事件结束时间。
function myFunction() {
var year = 2018;
var month = 11;
var fromDate = new Date(year,month,1,0,0,0);
var toDate = new Date(year,month,28,0,0,0);
var calendars = ['You'll have to put your calendar names back in here.']
var html='';
for (var x=0;x<calendars.length;x++) {
var calendar = CalendarApp.getCalendarsByName(calendars[x]);
var events = calendar[0].getEvents(fromDate, toDate);
for(var i = 0; i < events.length; i++){
var ev = events[i];
html+=Utilities.formatString('<br />Calendar Name: %s<br />EventTitle: %s<br />EventStartTime: %s<br />Event End Time: %s',calendar[0].getName(),events[i].getTitle(),events[i].getStartTime(),events[i].getEndTime() );
}
}
var userInterface=HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Calendar Events')
}