google-apps-script - Google Calendar App 脚本定期将数据拉入电子表格

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

我一直在使用此脚本将 Google 日历中的数据提取到电子表格中。我有几个问题需要改进:

  1. 是否可以将多个日历中的数据提取到同一个电子表格中,而不会相互覆盖?
  2. 是否可以让它在电子表格中添加一个新的数据列来标记每一行来自哪个日历?
  3. 此外,您如何将其设置为每 24 小时自动运行一次?

谢谢!

function caltest3(){
  var ss = SpreadsheetApp.openById("SPREADSHEET ID");
  SpreadsheetApp.setActiveSpreadsheet(ss);
  var cal=CalendarApp.getCalendarById("CALENDAR ID");
  var sheet = SpreadsheetApp.getActiveSheet();

  var events = cal.getEvents(new Date("January 1, 2013"), new Date("January 13, 2013"));
for (var i=0;i<events.length;i++) {
  //http://www.google.com/google-d-s/scripts/class_calendarevent.html
      var details=[[events[i].getTitle(), events[i].getStartTime(),            
                    events[i].getEndTime(), events[i].getDescription(),
                    events[i].getLocation()]];
  var row=i+1;
  var range=sheet.getRange(row+1,1,1,5);
  range.setValues(details);
    }
  }
}

最佳答案

简短的回答然后是一个例子:

  1. 您不能通过对 CalendarApp 的单个调用来完成此操作,但是如果您遍历 calendarIds 列表并将该日历的结果事件添加到数组中,则您可以对该数组进行排序并把这个放到电子表格中
  2. 只需将日历名称(或 ID)作为另一项添加到 details 数组中,只需记住将范围列数增加 1
  3. 将脚本触发器添加到您想要的任何时间段,documentation最好地描述了这一点。

无论您是从多个日历还是仅从一个日历构建日历事件数组,在循环中将它们添加到一个数组,然后在任何循环之外将它们写入电子表格是比每次调用时逐行写入更好的做法getRange()setValues 方法它们是单独的 API 调用,并且这些调用对于 Apps 脚本来说时间成本最高。使用适合您的数据的范围分别调用一次,您的脚本运行速度将提高一个数量级。

下面的脚本说明了答案 1 和 2。如果为此设置计时器,您的需求可能会有所不同,因为您可能想要影响查询的时间段?

function caltest3(){

  var ss = SpreadsheetApp.openById( 'spreadsheetId' ),
      sheet = ss.getSheetByName( 'sheetName' ),
      cals = ['id1', 'id2', 'id3'], c, cal, calName,
      start = new Date( 'whenever' ), end = new Date( 'whenever' ),
      events, i, details,
      eventslog = [], e,
      rows = [], range;

  for (c = 0; c < cals.length; c += 1) {

    cal = CalendarApp.getCalendarById(cals[c]);
    calName = cal.getTitle();
    events = cal.getEvents(start, end);

    // add the events of the current calendar to the array of all events
    eventslog = eventslog.concat(
      events.map(function(event) {
        return {
          time: new Date(event.getStartTime()).getTime(), // sort by this
          details: [
            event.getTitle(),
            event.getStartTime(),
            event.getEndTime(),
            event.getDescription(),
            event.getLocation(),
            calName // change calendar info position in array to suit
          ]
        };
      })
    );
  }

  // sort array of event so date order can be either way by reversing a & b
  eventslog.sort(function(a, b) { return a.time - b.time; });

  rows = eventslog.map(function(entry) { return entry.details; });

  range = sheet.getRange(2, 1, rows.length, 6);
  range.setValues(rows);
}

code spaced out for clarity

关于google-apps-script - Google Calendar App 脚本定期将数据拉入电子表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14415752/

相关文章:

google-apps-script - 在谷歌应用脚​​本中使用 Dall-E

javascript - 谷歌电子表格脚本 : "Cannot find function getRange in object Sheet" when creating a simple function

php - Google Cal API - 检查事件的脚本

google-apps-script - (Gmail) 从电子表格发送电子邮件。如何添加带有图像的签名?

google-apps-script - 如何使用 Google Apps 脚本将一张工作表中的值的背景颜色设置为另一张工作表中具有匹配值的单元格?编辑部分添加

javascript - 将表单数据提交到 Google 表格时 Google App 脚本出错

gradle - 添加Google API后出现Gradle错误

java - 如何使用 API 在新的 Google 电子表格中创建第一行?

javascript - 修改表单中特定字段的最佳方法是什么?

permissions - 想要允许人们编辑谷歌日历事件,但只能获得 'See only free/busy' 权限选项