简体   繁体   English

谷歌应用脚本表 if 语句发送 email

[英]Google apps script sheets if statement to send email

I have a sheet, and in cell B1 I have a formula that states "TRUE" if certain conditions have been met on the sheet.我有一张工作表,在单元格 B1 中我有一个公式,如果工作表上满足某些条件,则显示“TRUE”。

I have a time based trigger set up on my script to send me an email every 6 hours if B1 states TRUE.我在脚本上设置了一个基于时间的触发器,如果 B1 状态为 TRUE,则每 6 小时向我发送一个 email。

Here is the script I am using:这是我正在使用的脚本:

function sendEmails2() {
   var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Function2")
   var range = sheet.getRange("B1")   

   if (range = "TRUE"){
      var emailAddress = "email@email.com"  
      var message = "Please check your MCR Tracker as it has new entries that neeed reviewing: URL" 
      var subject = "Your MCR Tracker has new entries"
      MailApp.sendEmail(emailAddress,"noreply@ocado.com", subject, message)
    }
}  

When I manually run this script to test, it is sending an email, whether the condition has been met or not, rather than only if the condition has been met.当我手动运行此脚本进行测试时,它正在发送 email,无论是否满足条件,而不是仅在满足条件时。

I'm very new to apps script, some help on where I have gone wrong would be appreciated!我对应用程序脚本非常陌生,如果我出错了,我将不胜感激!

The problem is that in the condition of the if statement you are using an assignment operator ( = ) instead of a comparison operator ( == ) and also that you should first get the value of range to compare it to true.问题是在if语句的条件下,您使用的是赋值运算符 ( = ) 而不是比较运算符 ( == ),而且您应该首先获取range的值以将其与 true 进行比较。

Try replacing尝试更换

if (range = "TRUE"){
   ...
}

with

if (range.getDisplayValue() === "TRUE"){
   ...
}

Since you're using a formula to display "TRUE" you should add.getDisplayValue() when you set the initial value of your range variable.由于您使用公式来显示“TRUE”,因此您应该在设置范围变量的初始值时添加.getDisplayValue()。 Like this:像这样:

Replace var range = sheet.getRange("B1")替换var range = sheet.getRange("B1")

With var range = sheet.getRange("B1").getDisplayValue();使用var range = sheet.getRange("B1").getDisplayValue();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM