I was looking to send an automated mailing system using a google sheet. For sending out the template I stored the template in one cell and tried to replace a variable text in the cell with a range of cells using for loops. However my .replace function is not working and showing an error. TypeError: Cannot find function replace in object Range. (line 18, file "Automated mailing")
Below is the code -
function Automatedmailler() {
var app = SpreadsheetApp;
//Accessing VM reminder sheet
var activesheet = app.getActiveSpreadsheet().getSheetByName("VM Reminder Eligibility");
//Finding lastrow
var lastrow = activesheet.getLastRow();
var template = activesheet.getRange(1,9);
for (var i=2;i<=lastrow;i++){
var emailid = activesheet.getRange(i,3).getValue();
var subject = activesheet.getRange(i,4).getValue();
var name = activesheet.getRange(i,2).getValue();
var bodylink = activesheet.getRange(i,5).getValue();
//var body = "<td><nobr><p> Hi"+ " </td></nobr></p>"+name;
var body = template.replace("%name%",name)
MailApp.sendEmail(emailid, subject, body,{htmlBody:body});
}
}
Here is the link to the sheet - https://docs.google.com/spreadsheets/d/1yH70WQLKzMzu4jMLbIdxivUTd4M6rTT8FTQEDPGEgV8/edit#gid=0