我对 Google Apps Script/Javascript 很陌生,所以这可能是一个非常简单的答案。当电子表格中的某些单元格值发生更改时,我正在尝试发送几封电子邮件。下面是我的工作代码。我已将此设置为运行 onEdit。但是,每当修改工作表上的单元格时,它都会发送更新,而我只想在修改某些单元格(余额)时发送一封电子邮件。
function sendUpdate() {
var recipients=["User1@gmail.com","User2@email.com"];
var sheet = SpreadsheetApp.getActiveSheet();
// Get the range of cells that store balances
var sslink = SpreadsheetApp.getActiveSpreadsheet().getUrl()
var Balances = sheet.getRange("A28:C31").getValues();
var User1Balance = Balances[2][2];
var User2Balance = Balances[3][2]
var User3Balance = Balances[0][2]
var User4Balance = Balances[1][2]
for (var i = 0; i < 2; i++) {
GmailApp.sendEmail(recipients[i],
'Utilities Spreadsheet updated',
"This is an automated email generated from the utilities spreadsheet being updated. " +
"Current balances are: " +
"\n\n User1: " + User1Balance + "\n User2: " + User2Balance + "\n User3: " + User3Balance + "\n User4: " + User4Balance +
"\n\n Access the spreadsheet at: " + sslink);
}
};
我尝试使用 onOpen 函数读取初始值,然后在每次编辑工作表时比较这些值。但是,我在将初始数据传递到上面的主函数时遇到了一些问题。任何关于如何去做的建议都会很棒。我认为使用全局变量会很容易,但不确定这是否可能。