我创建了一个小的 Google 工作表,可以生成幻灯片和日历事件。 首页三页
在对现有的 jscript 进行了大量修改之后,我让它工作了,但是调用日历的一些事情让我感到困惑:
我的第一个脚本基于 CreateOneEvent.gs,具有以下内容
//Retrieve the value of the calendar ID from the cell that it lives in.
//Call CalendarApp service to open the calendar
var calendarId = spreadsheet.getRange("A1").getValue();
var eventCal = CalendarApp.getCalendarById(calendarId);
我的第二个脚本基于 BulkDeleteEvents.gs 使用这段代码:
//Retrieve the value of the calendar name from the cell that it lives in.
var calendarName = spreadsheet.getRange("D1").getValue();
//Call CalendarApp service to open the calendar
var calendar = CalendarApp.getCalendarsByName(calendarName)[0];
// Delete any pre-existing events with matching start date&time and end date&time
var events = calendar.getEvents(fromDate,toDate);
for(var i=0; i<events.length;i++){
var ev = events[i];
ev.deleteEvent();
}`
我尝试修改第二个脚本,以便它也可以通过calendarID检索并调用日历,
var calendar = CalendarApp.getCalendarByID(calendarID)[0];
,但其余代码拒绝运行。
我最终放弃并使用每个脚本的原始编码,但我想知道这两种方法
getCalendarByID
和 getCalendarsByName
是如何不兼容的。
如果有帮助,这里是两个脚本的全部优点:
function CreateOneEvent() {
//Calendar must be created in advance. Once created go to Settings and scroll down to Calender ID.
//Copy and paste ID into <Calendar ID> cell of main worksheet
// Delete any pre-existing duplicate events with the function BulkDeleteEvents
BulkDeleteEvents()
//Retrieve information from the spreadsheet
// Make the 3rd sheet active in the active spreadsheet.
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[2]);
//Retrieve the value of the calendar ID from the cell that it lives in.
//Call CalendarApp service to open the calendar
var calendarId = spreadsheet.getRange("A1").getValue();
var eventCal = CalendarApp.getCalendarById(calendarId);
//Select data from the spreadheet
var title = spreadsheet.getRange("A3").getValue();
var startTime = spreadsheet.getRange("B3").getValue();
var endTime = spreadsheet.getRange("C3").getValue();
var session = spreadsheet.getRange("D3").getValue();
var what = spreadsheet.getRange("E3").getValue();
//create event with data selected
eventCal.createEvent(title, startTime, endTime,{location: session,description: what});
}
function BulkDeleteEvents() {
//Calendar must be created in advance.
//Copy and paste the calendar name into <Calendar Name> cell of main worksheet
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[2]);
//Retrieve the value of the calendar name from the cell that it lives in.
//Retrieve the value of the event start date&time.
//Retrieve the value of the event end date&time.
var calendarName = spreadsheet.getRange("D1").getValue();
var fromDate = spreadsheet.getRange("B3").getValue();
var toDate = spreadsheet.getRange("C3").getValue();
//Call CalendarApp service to open the calendar
var calendar = CalendarApp.getCalendarsByName(calendarName)[0];
// Delete any pre-existing events with matching start date&time and end date&time
var events = calendar.getEvents(fromDate,toDate);
for(var i=0; i<events.length;i++){
var ev = events[i];
ev.deleteEvent();
}
}
尝试 var calendar = CalendarApp.getCalendarByID(calendarID)
它返回一个日历而不是日历数组