javascript - 如何循环遍历 Google 表格中的事件 ID 以检索附件 ID?

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

根据以下答案,我发现了如何通过 Google Apps 脚本和日历 API 使用事件 ID 来检索 Google 日历附件 ID。

我在电子表格的一列中有一个日历事件 ID 列表,我希望循环遍历该列表并返回相邻列中的事件附件 ID(并跳过任何没有附件的事件)。有人可以提供帮助吗?

让我获得一次检索 1 个事件附件 ID 的代码:

您想要从事件中检索文件 ID。如果我的理解是正确的,这个示例脚本怎么样?

var inStorePartiesCalendarID = "### calendar ID ###";
var eventId = "### Event ID ###";
var res = Calendar.Events.get(inStorePartiesCalendarID, eventId, {fields: "attachments/fileId"});
var fileIds = res.attachments.map(function(e){return e.fileId});
Logger.log(fileIds)

让我获得一次检索 1 个事件附件 ID 的代码的问题: How to get Google calendar event attachment using Apps Script

最佳答案

OP 的起点是 @Tanaike 为 How to get Google calendar event attachment using Apps Script 开发的代码。这里显示的结果代码是该代码/逻辑的增强,以实现:

-循环遍历工作表上的一系列 eventID,
- 计算附加到该事件的 fileID 的数量(如果有),
-检索每个文件ID,
-更新文件数量和相应文件 ID 的工作表。

一个事件最多可以附加 25 个附件。该代码是动态的;它将报告并显示任何给定事件ID 存在的尽可能多的附件。

Note: the eventID is the id field NOT the iCalUID.

此外,该代码还依赖于启用“高级 Google 服务”下的日历 API(脚本编辑器菜单:资源> 高级 Google 服务)以及“高级 Google 服务”上的“Google 日历 API” Google Cloud Platform API 仪表板。”

如果运算符(operator)希望/需要在函数的各个阶段识别关键值,我在代码中留下了许多 Logger 语句。

function so53877465() {
  // setup the spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("testsheet");

  // get the number of EventIDs in Column A
  var Avals = ss.getRange("A1:A").getValues();
  var Alast = Avals.filter(String).length;
  //Logger.log("DEBUG: The last row on A = " + Alast);// DEBUG

  //Loop though each event ID
  for (var r = 1; r < Alast; r++) {
    // Logger.log("DEBUG: Aval row " + r + " = " + Avals[r][0]);//DEBUG

    // substitute your own Calendar ID
    var myCalendarID = "<<inset your Calendar ID>>";

    // get the eventID
    var eventId = Avals[r][0];

    // get the Even info
    var response = Calendar.Events.get(myCalendarID, eventId);

    // just focus on the attachments
    var events = response.attachments;

    // setup some variables
    var filecount = 0; //count the number of files in an event
    var filearray = []; // create an array for the file could and fileIDs

    // ccount the number of attachments for this eventID
    for (key in events) {
      filecount++;
    }
    //Logger.log("DEBUG: For row = " + r + ", the file count is " + filecount);//DEBUG

    // push the filecount onto the empty array
    filearray.push(filecount);

    // loop through the fileIDs
    for (i = 0; i < filecount; i++) {
      // Logger.log("DEBUG: i: " + i);// DEBUG

      // get the attachments
      var event = events[i];
      //get the file iD
      var myfileid = event.fileId;
      // Logger.log("DEBUG: The file id: " + myfileid);//DEBUG

      // push the fileid onto the array
      filearray.push(myfileid);
    }

    // adjust the array for 2d
    var values = [
      filearray
    ];

    // define the target range
    var range = sheet.getRange((r + 1), 2, 1, (filecount + 1)); //(row, column, numRows, numColumns)
    // Logger.log("DEBUG: the target range is " + range.getA1Notation());//DEBUG

    // update the target range with the array values.
    range.setValues(values);
  }
}
<小时/>

之前
Data sheet - BEFORE

<小时/>

之后
Data sheet - AFTER

关于javascript - 如何循环遍历 Google 表格中的事件 ID 以检索附件 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53877465/

相关文章:

javascript - Three.js 中的可平铺纹理

javascript - Google Apps 脚本 - 如何发送包含数组内容的电子邮件?

javascript - 我可以让 Google Drive 电子表格像 MySQL 数据库一样工作吗?

php - 如何解决 Google 日历的链接重定向通知?

ruby - 谷歌 API : one authenticated user for all

java - 如何处理 urn :ietf:wg:oauth:2. 0:oob redirect in Google Calendar API Authorization

javascript - 如何从不提供 Restful 服务的站点提取数据?

javascript - 虽然 Ajax 多次提交但表单已提交

javascript - Edge 在 mouseleave/mouseenter 上看不到按钮按下

logging - Google Apps 脚本日志(网络应用)不会显示在新界面中