发送工作表中匹配的单元格值的电子邮件

时间:2018-10-07 12:02:49

标签: google-apps-script google-sheets

当Google脚本中所有行的第8列的值与第3列的值匹配时,我正在尝试发送电子邮件。

function sendEmailNotification() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var lrow=ss.getLastRow()
  var sheet = SpreadsheetApp.getActiveSheet();

  for (var i = 2; i < lrow; i++)
  {
    if (sheet.getRange(i, 8).getValue() = sheet.getRange(i, 3).getValue())

      var emailSubject =  'Value of ' + sheet.getRange(i, 1).getDisplayValue() + ' triggered, now pending your attention';
    var emailBody = 'Person1 has approved the item on row ' + ' of spreadsheet "' + '".\n\n';
    emailBody += 'To open the spreadsheet, click this link: ' + ss.getUrl() + '\n\n';
    emailBody += '(this is an automatically sent message)';

    MailApp.sendEmail("sdas22@gmail.com", emailSubject, emailBody);
;  }}

1 个答案:

答案 0 :(得分:0)

主要问题是IF语句

这是您的,它不起作用并且正在生成错误消息

if (sheet.getRange(i, 8).getValue() = sheet.getRange(i, 3).getValue())

这是我的作品

 if ((sheet.getRange(i, 8).getValue()) == (sheet.getRange(i, 3).getValue())){

    }

需要注意的几件事。

  • 比较运算符为“ ==”而不是“ =”
  • 每个getValue语句都封装在方括号中
  • 没有“花括号”来表示IF语句的目的。这一点特别重要。

最后一行也有分号,看起来似乎不合适。


这是我的代码版本-运行

function sendEmailNotification() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var lrow=ss.getLastRow()
  var sheet = SpreadsheetApp.getActiveSheet();

  for (var i = 2; i < lrow; i++)
  {

    if ((sheet.getRange(i, 8).getValue()) == (sheet.getRange(i, 3).getValue())){

        var emailSubject =  'Value of ' + sheet.getRange(i, 1).getDisplayValue() + ' triggered, now pending your attention';
        var emailBody = 'Person1 has approved the item on row ' + ' of spreadsheet "' + '".\n\n';
        emailBody += 'To open the spreadsheet, click this link: ' + ss.getUrl() + '\n\n';
        emailBody += '(this is an automatically sent message)';
        MailApp.sendEmail("sdas22@gmail.com", emailSubject, emailBody);
    }
  }
}