I have a spreadsheet where I have email addresses in column K and then data in columns A through D. I am trying to write a script that will email all email addresses in col K. The subject should be the last Row in Col C and the email Body should be the last row in col D
The Below is what I have but I have two issues. Whenever I need to add an email address to the list I will need to Adjust the script var dataRange = sheet.getRange("K4:K9");
to contain the new email address in K10. Is there a better way to do this?
Also I tried using getLastRow for the email Subject and Body and it works but it is just getting the last row in the dataRange. I have to update var dataRange2= sheet.getRange("A4:D6");
everyday a new line is added. I tried to make the dataRange larger but then it was grabbing the blank rows as the last row. Again I am not sure how to make this automated.
/**
* 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);
}
getRange
, namely the one that takes 4 parameters. It will work well withgetLastRow()
. – tehhowch