【问题标题】:Updated Google Calendar events from Sheets更新了表格中的 Google 日历活动
【发布时间】:2017-05-06 17:08:41
【问题描述】:

我正在尝试将 Google 日历同步到工作表,以便我可以轻松地从工作表内批量更新日历事件,然后将所有更改推送回日历。

我可以将所有事件下载到我的工作表中,特别是使用这段代码:

    // 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);

我可以使用此代码从下载的数据中在我的日历上创建新事件:

     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});
 }
}

但是,因为它是创建一个新事件而不是简单地更新原始事件,所以每次我推送脚本时它都会创建重复事件。

我想找到使用第一个函数中收集的事件 ID 并简单地使用新信息更新现有事件的代码。

我不希望使用删除所有日历事件然后创建所有新事件的代码。

【问题讨论】:

    标签: google-apps-script google-sheets google-api calendar google-spreadsheet-api


    【解决方案1】:

    可以使用getEventSeriesById() 方法。尽管名称表明此方法用于获取一系列日历事件,但它适用于单个事件。

    文档说明:

    获取具有给定 ID 的事件系列。如果给定的 ID 是针对单个 CalendarEvent,则 CalendarEventSeries 将与系列中的单个事件一起返回。

    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
        }
     }
    }
    

    请注意,上面的代码假定日历事件 ID 在 F 列中,并且下载日历数据的函数将 ID 放在 F 列中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 2014-10-22
      • 1970-01-01
      相关资源
      最近更新 更多