导出工作表数据时如何删除重复的日历事件?

时间:2018-06-24 19:59:50

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

我希望我在编辑D列时随时将Google表格导入到我的日历中。我有一个有效的脚本,但它会重复这些条目。我想删除具有相同事件名称的所有事件(列E)。用简单的话来说,每当我编辑D列时,我都希望删除所有日历条目并添加新的日历条目。

知道我在做什么错吗?

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh0 = ss.getSheetByName("DATA");
  var entries = [{
    name : "Export Events",
    functionName : "addEvents"
  }];
  ss.addMenu("Calendar Actions", entries);
};

function addEvents(){
  var ss = SpreadsheetApp.getActiveSheet();
  var headerRows = 1;
  var range = ss.getDataRange();
  var values = range.getValues(); 
  var data = ss.getRange("D2:E24").getValues();
  var cal = CalendarApp.getCalendarById('k8kjmthm3fmcapidh9urkk6h2g@group.calendar.google.com');
  var calID = "k8kjmthm3fmcapidh9urkk6h2g@group.calendar.google.com";
  //Logger.log(cal);
  //Logger.log(data.length);

  for(var i = 0;i<data.length;i++){
    if (i < headerRows) continue; 
    var column = data[i];
    var row = data[i];
    Logger.log(row);
    var date = row[0];
    //Logger.log(date);
    var title = row[1];
    var date = row[0];
    var id = row[1];
    var event = row[1]
    var eventSeries = row[1]
    var startDate = row[0]
    var endDate = row[0]

    try {
      var event = cal.getEventSeriesById(id);
      event.deleteEventSeries();
      row[1] = '';
      var event = cal.deleteEvent(row[1]);
    }
    catch (e) {
      Logger.log(event);
    }

    var newEvent = cal.createAllDayEvent(title, startDate).getId();
    row[1] = newEvent;  // Update the data array with event ID
  }
}

0 个答案:

没有答案