我希望能够将在Google日历中创建的新事件发送到Google表格,并将在Google表格中创建的新事件(行)发送到Google日历。
不想在Google日历或Google表格中创建重复的事件。
我有单独的程序可以在两个单独的Google表格中完成这些任务。两种功能均可独立工作,但我想将它们合并并使用一个Google表格工作。使用下面的代码,将删除我的工作表标题,并且无限次数地重新创建同一事件。 日历ID在单元格J2中。
Google表格标题如下:
function getEvents()
{
var sheet = SpreadsheetApp.getActiveSheet()
var headerRows = 1;
var range = sheet.getDataRange();
var data = range.getValues()
var calId = sheet.getRange("J2").getValue();
var cal = CalendarApp.getCalendarById(calId);
var events = cal.getEvents(new Date("9/1/2019 12:00 AM"), new Date("1/1/2090 11:59 PM"));
for(var i=0; i<events.length; i++){
var date = events[i].getStartTime();
var title = events[i].getTitle();
var tstart = events[i].getStartTime();
var tstop = events[i].getEndTime();
var loc = events[i].getLocation();
var id = events[i].getId();
for (var j=0; i<data.length; j++) {
if (j < headerRows) continue;
var row = data[j];
sheet.getRange(j, 2).setValue(title);
sheet.getRange(j, 3).setValue(tstart);
sheet.getRange(j, 4).setValue(tstop);
sheet.getRange(j, 5).setValue(loc);
sheet.getRange(j, 6).setValue(id);
}
}
}
function exportEvents() {
var sheet = SpreadsheetApp.getActiveSheet();
var headerRows = 1; // Number of rows of header info (to skip)
var range = sheet.getDataRange();
var data = range.getValues();
var calId = sheet.getRange("J2").getValue();
var cal = CalendarApp.getCalendarById(calId);
for (i=0; i<data.length; i++) {
if (i < headerRows) continue; // Skip header row(s)
var row = data[i];
var date = new Date(row[1]); // First column
var title = row[2]; // Second column
var tstart = new Date(row[3]);
tstart.setDate(date.getDate());
tstart.setMonth(date.getMonth());
tstart.setYear(date.getYear());
var tstop = new Date(row[4]);
tstop.setDate(date.getDate());
tstop.setMonth(date.getMonth());
tstop.setYear(date.getYear());
var loc = row[5];
var id = row[6]; // Sixth column == eventId
// Check if event already exists, update it if it does
try {
var event = cal.getEventSeriesById(id);
}
catch (e) {
// do nothing - we just want to avoid the exception when event doesn't exist
}
if (!event) {
//cal.createEvent(title, new Date("March 3, 2010 08:00:00"), new Date("March 3, 2010 09:00:00"), {description:desc,location:loc});
var newEvent = cal.createEvent(title, tstart, tstop, {location:loc}).getId();
row[6] = newEvent; // Update the data array with event ID
}
else {
event.setTitle(title);
event.setLocation(loc);
// event.setTime(tstart, tstop); // cannot setTime on eventSeries.
// ... but we CAN set recurrence!
var recurrence = CalendarApp.newRecurrence().addDailyRule().times(1);
event.setRecurrence(recurrence, tstart, tstop);
}
debugger;
}
// Record all event IDs to spreadsheet
range.setValues(data);
}
代码应在创建的每个事件中(在Google表格或Google日历中)在Google表格中输出1行和1个Google日历约会。