google-apps-script - 更新了 Google 表格中的 Google 日历事件

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

我正在尝试将 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 的代码,并使用新信息简单地更新现有事件。

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

最佳答案

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

文档指出:

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

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

关于google-apps-script - 更新了 Google 表格中的 Google 日历事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43823202/

相关文章:

if-statement - Google 表格 - 按 If/Then 桶分组,计算值

google-api - 范围 https ://www. googleapis.com/auth/calendar.app.created 的 Google Calendar API 请求被拒绝

javascript - Google Apps 脚本 - 尝试使用 getUrl。类型错误 : Cannot find function getUrl in object Sheet

javascript - Google Apps 脚本 HTML 应用程序中的 Google 图表

javascript - Apps 脚本 - 用逗号分割谷歌表单复选框答案

count - 如何计算每天的时间戳条目数

pdf - 使用 Google Script Editor 将 Google Doc 转换为 PDF

google-apps-script - 我如何 "Use Logger API to add logs to your project"? (谷歌脚本)

email - 通过 Gmail API (Javascript) 发送电子邮件

android - 在没有网站的情况下生成 Places Api key