简体   繁体   English

Google Apps 脚本 - 满足条件时发送非重复 email

[英]Google Apps Script - Send non repetitive email when condition met

function Buy() {
  // Fetch the values from column A
 var values = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Buy").getRange("A2:A7").getValues();

  if (values <= -5){
    // Fetch the email address
    var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Email").getRange("E2");
    var emailAddress = emailRange.getValue();
    // Send Alert Email.
    var message = 'Buy Goods ' + values; // Second column
    var subject = 'Buys';
    MailApp.sendEmail(emailAddress, subject, message);
  } 

}


Example Google Sheet Data in Column A - A2:A7 A 列中的示例 Google 表格数据 - A2:A7

enter image description here在此处输入图像描述

I am running above script but can't get the email where i am going wrong我在脚本上面运行,但无法获得 email 我出错的地方

Expected outcome in Email: Email中的预期结果:

Buy Goods购买商品

Apples Berries苹果浆果

function Buy() {
  // Fetch the values from column A
 var values = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Buy").getRange("A2:A7").getValues();

  if (values <= -5){
    // Fetch the email address
    var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Email").getRange("E2");
    var emailAddress = emailRange.getValue();
    // Send Alert Email.
    var message = 'Buy Goods ' + values; // Second column
    var subject = 'Buys';
    MailApp.sendEmail(emailAddress, subject, message);
  } 

}

I a expected this to return values and send email我希望它返回值并发送 email

Try this:试试这个:

function Buy() {
  const ss = SpreadsheetApp.getActive();
  const bsh = ss.getSheetByName("Sheet0");
  const esh = ss.getSheetByName("Sheet1");
  const values = bsh.getRange("A2:B7").getValues();
  const email = esh.getRange("E2").getValue();
  values.forEach((v, i) => {
    if (v[0] <= -5) {
      var message = 'Buy Goods ' + v[1]; // Second column
      var subject = 'Buys';
      MailApp.sendEmail(emailAddress, subject, message);
    }
  })
}

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

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