我在Google工作簿中有一个Google Spreadhsheet(“Trigger”),其中有三列:
这些列的值从第2行开始,到目前为止,我只需要针对此单行的解决方案。
每当单元格C2
中的值为"yes"
时(此处的计算基于当前股票价格),我想向A2
中的电子邮件帐户发送电子邮件在B2
中,主题为"Price has been reached? ==" & C2
到目前为止我(我对此很新,但在VBA方面有一些经验)是这样的:
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 1; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var subject = "Price has been reached? ==";
MailApp.sendEmail(emailAddress, subject, message);
}
}
这适用于发送电子邮件,但不考虑C2
中的值。我怎样才能做到这一点?
此外,我想知道如何编写此脚本应仅链接到电子表格“触发器”而不是此工作簿中的其他工作表?
一旦这个工作,我可以在脚本编辑器中定义一个正确的触发器,然后考虑如何使这个工作多行。
答案 0 :(得分:2)
某些功能(例如GOOGLEFINANCE
)不会导致onEdit或onChange触发。
您需要创建基于时间的触发器,该触发器经常检查(最多每分钟一次)
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Trigger"); // To only handle the trigger sheet
var startRow = 2; // First row of data to process
var numRows = 1; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
if (row[2] === "Yes") { // Trigger only if Column C is "Yes"
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var subject = "Price has been reached? ==" + row[2]; // Add "Yes" although by your trigger logic it will always say yes in the email
MailApp.sendEmail(emailAddress, subject, message);
}
}
}
然后在脚本编辑器中设置触发器。