现在我正在尝试设置一个可以帮助我的同事更新库存的功能。我需要一个程序,一旦股票更新完成,股票通知的电子邮件将发送给经理。
我试图在没有触发器的情况下运行该程序,我收到了一个完美的电子邮件,但现在我需要对程序应用触发器,所以当我的同事更新库存时,她不需要运行程序来发送电子邮件。
我只是想知道如何才能更好地触发电子邮件发送,而不是每次更新都会触发程序运行并发送多封电子邮件。另外请告诉我如何设置范围'e'以及如何设置触发器工作
由于
这是代码
function onEdit(e) {
//if (activeSheet.getName() !== "inventory") return;
//var sheet = SpreadsheetApp.getActiveSheet();
var sheet = e.source;
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var workRows = numRows -8;
var dataRange = sheet.getRange(8,2, workRows, 13)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
//email information and content
var emailAddress = 'xxx@gmail.com';
var bodyrefill = '';
var bodyset = '';
var bodysend = '';
var subject = '';
var check1 = 0;
var check2 = 0;
/*
var range = e.range;
var columnOfCellEdited = range.getColumn();
if (columnOfCellEdited==14){
columnofCellEdited.setNote('Last modified: ' + new Date());
}
*/
for (var row in data){
if(data[row][12]!=0){
if (data[row][11]<=data[row][12]){
check1 += 1;
var num = data[row][12]-data[row][11];
bodyrefill = bodyrefill + "The " + row + " line, " + data[row][2] + " needs refill product for" + num + "\n";
}
}
else{
check2 += 1;
bodyset = bodyset + "The " + row + " line, product " + data[row][2] + " needs to set up a product limit." + "\n";
}
}
if (check1 > 0){
if (check2 > 0){
subject = "Google sheet has products need to refill and set up alart limit.";
bodysend = bodyrefill + bodyset;
}
else{
subject = "Google sheet has products need to refill.";
bodysend = bodyrefill;
}
}
else{
subject = "Google sheet has products need to set up alart limit.";
bodysend = bodyset;
}
MailApp.sendEmail(emailAddress, subject, bodysend);
SpreadsheetApp.flush();
}