需要JS帮助 - 观看谷歌工作表专栏,通过Gmail发送更新价值的电子邮件

时间:2018-06-06 20:04:32

标签: javascript google-sheets

我需要在工作表的特定标签上更新E栏时向3个人发送一封电子邮件,其值为' TRUE'。我已经找到了很多关于如何引用电子表格的内容,但没有关于如何将其引导到特定标签的内容......这张表有两个标签。

非常感谢任何建议!我试过这个:( PIPELINE是标签名称。)



function sendEmails() {
  
var sSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("PIPELINE");
var sheetNameToWatch = "PIPELINE";
var columnNumberToWatch = 5; // column A = 1, B = 2, etc.
var valueToWatch1 = "TRUE";
  
if(column[5] = 'TRUE')
  
 //Define Notification Details
 var recipients = "kessla.sloan@skysmb.com, email@email.com";
 var subject = "TEST Update"+e.range.getSheet().getName();
 var body = "google sheet update - TEST";
 //Send the Email
 GmailApp.sendEmail(recipients, subject, body);
    }




1 个答案:

答案 0 :(得分:0)

由于您希望每次编辑工作表时都运行脚本,因此应使用onEdit(e)触发器。

function onEdit(e){
    sendEmails(e.range);
}

此触发器的参数e包含编辑范围,因此您可以轻松检查if中的列和工作表。

function sendEmails(range) {
    var sheetNameToWatch = "PIPELINE";
    var columnNumberToWatch = 5; // column A = 1, B = 2, etc.
    var valueToWatch1 = "TRUE";

    //here you check if all the requirements are met
    var rightSheet = range.getSheet().getName() == sheetNameToWatch;
    //the == are used for comparaison, while = are used only for assignment.
    var rightColumn = range.getColumn() == columnNumberToWatch;
    var rightValue = range.getValue() == valueToWatch1;

    // the && are "and" operators, so that the script run only of the 3 conditions are met.
    if(rightSheet && rightColumn && rightvalue){ //don't forget the brackets here
         //Define Notification Details
         var recipients = "kessla.sloan@skysmb.com, email@email.com";
         var subject = "TEST Update"+ range.getSheet().getName();
         var body = "google sheet update - TEST";
         //Send the Email
         GmailApp.sendEmail(recipients, subject, body);
    }
}