Google sheets 脚本,用于在每次更新列旁边的单元格时向列中的特定单元格发送电子邮件

Google sheets 脚本,用于在每次更新列旁边的单元格时向列中的特定单元格发送电子邮件,google-sheets,Google Sheets,我希望在我尝试做标题所说的事情之前,没有人问过我这个问题 在这个特定的例子中,我在A列中得到了一个电子邮件列表,我希望该电子邮件地址在其旁边的单元格更新为“是”时收到一个通知 到目前为止,我得到了这个: function Initialize() { var triggers = ScriptApp.getProjectTriggers(); for(var i in triggers) { ScriptApp.deleteTrigger(triggers[i]); }

我希望在我尝试做标题所说的事情之前,没有人问过我这个问题

在这个特定的例子中,我在A列中得到了一个电子邮件列表,我希望该电子邮件地址在其旁边的单元格更新为“是”时收到一个通知

到目前为止,我得到了这个:

function Initialize() {

  var triggers = ScriptApp.getProjectTriggers();

  for(var i in triggers) {
    ScriptApp.deleteTrigger(triggers[i]);
  }

  ScriptApp.newTrigger("sendNotification")
  .forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
  .onEdit()
  .create();

};


/**
 * 
 */


function sendNotification(e) {


  if("B2" == e.range.getA1Notation() ) {

    if(e.value == "Yes") {



      var recipients = "IDK what to put in here";
      var subject = "There's an update to your request";
      var body = "We resolved your issue!";


      MailApp.sendEmail(recipients, subject, body);
    }
  }
}

我已经用注释编辑了代码,解释了我所做的更改,您的思路是正确的。OnEditTriggers发送电子邮件通常是不受欢迎的,通常不是一个好的做法。然而,在您的情况下,您仍然可以控制何时发送电子邮件,因此,在每次编辑时,您都可以通过直接发送电子邮件来进行改进

仅供参考您不需要Initialize()来设置手动触发器您可以手动设置:

新功能:

e.range.getColumn()/getRow()以获取特定的列或行。这样你就可以处理数字而不是字符串。与字符串相比,易于使用算术操作数字


e.source.getActiveSheet()为您提供更改发生位置的sheet对象。您可以使用e.source.getActiveSheet().getName()获取工作表的名称,并使用它确保只有在特定工作表上进行编辑时才会触发电子邮件

我终于让它工作了,我对ir做了一些修改,这样它就可以和我正在处理的一些工作表一起工作了,但是非常感谢!我快发疯了,不知道如何正确使用e.range.getColumn/row很高兴我能帮上忙
 function Initialize() {
      var triggers = ScriptApp.getProjectTriggers();
      // The below code would delete all the triggers in your project not just the one you setup through this code.  
      for(var i in triggers) {
        ScriptApp.deleteTrigger(triggers[i]);
      }
      ScriptApp.newTrigger("sendNotification")
       .forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
       .onEdit()
       .create();
      };

   function sendNotification(e) {
      //if("B2" == e.range.getA1Notation() ) {
      //This will only check is the range is B2 cell, if you want to check for  column B. use getColumn
      if(e.range.getColumn() == 2){
        if(e.value == "Yes") {   
          //var recipients = "xxx@gmail.com"; //Email Address of the indented Recipents 
          // In your case email would be the value in cell A2 or cell in Column A corresponding to the one edited in Col B
          // you can get that value like so
          var recipients = e.source.getActiveSheet().getRange(e.range.getRow(),1).getValue()
          Logger.log(recipients)
          var subject = "There's an update to your request";
          var body = "We resolved your issue!";
          MailApp.sendEmail(recipients, subject, body);
        }
      }
    }