Google 工作表的电子邮件脚本
Email Script for Google Sheets
我需要有关我在网上找到的电子邮件脚本的帮助。如果您想向 B 列中的所有人发送电子邮件,我发现该脚本有效。我在 A 列中创建了一个部分,询问我是否要向该个人发送电子邮件(因为我不想向所有人发送电子邮件),但我不想确定如何更改编码以识别这一点。我还在努力learn/understandJavaScript。任何帮助都会很棒。
谢谢。
您可以在下面查看我的sheet:
https://docs.google.com/spreadsheets/d/1-lcCUN7ROBAhWNFsqura5M7LvhsQnzBBqE06jxxQkFM/edit?usp=sharing
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange(1,5); // Fetch the range of cells E1:E1
var CC = range.getValues();
var range = sheet.getRange(2,5); // Fetch the range of cells E2:E2
var subject = range.getValues(); // Fetch value for subject line from above range
var range = sheet.getRange(2, 9); // Fetch the range of cells I2:I2
var numRows = range.getValues(); // Fetch value for number of emails from above range
var startRow = 5; // First row of data to process
var dataRange = sheet.getRange(startRow, 2, numRows,8 ) // Fetch the range of cells B5:I_
var data = dataRange.getValues(); // Fetch values for each row in the Range.
for (i in data) {
var row = data[i];
var emailAddress = row[0]; // 1st column
var message = row[7]; // 8th column
MailApp.sendEmail({
to: emailAddress,
cc: CC,
subject: subject,
htmlBody: message,
})}
}
}
试试下面的代码:
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange(1,5); // Fetch the range of cells E1:E1
var CC = range.getValues();
var ccEmail = CC[0][0];
var range = sheet.getRange(2,5); // Fetch the range of cells E2:E2
var subject = range.getValues(); // Fetch value for subject line from above range
var emailSubject = subject[0][0];
var range = sheet.getRange(2, 9); // Fetch the range of cells I2:I2
var numRows = range.getValues(); // Fetch value for number of emails from above range
var startRow = 5; // First row of data to process
var endRow = numRows[0][0];
var dataRange = sheet.getRange(startRow, 2, endRow,8 ) // Fetch the range of cells B5:I_
var data = dataRange.getValues(); // Fetch values for each row in the Range.
for (i in data) {
var row = data[i];
var sendEmail = row[0];
if (sendEmail == "Yes"){
var emailAddress = row[1]; // 2nd column
var message = row[7]; // 8th column
MailApp.sendEmail({ to: emailAddress, cc: ccEmail , subject: emailSubject, htmlBody: message});
}
}
}
编辑代码,因为 getRange() 方法获取二维数组中的值,您必须从数组中获取第一个值。
希望对您有所帮助!
我需要有关我在网上找到的电子邮件脚本的帮助。如果您想向 B 列中的所有人发送电子邮件,我发现该脚本有效。我在 A 列中创建了一个部分,询问我是否要向该个人发送电子邮件(因为我不想向所有人发送电子邮件),但我不想确定如何更改编码以识别这一点。我还在努力learn/understandJavaScript。任何帮助都会很棒。
谢谢。
您可以在下面查看我的sheet:
https://docs.google.com/spreadsheets/d/1-lcCUN7ROBAhWNFsqura5M7LvhsQnzBBqE06jxxQkFM/edit?usp=sharing
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange(1,5); // Fetch the range of cells E1:E1
var CC = range.getValues();
var range = sheet.getRange(2,5); // Fetch the range of cells E2:E2
var subject = range.getValues(); // Fetch value for subject line from above range
var range = sheet.getRange(2, 9); // Fetch the range of cells I2:I2
var numRows = range.getValues(); // Fetch value for number of emails from above range
var startRow = 5; // First row of data to process
var dataRange = sheet.getRange(startRow, 2, numRows,8 ) // Fetch the range of cells B5:I_
var data = dataRange.getValues(); // Fetch values for each row in the Range.
for (i in data) {
var row = data[i];
var emailAddress = row[0]; // 1st column
var message = row[7]; // 8th column
MailApp.sendEmail({
to: emailAddress,
cc: CC,
subject: subject,
htmlBody: message,
})}
}
}
试试下面的代码:
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange(1,5); // Fetch the range of cells E1:E1
var CC = range.getValues();
var ccEmail = CC[0][0];
var range = sheet.getRange(2,5); // Fetch the range of cells E2:E2
var subject = range.getValues(); // Fetch value for subject line from above range
var emailSubject = subject[0][0];
var range = sheet.getRange(2, 9); // Fetch the range of cells I2:I2
var numRows = range.getValues(); // Fetch value for number of emails from above range
var startRow = 5; // First row of data to process
var endRow = numRows[0][0];
var dataRange = sheet.getRange(startRow, 2, endRow,8 ) // Fetch the range of cells B5:I_
var data = dataRange.getValues(); // Fetch values for each row in the Range.
for (i in data) {
var row = data[i];
var sendEmail = row[0];
if (sendEmail == "Yes"){
var emailAddress = row[1]; // 2nd column
var message = row[7]; // 8th column
MailApp.sendEmail({ to: emailAddress, cc: ccEmail , subject: emailSubject, htmlBody: message});
}
}
}
编辑代码,因为 getRange() 方法获取二维数组中的值,您必须从数组中获取第一个值。 希望对您有所帮助!