我正在尝试设置日历事件从日程安排我在谷歌表谷歌日历,以避免手动做它。
我开发了一个代码,它给我错误信息
这是我的表单的样子
function scheduleShifts() {
var spreadsheet = SpreadsheetApp.getActiveSheet();
var calendar_ID = spreadsheet.getRange("A1").getValue();
var eventCal = CalendarApp.getCalendarById(calendar_ID);
var events = spreadsheet.getRange("H2:I996").getValues();
for (x=0; x<events.length; x++) {
var schedule = events[x];
var startTime = schedule[7];
var endTime = schedule[8];
var name = schedule[2];
var address = schedule [3];
var phone = schedule [4];
var description = schedule[5];
var comments = schedule[6];
eventCal.createEvent(address, startTime, endTime);
}
}
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Sync to Calendar')
.addItem('Schedule shifts now', 'scheduleShifts')
.addToUi();
}
试试这个:
function scheduleShifts() {
var sh = SpreadsheetApp.getActiveSheet();
var cid = sh.getRange("A1").getValue();
var cal = CalendarApp.getCalendarById(cid);
var evs = sh.getRange("A2:I996").getValues();
for (x = 0; x < evs.length; x++) {
try {
var ev = evs[x];
var startTime = new Date(ev[7]);
var endTime = new Date(ev[8]);
var address = ev[3];
cal.createEvent(address, startTime, endTime);
}
catch(e){console.error(e); continue;}
}
}
当前您没有保留一个列来显示事件已创建,因此如果您多次运行此操作,您将创建重复的事件。你需要仔细考虑一下。