I want to send the email to the corresponding person with a attached table of that row based on the cell value. For example, In the sheet, when the the column D==="New" and "Ongoing", an email will send to the recipients of C column with a generated table of that row. I don't want onedit trigger as I will send the email only once in a week. When table will generate, it will only take column Resource, status, Due date and Link. I have write the code for sending email and as well creating a table. The problem is when I am creating the table, It is taking all the data where status is New. For this reason, all the recipients get the same table but I want the table in a way that the recipient will get only his corresponding row's table. I have attached my code here. Can anyone one suggest what should I add here? Sheet link: https://docs.google.com/spreadsheets/d/1GC59976VwB2qC-LEO2sH3o2xJaMeXfXLKdfOjRAQoiI/edit#gid=0
Code:
function myFunction() {
var ss=SpreadsheetApp.getActiveSpreadsheet()
var sheet=ss.getSheetByName("Sheet3")
var sheetData=sheet.getRange(2,1,sheet.getLastRow()-1,sheet.getLastColumn()).getValues()
var filteredRows_new = sheetData.filter(function(row){
if (row[3] === 'New') {
return row
}
})
// create the html table
var header=sheet.getRange(1,1,1,sheet.getLastColumn()).getValues()
var resource=header[0][1]
var status_r=header[0][3]
var due_date_r=header[0][5]
var link_r=header[0][10]
var data=filteredRows_new.map(col=>[col[1],col[3],col[5],col[10]])
var htmltemplate=HtmlService.createTemplateFromFile("email")
htmltemplate.resource=resource
htmltemplate.status_r=status_r
htmltemplate.due_date_r=due_date_r
htmltemplate.link_r=link_r
htmltemplate.data=data
var htmlformail=htmltemplate.evaluate().getContent()
console.log(htmlformail)
// extract only the new status data for email
filteredRows_new.forEach(function(row) {
//Email address
var email = row[2];
GmailApp.sendEmail(email,"automation","open the link for html", { htmlBody: htmlformail })
})
//extract the rows for ongoing
var filteredRows_ongoing = sheetData.filter(function(col){
if (col[3] === 'ongoing') {
return col
}
})
//console.log(filteredRows_ongoing)
}