Updated Google Calendar events from Sheets

若如初见. 提交于 2019-12-13 11:51:49

问题


I am trying to sync a Google Calendar to a Sheet to allow me to easily mass update calendar events from within the sheet, then push all the changes back to the calendar.

I'm able to download all the events into my Sheet, specifically using this bit of code:

    // Loop through all calendar events found and write them out starting on calulated ROW 2 (i+2)
for (var i=0;i<events.length;i++) {
var row=i+2;
var myformula_placeholder = '';
// Matching the "header=" entry above, this is the detailed row entry "details=", and must match the number of entries of the GetRange entry below
// NOTE: I've had problems with the getVisibility for some older events not having a value, so I've had do add in some NULL text to make sure it does not error
var details=[[events[i].getTitle(), events[i].getStartTime(), events[i].getEndTime(), events[i].getId()]];
var range=sheet.getRange(row,1,1,4);
range.setValues(details);

I'm able to create NEW events onto my calendar from the downloaded data using this code:

     function caltest1() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = 2;   // Number of rows to process
  var dataRange = sheet.getRange(startRow, 1, numRows, 5);
  var data = dataRange.getValues();
  var cal = CalendarApp.getDefaultCalendar();
  for (i in data) {
    var row = data[i];
    var title = row[0];  // First column
    var desc = row[1];       // Second column
    var tstart = row[2];
    var tstop = row[3];
    var loc = row[4];
    //cal.createEvent(title, new Date("March 3, 2010 08:00:00"), new Date("March 3, 2010 09:00:00"), {description:desc,location:loc});
    cal.createEvent(title, tstart, tstop, {description:desc,location:loc});
 }
}

However because it's creating a new event and not simply updating the original event, it creates duplicate events every time I push the script.

I want to find code that instead uses the event ID gathered in the first function and simply updates the existing event with the new information.

I'd prefer not to use code that deletes all calendar events then creates all the new ones.


回答1:


The getEventSeriesById() method can be used. Even though the name indicates that this method is for getting a calendar event that is a series, it works for a single event.

The documentation states:

Gets the event series with the given ID. If the ID given is for a single CalendarEvent, then a CalendarEventSeries will be returned with a single event in the series.

Apps Script documentation - getEventSeriesById

function caltest1() {
  var cal,desc,i,iCalId,loc,row,sheet,startRow,thisEvent,title,tstart,
      tstop,;

  sheet = SpreadsheetApp.getActiveSheet();
  startRow = 2;  // First row of data to process
  var numRows = 2;   // Number of rows to process
  var dataRange = sheet.getRange(startRow, 1, numRows, 5);
  var data = dataRange.getValues();

  cal = CalendarApp.getDefaultCalendar();

  for (i in data) {
    row = data[i];
    title = row[0];  // First column
    desc = row[1];       // Second column
    tstart = row[2];
    tstop = row[3];
    loc = row[4];
    iCalId = row[5];//Column 6 or F

    //cal.createEvent(title, new Date("March 3, 2010 08:00:00"), new Date("March 3, 2010 09:00:00"), {description:desc,location:loc});
    //cal.createEvent(title, tstart, tstop, {description:desc,location:loc});

    thisEvent = cal.getEventSeriesById(iCalId);//Get a calendar event by it's ID

    if (thisEvent) {
      thisEvent.setDescription('Test it to determine if it works');//Edit the description
    }
 }
}

Note that the above code assumes that the calendar event ID is in column F, and that the function that downloaded the calendar data put the ID in column F.



来源:https://stackoverflow.com/questions/43823202/updated-google-calendar-events-from-sheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!