You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Google Apps Script获取Google日历事件附件

嘿,看到你已经搞定了给日历事件添加附件的功能,太棒了!要获取这些日历事件的附件其实也很简单,我给你捋清楚具体怎么操作:

获取Google日历事件附件的方法

核心要点是调用Calendar API时必须带上supportsAttachments: true参数,否则返回的事件对象里不会包含附件数据。

1. 获取单个事件的附件

如果你已经知道目标事件的ID和所属日历ID,可以用Calendar.Events.get()方法来获取附件:

function getEventAttachments(eventId, calendarId) {
  // 关键:必须传入supportsAttachments: true,否则无法获取附件数据
  const event = Calendar.Events.get(calendarId, eventId, {supportsAttachments: true});
  
  // 检查事件是否存在附件
  if (event.attachments && event.attachments.length > 0) {
    console.log("找到事件附件:");
    event.attachments.forEach((attachment, idx) => {
      console.log(`第${idx + 1}个附件:`);
      console.log(`标题: ${attachment.title}`);
      console.log(`文件链接: ${attachment.fileUrl}`);
      console.log(`文件ID: ${attachment.fileId}`); // 这个字段可以直接用来操作Drive文件,比从URL提取更方便
    });
    return event.attachments;
  } else {
    console.log("该事件没有附件");
    return [];
  }
}

调用这个函数时,只需要传入你的实际事件ID和日历ID即可,比如:

// 替换成你的实际参数
getEventAttachments("your-event-id-here", "your-calendar-id-here");

2. 批量获取日历中所有带附件的事件

如果需要遍历某个日历里的所有事件并提取附件,可以用Calendar.Events.list()方法:

function getAllEventsWithAttachments(calendarId) {
  const eventsResponse = Calendar.Events.list(calendarId, {
    supportsAttachments: true,
    maxResults: 200, // 可根据需求调整单次获取的事件数量
    singleEvents: true, // 把重复事件展开为单个独立事件,避免只获取主事件的附件
    timeMin: new Date().toISOString() // 可选:只获取当前时间之后的事件,可按需修改
  });

  if (!eventsResponse.items || eventsResponse.items.length === 0) {
    console.log("该日历中没有找到事件");
    return;
  }

  console.log("带附件的事件列表:");
  eventsResponse.items.forEach(event => {
    if (event.attachments && event.attachments.length > 0) {
      console.log(`\n事件标题: ${event.summary}`);
      console.log("附件:");
      event.attachments.forEach(att => {
        console.log(`- ${att.title}: ${att.fileUrl}`);
      });
    }
  });
}

一些注意事项

  • 权限问题:确保你的脚本已经获得了Calendar API的访问权限,操作Drive文件的话可能还需要Drive API权限(一般授权流程会自动提示你授权)
  • 共享日历权限:如果操作的是共享日历,要确保你的账号拥有读取该日历事件及附件的权限
  • 重复事件处理:如果不设置singleEvents: true,重复事件只会返回主事件的附件,若需要所有重复实例的附件,一定要开启这个参数

内容的提问来源于stack exchange,提问作者Ryan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:19:40