首页 > 解决方案 > Google Sheet 在单元格具有特定值时发送电子邮件

问题描述

我有一个供学生输入数据的 Google 表单,如果特定问题有特定答案,我希望将电子邮件发送给我自己或其他人。在电子邮件中,我希望将提交表格的学生的姓名包含在电子邮件中。我遇到的问题是电子邮件仅从表格的第 2 行发送信息。(我有第 1 行 Frozen 作为标题)。我需要它从谷歌表单输入的最后一行数据中提取。

谷歌表格 - https://docs.google.com/forms/d/1r1Tsyfl71zfzgzhs3-_qFB57FcSSEX1zS5BkTgz-ESA/prefill

谷歌表格数据 - https://docs.google.com/spreadsheets/d/1P1RD7My91jLSHS9hgEOBPv5oYnfCJQviN6Nzy_cH7Cc/edit?usp=sharing

这是我的脚本代码:

 function CheckStudent(e) {

  
       // Fetch the email address
      var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2").getRange("B2");
      var emailAddress = emailRange.getValue();
  
  
      // Send Alert Email.
    {
      var studentData = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses 1").getRange("C2");
    var studentData = studentData.getValue();
    var message = studentData + ' is not doing well today!' // Second column
    var subject = 'Student Daily Check Alert';
    MailApp.sendEmail(emailAddress, subject, message);
    }
    }

标签: google-apps-scriptgoogle-sheetsgoogle-formsemail-notifications

解决方案


如果您已经在表单提交的表单中注册了电子邮件,那么就像创建可安装触发器一样简单。

您可以使用事件对象来做到这一点。

如果您不熟悉触发器,那么我建议您阅读其中的官方文档,您可以看到一些如何创建触发器的示例:

var sheet = SpreadsheetApp.getActive();
ScriptApp.newTrigger("CheckStudent") // You will need to adapt your function 
                                     // to recieve the event parameter
  .forSpreadsheet(sheet)
  .onFormSubmit()
  .create();

一旦你执行了前面的代码,你的函数就会在每次有人提交表单时触发。


编辑

现在我想你可以直接在表单对象中执行触发器,而不是通过工作表。

稍微更改事件对象代码:

var form = FormApp.getActiveForm();
ScriptApp.newTrigger('CheckStudent')
    .forForm(form)
    .onOpen()
    .create();

推荐阅读