I'm struggling around to send email to myself with content of a table 'row by row'. My needs are simple: I count a column and if the value is greater than 0, then my Google Trigger will check this (periodically) and send me an email. This works.
But I am not able to get the content of the lines (rows) into the 'message' to the Email, something like
Introtext:
Screenshot of my table attached. The range I ask for in email message is 'A3:D' (if not blank). So this is my simple code for now:
function sendEmail() {
// Fetch due Today
var myRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("DueToday").getRange("D1");
var todaydue = myRange.getValue();
// for now I check only one cell - but would like to get the set of rows (line by line)
var myDetailsRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("DueToday").getRange("B3:B3");
var myDetails = myDetailsRange.getValues();
var result = [].concat.apply([], myDetails).filter(String);
// Check totals due today
if (todaydue > 0) {
// Send Alert Email
var emailAddress = 'mymail@gmail.com'
var subject = 'JRFgo² Reminder: ' + todaydue+ ' due today';
var message = '‣ '+result+ ;
GmailApp.sendEmail(emailAddress, subject, message);
}
}
try this:
function sendEmail() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("DueToday");//your Sheet name
const lastRow = sheet.getLastRow();
let message = "";
const headersLength = 2;
//if lastRow is higher than 2, means there are rows with tasks
if (lastRow > headersLength){
//get all data range with tasks
const data = sheet.getRange(3,1,lastRow - headersLength,sheet.getLastColumn()).getValues();
//Loop through each row to build the message
for (var i = 0; i < data.length; i++) {
message+= `Row ${i+1} Data: ${data[i]}\n\n`
}
const subject = `JRFgo² Reminder: ${lastRow-headersLength} due today`;
const emailAddres = "YOUR EMAIL ADDRESS";
GmailApp.sendEmail(emailAddres, subject, message);
}
}
This approach does not need the value from D1 to work since it checks the length of the sheet. Please note that if you remove or add more rows above the headers, you will need to update the headersLength value to match the number of rows to exclude from the data range with tasks