Google Apps脚本电子邮件通知无法正常运行

时间:2015-05-04 16:44:21

标签: google-apps-script google-apps

我真的可以使用一些帮助,我真的很感激任何人都可以提供的指示。我知道还有一些其他类似的问题,但是根据我的发现,没有其他人有这个具体的问题/请求。

我有一张Google Sheet,其中有三位不同的人合作。我有这个通知脚本,它将通过电子邮件通知特定用户有关F列中任何单元格的任何更改。重要的是,此电子邮件通知还包含该行中已更改F单元格值的A单元格的值。

然而,我真正想要实现的是一个通知脚本,当F列中的任何单元格的新值为&#34时,它将通过电子邮件发送指定人 ;是&# 34;在主表的Sheet2中。

我到目前为止的代码是(在F列中发生任何更改时发送通知):

function sendNotification(e) {
  var received = e.value; //Gets the value of the edited cell
};
    function sendNotification() {
          var ss = SpreadsheetApp.getActiveSpreadsheet();
          var sheet = ss.getSheetByName("Sheet 2");
          var cell = ss.getActiveCell().getA1Notation();
          var row = sheet.getActiveRange().getRow();
          var cellvalue = ss.getActiveCell().getValue().toString();
          var received = ss.getRange('F2:F').getValue();
          var recipients = "example@gmail.com";
          var message = '';
          if(received==="Yes"){ 
            message = sheet.getRange('A'+ sheet.getActiveCell().getRowIndex()).getValue()
            var subject = 'A cell value '+ sheet.getRange('A'+ sheet.getActiveCell().getRowIndex()).getValue() + ' has been received';
            var body = sheet.getName() + ' has been updated. Visit ' + ss.getUrl() + ' to view the changes on row: «' + row + '». New comment: «' + cellvalue + '». For message: «' + message + '»';
           MailApp.sendEmail(recipients, subject, body)};
        Logger.log('received value: ' + received);
        };

2 个答案:

答案 0 :(得分:1)

If it helps, below is a code that works in my tests : (set up an installable trigger to make it work)

note : I added a couple of Logger.log here and there to check the variables and a Browser message to avoid sending mails while testing.

function sendNotification() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Sheet 2");
  var cell = ss.getActiveCell();
  var row = sheet.getActiveRange().getRow();
  var col = cell.getA1Notation().replace(/[0-9]/g,'').toLowerCase();
  Logger.log('col = '+col);
  var cellValue = cell.getValue().toString().toLowerCase();
  Logger.log(cellValue);
  var recipients = "example@gmail.com";
  var message = '';
  Logger.log(cellValue=="yes" && col=='f');
  if(cellValue=="yes" && col=='f' ){ 
    message = sheet.getRange('A'+ sheet.getActiveCell().getRowIndex()).getValue()
    var subject = 'A cell value '+ sheet.getRange('A'+ sheet.getActiveCell().getRowIndex()).getValue() + ' has been received';
    var body = sheet.getName() + ' has been updated. Visit ' + ss.getUrl() + ' to view the changes on row: «' + row + '». New comment: «' + cellValue + '». For message: «' + message + '»';
//    MailApp.sendEmail(recipients, subject, body)
    Browser.msgBox('mail sent');
  }
    Logger.log('message body: ' + body);
};

答案 1 :(得分:0)

要设置事件对象,请使用:

function sendNotification(e) {
  var received = e.value; //Gets the value of the edited cell
};

发送电子邮件的代码是if正文的 。所以无论如何都会发送电子邮件。

更改为:

if(received==="Yes"){ 
  message = sheet.getRange('A'+ sheet.getActiveCell().getRowIndex()).getValue()

  var subject = 'A cell value '+ sheet.getRange('A'+ sheet.getActiveCell().getRowIndex()).getValue() + ' has been received';
  var body = sheet.getName() + ' has been updated. Visit ' + ss.getUrl() + ' to view the changes on row: «' + row + '». New comment: «' + cellvalue + '». For message: «' + message + '»';
  MailApp.sendEmail(recipients, subject, body);
};

在这行代码中:

if(received="Yes"){ 

您正在使用赋值运算符来检查是否相等。平等检查用双重或三等号完成。

应该是:

if(received==="Yes"){