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

Google Apps Script报错e.namedvalues未定义,邮件发送功能故障求助

问题排查与修复:表单提交后邮件发送失败(报错"namedvalues is not defined")

核心问题分析

  1. 事件对象属性大小写错误:Google Apps Script表单提交事件对象的属性为namedValues(驼峰命名,V大写),原代码写成e.namedvalues(小写v),直接导致变量未定义报错。
  2. createPDF函数逻辑异常:函数未接收外部传入的表单数据,反而内部硬编码测试数据,且没有返回生成的PDF文件对象,导致邮件发送时附件参数无效。
  3. 邮件参数拼写错误:sendEmail函数中附件参数attachement拼写错误,正确应为attachments(复数形式)。

修复后的完整代码

function afterformsubmit(e) {
  // 修正大小写:使用正确的驼峰命名属性namedValues
  const info = e.namedValues;
  const pdfFile = createPDF(info);
  // 确保表单字段名与实际一致,这里使用表单中的"Email Address"字段
  sendEmail(info['Email Address'][0], pdfFile);
}

function sendEmail(email, pdfFile) {
  // 修正附件参数拼写:attachments
  GmailApp.sendEmail(email, "您的出行申请文件已生成", "请查收附件中的申请文件", {
    attachments: [pdfFile],
    name: '出行服务申请系统'
  });
}

function createPDF(info) {
  // 移除硬编码测试数据,使用传入的表单真实数据
  const PDFfolder = DriveApp.getFolderById("1DOuJV6BDvsHj1kuYIXuV_gvhz7fIogYs");
  const tempfolder = DriveApp.getFolderById("1RYqxPdGoUPftkemxAktGFhLiZF6EiJDI");
  const templatedoc = DriveApp.getFileById("1lXxmwzAilKLUQSkJ7TOe-O0Y149xP_3Bx_YQu7hwd6w");

  const newTempfile = templatedoc.makeCopy(tempfolder);
  const openDoc = DocumentApp.openById(newTempfile.getId());
  const body = openDoc.getBody();

  // 用表单数据替换模板占位符
  body.replaceText("{dor}", info['Date of request'][0]);
  body.replaceText("{from}", info['From'][0]);
  body.replaceText("{to}", info['Going To'][0]);
  body.replaceText("{Office}", info['Office'][0]);
  body.replaceText("{Unit}", info['Unit'][0]);
  body.replaceText("{dov}", info['Date of visit'][0]);
  body.replaceText("{tov}", info['Time of Visit'][0]);
  body.replaceText("{Passenger name}", info['Passenger name'][0]);
  body.replaceText("{Contact add}", info['Contact add'][0]);
  body.replaceText("{Pick up location}", info['Pick up location'][0]);
  body.replaceText("{Return date}", info['Return date'][0]);
  body.replaceText("{Return time}", info['Return time'][0]);
  body.replaceText("{Number of Passenger}", info['Number of Passenger'][0]);
  body.replaceText("{Return location}", info['Return Pick up'][0]);

  openDoc.saveAndClose();

  const blobPDF = newTempfile.getAs(MimeType.PDF);
  // 生成带乘客姓名的自定义文件名,便于识别
  const pdfFileName = `出行申请_${info['Passenger name'][0]}_${info['Date of request'][0]}`;
  const pdfFile = PDFfolder.createFile(blobPDF).setName(pdfFileName);

  // 返回PDF文件对象,供邮件发送函数调用
  return pdfFile;
}

额外测试建议

可以添加模拟测试函数,避免反复提交表单测试:

// 模拟表单提交事件,用于测试代码逻辑
function testSubmit() {
  const mockE = {
    namedValues: {
      'Date of request': ['7/26/2022'],
      'From': ['Wardak'],
      'Going To': ['Said Abad'],
      'Office': ['WRMO'],
      'Unit': ['M&E'],
      'Date of visit': ['7/26/2022'],
      'Time of Visit': ['12:00:00 PM'],
      'Passenger name': ['Adil mansour'],
      'Contact add': ['0456125421'],
      'Pick up location': ['Said Abad'],
      'Return date': ['7/26/2022'],
      'Return time': ['8:00:00 AM'],
      'Number of Passenger': ['1'],
      'Return Pick up': ['Kabul'],
      'Email Address': ['test@example.com']
    }
  };
  afterformsubmit(mockE);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 15:05:26