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

修复Google表单脚本 按单元格关键词触发带完整提交内容的告警邮件

修复后的Google Apps Script及说明

修复的核心问题

  • 原代码关键词匹配逻辑完全错误:既使用了错误的精确匹配判断,也存在语法错误,导致要么永远触发要么不触发告警
  • 原代码遗漏了D列「昨日学习/工作内容」的取值拼接,导致邮件正文缺字段
  • 优化了关键词匹配规则,支持不区分大小写匹配,避免漏判

修复后完整代码

function checkComments(){
  // 配置参数:请自行修改告警邮箱和关键词列表
  const ALERT_EMAIL = '替换为你的实际收件邮箱地址';
  // 告警关键词列表,新增关键词直接往数组里加即可,不需要改判断逻辑
  const WARN_KEYWORDS = ['drug', 'die', 'emotion', 'suicide'];
  
  const sheet = SpreadsheetApp.getActive().getSheetByName("Sheet2");
  const lastRow = sheet.getLastRow();
  // 读取最新一行的补充内容,统一转小写做不区分大小写匹配
  const comments = String(sheet.getRange('J' + lastRow).getValue()).toLowerCase();

  // 检查是否包含任意告警关键词
  const hasWarnWord = WARN_KEYWORDS.some(keyword => comments.includes(keyword));
  if (!hasWarnWord) return;

  // 读取最新一行所有字段内容
  const timestamp = sheet.getRange('A' + lastRow).getValue();
  const studemail = sheet.getRange('B' + lastRow).getValue();
  const studname = sheet.getRange('C' + lastRow).getValue();
  const yesterdayWork = sheet.getRange('D' + lastRow).getValue();
  const feelings = sheet.getRange('E' + lastRow).getValue();
  const tired = sheet.getRange('F' + lastRow).getValue();
  const stressed = sheet.getRange('G' + lastRow).getValue();
  const todayPlan = sheet.getRange('H' + lastRow).getValue();
  const expectThing = sheet.getRange('I' + lastRow).getValue();

  // 拼接邮件内容,增加字段说明方便阅读
  const results = `
时间戳:${timestamp}
学生姓名:${studname}
学生邮箱:${studemail}
昨日学习/工作内容:${yesterdayWork}
今日整体感受:${feelings}
今日是否疲惫:${tired}
今日是否有压力:${stressed}
今日安排:${todayPlan}
今日值得期待的事:${expectThing}
其他补充内容:${comments}
  `.trim();

  const subject = `Daily Survey Alert from ${studname}`;
  const message = `Alert from the daily survey!\n\n${results}`;
  
  MailApp.sendEmail(ALERT_EMAIL, subject, message);
}

使用说明

  • 把代码里的替换为你的实际收件邮箱地址改成你自己的收信邮箱
  • 需要新增告警关键词时,直接往WARN_KEYWORDS数组里加英文小写的关键词即可,不需要修改判断逻辑
  • 原有触发器配置不用改动,直接替换代码保存即可生效

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 16:57:05