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

如何在Google Apps Script中添加CC,及通过onFormSubmit触发器实现条件发信

解决方案

1. 添加抄送(CC)配置

操作步骤:

  • 打开脚本关联的「Sender」工作表,在第3行第1列填写「抄送邮箱」,第3行第2列填入固定抄送邮箱,多个邮箱用英文逗号分隔即可。
  • 修改sendEmails函数的两处代码:
    1. 在读取发件人配置的代码后新增读取抄送配置的行:
    var replyTo = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sender").getRange(2, 2).getValue();
    var name_shown = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sender").getRange(1, 2).getValue();
    // 新增抄送配置读取
    var ccEmail = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sender").getRange(3, 2).getValue();
    
    1. 在MailApp.sendEmail的参数中新增cc字段:
    MailApp.sendEmail(emailAddress,subject, "", {
      htmlBody: message,
      replyTo: replyTo,
      name: name_shown,
      cc: ccEmail // 新增的抄送配置
    });
    

2. 配置表单提交触发特定条件发邮件

操作步骤:

  • 先确认关联表单的工作表中,问题「你喜欢什么颜色?红色或黄色」对应的列位置,数组下标从0开始计数,比如问题是表单第6题,对应下标就是5,你可以先提交一次测试数据核对对应下标。
  • 新增触发函数,代码如下:
function onFormSubmitHandler(e) {
  // 获取最新提交的行数据
  const submittedRow = e.values;
  // 将下标替换为你颜色问题对应的实际下标
  const selectedColor = submittedRow[5];
  // 判断是否选择了黄色
  if (selectedColor === "黄色") {
    // 复用现有配置读取逻辑
    const htmlcode = String(SpreadsheetApp.getActiveSpreadsheet().getSheetByName("HTML").getRange(1, 1).getValue());
    const replyTo = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sender").getRange(2, 2).getValue();
    const name_shown = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sender").getRange(1, 2).getValue();
    const ccEmail = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sender").getRange(3, 2).getValue();
    // 可自定义收件人、主题,也可以读取表单提交的邮箱:submittedRow[对应邮箱列的下标]
    const receiverEmail = "目标收件人邮箱@xxx.com";
    const subject = "颜色选择为黄色的提醒邮件";
    let message = htmlcode;
    // 模板变量替换逻辑和你现有sendEmails函数中的逻辑一致即可
    MailApp.sendEmail(receiverEmail, subject, "", {
      htmlBody: message,
      replyTo: replyTo,
      name: name_shown,
      cc: ccEmail
    });
  }
}
  • 配置可安装触发器:打开脚本编辑器,点击左侧「触发器」按钮,选择「添加触发器」,运行函数选择onFormSubmitHandler,事件来源选择「电子表格」,事件类型选择「表单提交时」,保存后即可生效。注意必须使用可安装触发器,简单触发器没有发送邮件的权限。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.16 02:15:04