简体   繁体   English

Google 表格 - 发送 Email

[英]Google Sheet - Send Email

I need my google sheet to send emails every time a condition becomes true.每次条件变为真时,我都需要我的 google 表格来发送电子邮件。 In this case, every time value of C2 is lower than value of J2.在这种情况下,每次 C2 的值都低于 J2 的值。 On the cell L2 there is the email address.在单元格 L2 上有 email 地址。

The code (found online and just edited)代码(在网上找到并刚刚编辑)

function CheckPrice() {
  var LastPriceRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Alerts").getRange("C2"); 
  var LastPrice = LastPriceRange.getValue();
  var EntryLimitRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Alerts").getRange("J2"); 
  var EntryLimit = LastPriceRange.getValue();
  var StockNameRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Alerts").getRange("B2"); 
  var StockName = LastPriceRange.getValue();
  // Check totals sales
  if (LastPrice < EntryLimit){
    // Fetch the email address
    var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Alerts").getRange("L2");
    var emailAddress = emailRange.getValues();
  
    // Send Alert Email.
    var message = 'Ticker ' + StockName + ' has triggered the alert';
    var subject = 'Stock Alert';
    MailApp.sendEmail(emailAddress, subject, message);
    }
}

With this code I don't receive any error, but I don't even receive the email. I granted permissions as requested when I run the script for the first time.使用此代码我没有收到任何错误,但我什至没有收到 email。我在第一次运行脚本时按要求授予了权限。

On L2 I put the same email address I granted permission (I send email to myself).在 L2 上,我放置了我授予权限的相同 email 地址(我将 email 发送给自己)。 I did a try even putting a secondary email address I have.我什至尝试放置一个辅助 email 地址。

Can you please show me what's wrong?你能告诉我哪里出了问题吗?

Issues:问题:

  • See the first lines of your code where you define LastPrice , EntryLimit and StockName .查看您定义LastPriceEntryLimitStockName的代码的第一行。 All of them are coming from the same range: LastPriceRange .他们都来自同一个范围: LastPriceRange

  • I also removed all the unnecessary calls in your script.我还删除了脚本中所有不必要的调用。 There is no need to call SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Alerts") so many times when you can just put it in a variable and use that variable instead.无需多次调用SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Alerts") ,您只需将其放入一个变量并使用该变量即可。

  • Also, you don't need to define unnecessary variables.此外,您不需要定义不必要的变量。 For example, you can get the value of a cell with one line: sh.getRange("B2").getValue() .例如,您可以使用一行获取单元格的值: sh.getRange("B2").getValue()

Solution:解决方案:

function CheckPrice() {
  
  const sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Alerts");   
  const LastPrice = sh.getRange("C2").getValue();
  const EntryLimit = sh.getRange("J2").getValue();
  const StockName = sh.getRange("B2").getValue(); 

  // Check totals sales
  if (LastPrice < EntryLimit){
    // Fetch the email address
    const emailAddress = sh.getRange("L2").getValue();
  
    // Send Alert Email.
    const message = 'Ticker ' + StockName + ' has triggered the alert';
    const subject = 'Stock Alert';
    MailApp.sendEmail(emailAddress, subject, message);
    }
}

If you want an email to be sent when you edit a particular cell, then you need to transform the aforementioned solution to an onEdit(e) trigger.如果您希望在编辑特定单元格时发送 email,则需要将上述解决方案转换为onEdit(e)触发器。 If you want a time basis trigger then you can just use the solution above directly.如果你想要一个时基触发器,那么你可以直接使用上面的解决方案。

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

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