如何在回答特定Google表单项目时发送审批者电子邮件

时间:2016-01-22 17:31:43

标签: javascript google-apps-script google-sheets google-form

我在使用Google表格/表格时遇到了麻烦 - 只是因为你从一开始就知道我不是一个程序员,而且我对使用Apps脚本非常陌生。

我正在努力实现以下目标:

1)让用户回答表单上的单个问题 2)如果回答了该问题,并将值写入关联工作表中的单元格列,则会自动向2个预定义的批准者电子邮件地址发送电子邮件 3)如果没有回答该问题,并且没有任何值写入单元格列,则不会向2个批准者发送电子邮件

如果我使用下面包含的脚本手动将值添加到工作表中的相应列,我可以完成这项工作,但如果写入工作表的值直接来自表单,则对我不起作用(这就是我需要做的!)。

下面是我在表单上的脚本编辑器中编写的代码,它允许我手动更新列并将通知发送给批准者(同样,我的问题只是如果列不发送电子邮件从表单动态更新)。正如现在所写的那样,如果我手动更新第5列和/或第7列,则会向指定的批准者集发送电子邮件。

但是,如果表单动态更新第5列或第7列,它将不会发送通知。

请帮助,我已经坚持了好几天 - 谢谢!!!

这是我当前的脚本:

/* 
 * This function sends an email when a specific Google Sheets column is edited
 * The spreadsheets triggers must be set to onEdit for this function to work
 */

function sendNotification() {
    //var ss = SpreadsheetApp.openById('1N6dqSXs8hdhCi1vrOGT7I7tD2yDMtgK7BSddMPqmuo0');  
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getActiveSheet();
    //Get Active cell
    var mycell = ss.getActiveSelection();
    var cellcol = mycell.getColumn();
    var cellrow = mycell.getRow();
    //Define Notification Details for vars

    var recipients = "joseph@XXXXX.com,jmang@XXXXX.com";
    var recipients1 = "joseph2@XXXXX.com"

    var subject = "NEW SENSITIVE DATA REQUEST REQUIRES APPROVAL";
    var subject1 = "ALERT: SENSITIVE DATA REQUEST";
    var body = ss.getName() + " has been updated.  Visit " + ss.getUrl() + " to view the changes.";

    //Check to see which column will trigger the email - this script sends an email to "var recipients" if 
    //column 5 is updated and sends and email to "var recipients1" when colum 7 is updated.

    if (cellcol == 5) {
        //Send the Email to recipients defined in "var recipients"
        MailApp.sendEmail(recipients, subject, body);
    }

    if (cellcol == 7) {
        //Send the Email to recipients defined in "var recipients1"
        MailApp.sendEmail(recipients1, subject1, body);
    }

    //End sendNotification
}

这是我在Sheet上设置触发器的方式: 发送表中的sendNotification 1)On Change,2)On Edit,3)On FormSubmit

最后一件事 - 我的公司不允许使用附加组件,因此我必须使用Apps脚本执行此操作...

2 个答案:

答案 0 :(得分:1)

将单独的触发器设置为单独的事件;

工作表编辑事件的

function sendNotificationAtEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//get edited range
var range = event.range;
var cellcol = range.getColumn();
var cellrow = range.getRow();

//Define Notification Details for vars
var recipients = "joseph@XXXXX.com,jmang@XXXXX.com";
var recipients1 = "joseph2@XXXXX.com"

var subject = "NEW SENSITIVE DATA REQUEST REQUIRES APPROVAL";
var subject1 = "ALERT: SENSITIVE DATA REQUEST";
var body = ss.getName() + " has been updated.  Visit " + ss.getUrl() + " to view the changes.";

    if (cellcol == 5) {
        //Send the Email to recipients defined in "var recipients"
        MailApp.sendEmail(recipients, subject, body);
    }

    if (cellcol == 7) {
        //Send the Email to recipients defined in "var recipients1"
        MailApp.sendEmail(recipients1, subject1, body);
    }

    //End sendNotification
}

表单提交事件:

function sendNotificationAtFormSubmit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();

//Define Notification Details for vars
var recipients = "joseph@XXXXX.com,jmang@XXXXX.com";
var recipients1 = "joseph2@XXXXX.com"

var subject = "NEW SENSITIVE DATA REQUEST REQUIRES APPROVAL";
var subject1 = "ALERT: SENSITIVE DATA REQUEST";
var body = ss.getName() + " has been updated.  Visit " + ss.getUrl() + " to view the changes.";
//if form submitted 5th question's answer is not empty
    if (event.values[4] !== '') {
        //Send the Email to recipients defined in "var recipients"
        MailApp.sendEmail(recipients, subject, body);
    }

    if (event.values[6] !== '') {
        //Send the Email to recipients defined in "var recipients1"
        MailApp.sendEmail(recipients1, subject1, body);
    }

    //End sendNotification
}

答案 1 :(得分:0)

执行所需操作的最安全方法是检查表单响应,而不是电子表格中的单元格。至少不适用于On Form Submit触发器。您可能需要两个,onEdit()触发,一个表单提交触发器。

因此,在“表单提交”触发器中,您需要获取当前的表单响应,获取所需的项目,然后从电子表格中与第5列对应的项目中获取单独的响应。

您可以检查回复表中的最后一行,并获取第5列,这将完美地工作 IF 您从未有很多人同时提交表单请求。在这种情况下,您需要锁定您可能想要执行的文档。

为了使用On Submit Trigger获取最后一个响应,您需要将“event”对象添加到该函数中。

function sendNotification(e) {

然后你需要获得当前的回复:

function sendNotification(e) {
  /* Logger.log('e: ' + e);

  for (var key in e) {
    Logger.log('key: ' + key)  
    Logger.log('value: ' + e[key]);
  };  */

  var allAnswers = e.response.getItemResponses();
  var howManyQuestionsInForm = allAnswers.length;
  Logger.log('howManyQuestionsInForm: test again: ' + howManyQuestionsInForm);
  /* for (var key in allAnswers) {
    Logger.log('key: ' + key)  
    Logger.log('value: ' + allAnswers[key]);
  }; */

  var questionThree = allAnswers[2];

  var theAnswerIs = questionThree.getResponse();
  Logger.log('theAnswerIs: ' + theAnswerIs);

  if (theAnswerIs !== "") {
    //Send email
    Logger.log('it got to here')
  };

};

Google Documentation - Form Event