簡體   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);
  } 

}


A 列中的示例 Google 表格數據 - A2:A7

在此處輸入圖像描述

我在腳本上面運行,但無法獲得 email 我出錯的地方

Email中的預期結果:

購買商品

蘋果漿果

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);
  } 

}

我希望它返回值並發送 email

試試這個:

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