[英]In Google Apps Script, how to send an email only one time if condition met
[英]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.