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

Google Apps Script日历脚本故障:无法检测录制文件并发送邮件

Google Apps Script无法发送带录制文件邮件的问题排查

需求

  • 遍历当日日历事件
  • 找到标题为“Test”的会议
  • 检测该会议是否附有录制文件
  • 若存在录制文件则发送包含该文件的邮件

用户提供的代码

function sendEmailIfVideoAttached() {
  // Set the email recipient
  var recipient = "test@example.com";

  // Get today's calendar events
  var today = new Date();
  var events = CalendarApp.getDefaultCalendar().getEventsForDay(today);

  // Find the event titled "Test" and check if it has a video attachment
  for (var i = 0; i < events.length; i++) {
    var event = events[i];
    if (event.getTitle() == "Test") {
      var attachments = event.getAttachments();
      for (var j = 0; j < attachments.length; j++) {
        var attachment = attachments[j];
        if (attachment.getContentType().startsWith("video/")) {
          // If there's a video attachment, send an email to the recipient
          MailApp.sendEmail({
            to: recipient,
            subject: "Test event has a video attachment",
            body: "The Test event on " + today.toLocaleDateString() + " has a video attachment.",
          });
          return; // Stop looking for more events
        }
      }
      // If the event doesn't have an attachment with a video content type, check the fileId
      var attachmentIds = event.getAttachmentIds();
      for (var k = 0; k < attachmentIds.length; k++) {
        var fileId = attachmentIds[k];
        var file = DriveApp.getFileById(fileId);
        var fileMimeType = file.getMimeType();
        if (fileMimeType.startsWith("video/")) {
          // If there's a video file attachment, send an email to the recipient
          MailApp.sendEmail({
            to: recipient,
            subject: "Test event has a video attachment",
            body: "The Test event on " + today.toLocaleDateString() + " has a video attachment.",
          });
          return; // Stop
        }
      }
    }
  }
}

代码存在的问题

  1. 核心功能缺失:未添加邮件附件
    这是最关键的问题——代码只发送了纯文本邮件,完全没有将找到的视频文件作为附件传入MailApp.sendEmail的参数中,自然无法实现“发送包含录制文件的邮件”的需求。

  2. 冗余的检测逻辑
    代码分别通过getAttachments和getAttachmentIds两次检测视频附件,逻辑重复。实际上CalendarApp.EventAttachment对象已经提供了直接获取文件内容的方法,不需要额外调用Drive服务通过ID取文件。

  3. 日期显示精度不足
    用脚本运行时的today日期,不如直接取事件的开始时间日期更准确,避免脚本跨零点运行时出现日期偏差。

  4. 潜在权限风险
    调用DriveApp.getFileById需要额外的Drive访问权限,如果脚本未授权该权限会直接报错,反而用Calendar附件自带的copyBlob()方法更安全高效。

修正后的代码

function sendEmailIfVideoAttached() {
  const recipient = "test@example.com";
  const today = new Date();
  const events = CalendarApp.getDefaultCalendar().getEventsForDay(today);

  for (const event of events) {
    if (event.getTitle() !== "Test") continue;

    const attachments = event.getAttachments();
    for (const attachment of attachments) {
      if (attachment.getContentType().startsWith("video/")) {
        // 将视频附件转为Blob格式,添加到邮件附件中
        const videoBlob = attachment.copyBlob();
        MailApp.sendEmail({
          to: recipient,
          subject: "Test event has a video attachment",
          body: `The Test event on ${event.getStartTime().toLocaleDateString()} has a video attachment.`,
          attachments: [videoBlob] // 关键:添加附件参数
        });
        return; // 找到目标事件后终止脚本
      }
    }
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 15:17:49