0
votes

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

1
Your code looks very similar to this. Are the answers helpful to you?Jescanellas

1 Answers

2
votes

Range's do not have a replace method. I think you want to get the display value and replace on that.

var template = activesheet.getRange(1,9).getDisplayValue();