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

如何为现有Google Apps Script邮件添加指定Drive文档作为附件?

问题分析

你的代码存在两个核心问题:

  1. 变量名冲突:循环内已经定义了const file = temp.makeCopy(folder);,后续又用var file获取指定Drive文档,这会覆盖之前的变量,导致最后file.setTrashed(true);删除的是你新加的文档,而不是生成的模板副本。
  2. 未使用构建好的附件数组:你创建了attachments数组并添加了额外文档,但发送邮件时依然只传入了单个PDF blob,完全没用到这个数组。
修正后的完整代码
const SHEETID = '1rx1lCYKdhi8dhivoYpUO6EIHb2TWTpiMVduK7M-L2A4';
const DOCID = '1sRZqPCkuATT9tQDZlJDp-DicP6saBpZoAXVvKWXT_XM';
const FOLDERID = '1wsyrUM29A1LIiKCjsJE7olKb0ycG2_M5';
// 把额外文档ID设为常量,方便后续修改维护
const EXTRA_DOC_ID = '1vGvLVP2RV1krxnj8Mt6hMiFHVBoIdbFG';

function PitchFees2026() {
  const sheet = SpreadsheetApp.openById(SHEETID).getSheetByName('2026 Fees');
  const temp = DriveApp.getFileById(DOCID);
  const folder = DriveApp.getFolderById(FOLDERID);

  const data = sheet.getDataRange().getValues();
  const rows = data.slice(1);
  rows.forEach((row, index) => {
    const copyFile = temp.makeCopy(folder); // 重命名变量避免冲突
    const doc = DocumentApp.openById(copyFile.getId());
    const body = doc.getBody();
    
    // 一次性替换所有模板占位符,不用遍历表头重复操作
    body.replaceText('{NAME}', row[1]);
    body.replaceText('{PITCH}', row[0]);
    body.replaceText('{AMOUNT}', row[3]);
    body.replaceText('{FIRST}', row[4]);
    body.replaceText('{SECOND}', row[5]);
    body.replaceText('{REF}', row[10]);
    body.replaceText('{BNAME}', row[7]);
    body.replaceText('{CODE}', row[8]);
    body.replaceText('{NUMBER}', row[9]);
    body.replaceText('{TERMS}', row[6]);
    
    doc.setName(row[10]);
    const blob = doc.getAs(MimeType.PDF);
    doc.saveAndClose();

    const pdf = folder.createFile(blob).setName(row[10] + '.pdf');

    const email = row[2];
    const subject = row[10];
    const messageBody = "This message (including any attachments) is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination, or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. \n \nIf you received this in error, please delete the material from your computer and contact the sender. \n\nPlease consider the environment before printing this e-mail.";

    // 构建完整的附件数组:包含生成的PDF和指定文档的PDF
    const attachments = [blob];
    const extraDocFile = DriveApp.getFileById(EXTRA_DOC_ID);
    attachments.push(extraDocFile.getAs(MimeType.PDF));

    // 发送邮件时传入完整的附件数组
    MailApp.sendEmail({
      to: email,
      subject: subject,
      body: messageBody,
      attachments: attachments
    });

    Logger.log(row);
    copyFile.setTrashed(true); // 使用正确的变量删除模板副本
  });
}
额外优化说明
  • 将额外文档ID抽离为常量,后续修改时只需改一处即可。
  • 简化了模板替换逻辑:原来遍历表头每个元素都重复替换所有占位符,现在直接一次性替换,提升代码运行效率。
  • 彻底解决变量名冲突问题,避免误删文件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 13:13:15