我有一个电子表格,在其中我在K列中有电子邮件地址,然后在A列至D列中有数据。我正在尝试编写一个脚本,该电子邮件将通过电子邮件将第K列中的所有电子邮件地址发送出去。 C,电子邮件正文应为D栏的最后一行
下面是我所拥有的,但是我有两个问题。每当我需要在列表中添加电子邮件地址时,都需要调整脚本var dataRange = sheet.getRange("K4:K9");
以在K10中包含新的电子邮件地址。有一个更好的方法吗?
我也尝试对电子邮件主题和正文使用getLastRow,它可以工作,但它只是获取dataRange中的最后一行。我每天必须更新var dataRange2= sheet.getRange("A4:D6");
,以添加新行。我试图使dataRange变大,但随后它抓住了空白行作为最后一行。同样,我不确定如何使它自动化。
/**
* Sends emails with data from the current spreadsheet.
*/
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 3; // First row of data to process
var numRows = 2; // Number of rows to process
// Fetch the Email range of cells K4:K6
var dataRange = sheet.getRange("K4:K9");
var emailAddress = dataRange.getValues() // Email Col
var dataRange2= sheet.getRange("A4:D6");
var message = SpreadsheetApp.getActiveSheet().getRange("D" + dataRange2.getLastRow()).getValues(); // Fact Col
var subject = SpreadsheetApp.getActiveSheet().getRange("C" + dataRange2.getLastRow()).getValues(); // Question Col
MailApp.sendEmail(emailAddress, subject, message);
}
答案 0 :(得分:2)
使用“ for”循环遍历数据的每一行。如果您要为包含电子邮件地址的每一行发送一封电子邮件。
运行代码,然后查看日志。在代码编辑器中,单击“查看”菜单,然后单击“日志”。
出于测试目的,您可能需要注释掉发送电子邮件的语句,运行代码并查看日志。
function sendEmails2() {
var data,dataRange,emailAddress,i,L,lastRow,message,sheet,startRow,subject,thisRow;
sheet = SpreadsheetApp.getActiveSheet();
lastRow = sheet.getLastRow();
startRow = 3; // First row of data to process
dataRange = sheet.getRange(startRow, 1, lastRow - startRow, 11);//Start in column 1 and get 11 columns of data
data = dataRange.getValues();//Get a 2D array of data
L = data.length;//The number of inner arrays in the 2D array - which is
//the number of rows in the range
for (i=0;i<L;i++) {
thisRow = data[i];//Get one inner array of data which represents one row
subject = thisRow[2];//The subject is in column 3 which is index 2
message = thisRow[3];//The message is in column 4 which is index 3
emailAddress = thisRow[10];
Logger.log("subject: " + subject)
Logger.log("message: " + message)
Logger.log("subject: " + subject)
if (!emailAddress) {
continue;
}
MailApp.sendEmail(emailAddress, subject, message);
}
}