javascript - 如何在回答特定的 Google 表单项目时发送批准人电子邮件

标签 javascript google-apps-script google-sheets google-forms

我在使用 Google Forms/Sheets 时遇到了问题 - 只是让您从一开始就知道我不是一个编码员,而且我对使用 Apps Script 还很陌生。

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

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
}

我在工作表上的触发器是这样设置的: 来自电子表格的 sendNotification 1) 更改时,2) 编辑时,以及 3) 表单提交时

最后一件事 - 我的公司不允许加载项,所以我必须使用 Apps 脚本来做到这一点...

最佳答案

为不同的事件设置不同的触发器;

对于工作表编辑事件:

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
}

关于javascript - 如何在回答特定的 Google 表单项目时发送批准人电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34952705/

相关文章:

google-apps-script - 检查执行是否在 Debug模式下运行

google-apps-script - 与 Google Apps 脚本一起使用的 Caja 版本

javascript - 将谷歌电子表格中的数据提取到 html 表中

javascript - 正则表达式在 JavaScript 中经过测试和工作,但在 Google Sheets REGEXEXTRACT 中中断

javascript - 获取关联数组的特定值及其键名

javascript - 如何检查两个 'lines'是否重叠?

javascript - 如何监听 Angular 1.5 组件中的作用域事件?

javascript - 堆中的本地对象和工厂模式

algorithm - 加快工作表的查找和替换 Google Apps 脚本功能

excel - 使用工作表/excel公式从选项卡中获取唯一行