我在 Google 电子表格上编写了脚本,以便在修改电子表格或添加任何数据时发送电子邮件。电子邮件触发器正在工作,但每当在下一行中输入任何数据时,它也会将电子邮件发送到以前的电子邮件地址。
请提出解决方案
以下是编写的脚本:
function onEdit(e) {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 1; // Number of rows to process
var dataRange = sheet.getRange(startRow, 1 , numRows,3) // Fetch the range of cells A2:B3
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[2]; // First column
var message = row[0] + "requested" + row [1]; // Second column
var subject = "Sending emails from a Spreadsheet";
MailApp.sendEmail(emailAddress, subject, message);
}
}