Question: This is in JavaScript on Google Scripts. How do I get this to work? It says I have an error when I am trying to
This is in JavaScript on Google Scripts.
How do I get this to work? It says I have an error when I am trying to replace; this is basically a simple mail merge program I wanted to try out.
function myFunction() {
var sheet = SpreadSheetApp.getActiveSheet(); //getting the active sheet that you are on
var data = sheet.getDataRange().getValues(); //getting the values for that sheet
var draft = MailApp.getDraftMessages()[1]; //make sure that it is the first draft in your drafts folder
var body = draft.getPlainBody(); //getting body of email - no HTML formatting
for (var i = 2; i < sheet.getMaxRows(); i++) {
var newDraft = draft; //copying everything into new draft
var name = data[i][0]; //name should be first column
var country = data[i][2]; //country should be third column
var university = data[i][3]; //university should be fourth column
var newDraft.replace("firstName", name); //replacing everything
var newDraft.replace("thisCountry", country);
var newDraft.replace("thisSchool", university);
MailApp.sendEmail(data[i][1], draft.getSubject(), draft); //sends email
delete newDraft;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
