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

请求编写Google表单响应表格新增邮箱触发发邮件的Google Script

Google Script 示例代码及配置指南

完整脚本代码

function sendExtraContactEmail(e) {
  // 获取表单响应表
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses 1');
  // 获取当前提交行的行号
  const rowIndex = e.range.getRow();
  // 获取N列的邮箱地址(14对应N列)
  const extraEmail = sheet.getRange(rowIndex, 14).getValue().trim();

  // 跳过空邮箱或格式无效的情况
  if (!extraEmail || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(extraEmail)) {
    return;
  }

  // 获取当前行的所有提交数据
  const rowData = sheet.getRange(rowIndex, 1, 1, sheet.getLastColumn()).getValues()[0];
  // 获取表格的可访问URL
  const sheetUrl = SpreadsheetApp.getActiveSpreadsheet().getUrl();

  // 构建邮件主题和内容
  const subject = `新表单提交通知 - ${rowData[1] || '未知提交人'}`; // 假设第2列是提交人姓名,可自行调整
  let body = `以下是新的表单提交详情:\n\n`;
  rowData.forEach((value, index) => {
    const header = sheet.getRange(1, index + 1).getValue(); // 获取对应表头
    body += `${header}: ${value || '无'}\n`;
  });
  body += `\n查看完整表格:${sheetUrl}`;

  // 指定发件邮箱(需确保该邮箱已在你的Google账号中验证,或属于同G Suite域)
  const fromEmail = 'your-specified-email@example.com';

  try {
    // 发送邮件
    MailApp.sendEmail({
      to: extraEmail,
      subject: subject,
      body: body,
      from: fromEmail
    });
    console.log(`邮件已成功发送至:${extraEmail}`);
  } catch (error) {
    console.error(`发送邮件失败:${error.message}`);
  }
}

关键配置说明

  • 空单元格处理:脚本会先检查N列邮箱是否为空,同时用正则验证邮箱格式,无效或空的情况直接跳过,避免报错。
  • 指定发件人:将your-specified-email@example.com替换为你需要的发件邮箱,注意该邮箱必须是与当前Google账号关联的已验证邮箱(比如别名、同域邮箱),否则会触发权限错误。
  • 提交数据展示:脚本自动获取当前行的所有表头和对应数据,无需手动指定列,适配表单字段的增减。

触发条件设置步骤

  1. 打开脚本编辑器(表格右上角「扩展程序」→「Apps 脚本」)
  2. 点击左侧菜单栏的「触发器」图标(闹钟形状)
  3. 点击「添加触发器」按钮:
    • 选择要运行的函数:sendExtraContactEmail
    • 选择部署类型:「Head」
    • 选择事件源:「从表单提交」
    • 选择事件类型:「表单提交」
  4. 点击「保存」,按提示完成授权验证(需允许脚本访问邮箱和表格数据)

注意事项

  • 脚本仅处理新提交的行,无需遍历N2:N5000的所有单元格,效率更高。
  • 若遇到发件权限问题,可在Google账号的「安全性」→「应用密码」(仅两步验证用户可用)生成专用密码,或确保指定发件邮箱是账号的别名。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 08:45:31