首页 > 解决方案 > 如何在 Google 表格中运行动态脚本(实时编辑)

问题描述

我正在尝试运行一个脚本,该脚本将在复选框标记为“TRUE”时执行并向客户发送电子邮件。我无法让触发器工作,我似乎也可以自己做。我把代码放在下面。请帮忙。

'''code'''
***Email Function
function SendEmail(i) {
  
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  //var lr= ss.getLastRow();
//for (var i = 2; i<=lr;i++){
  var currentEmail= ss.getRange(i, 1).getValue();
  //logger.log(currentEmail);
   var currentADID= ss.getRange(i, 3).getValue();
  MailApp.sendEmail(currentEmail,"Customer Ready"+ currentADID,"This customer should be ready to schedule and installtion date and time" );
}


Trigger Function
function CheckCD() {
 var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var lr= ss.getLastRow();
  for (var i = 2; i<=lr;i++){
    var currentDCheck= ss.getRange(i, 4).getValue();
    var x= onEdit():
if (currentDCheck == true)
SendEmail(i)
  }
}***

标签: google-sheets

解决方案


您实际上误解了如何使用onEdit触发器,尽管由于需要获得许可,在这种情况下您需要安装触发器MailApp.sendEmail

无论如何,您需要使用事件对象 ( e),因为它包含有关已编辑单元格属性的详细信息。请参见下面的代码:

代码:

// Trigger function, install it under "Triggers" tab as you need permission for MailApp
function CheckCD(e) {
  // get the row and column of the edited cell
  var row = e.range.getRow();
  var column = e.range.getColumn();
  // proceed only if edited cell's range is D2:D and value is "TRUE"
  if (row > 1 && column == 4 && e.value == "TRUE") {
    // get the sheet where the cell was edited
    var ss = e.source.getActiveSheet();
    // get Email and ADID of the same row where checkbox was ticked
    var currentEmail = ss.getRange(row, 1).getValue();
    var currentADID = ss.getRange(row, 3).getValue();
    MailApp.sendEmail(currentEmail, "Customer Ready" + currentADID, "This customer should be ready to schedule and installtion date and time");
  }
}

安装触发器:

安装

  • 确保选择On edit事件类型并选择要运行的功能。(在这种情况下,CheckCD

样本数据:

样本数据

输出:

输出

笔记:

  • 该脚本的行为是,每当有人选中 range 上的复选框D2:D并且结果值为TRUE时,它就会发送一封电子邮件。(因为您似乎每行都有不同的电子邮件,但如果没有,请将它们全部发送到一封电子邮件中)

参考:


推荐阅读