I have an email script that is functioning correctly, but I only want it to run through the last row with data in it. If there is no data in a row, then I do not need the script to run for that specific row. How can I modify the example script below to reflect this requirement?
For instance, if there are only 3 rows of data in my spreadsheet, I want the script to process those 3 rows and not continue beyond that. However, as the number of entries may vary each day, I cannot set a fixed number.
Essentially, I am trying to replace:
var numRows =20;// Number of rows to process
with some iteration using .getlastrow...I believe?
Here is the sample script for your reference:
function sendEmails() {
var sheet =SpreadsheetApp.getActiveSheet();
var startRow =2;// First row of data to process
var numRows =20;// 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 ="Sending emails from a Spreadsheet";
MailApp.sendEmail(emailAddress, subject, message);
}
}
Thank you for assisting me!