Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

So this is not working correctly, this is my sheet: and my google scripts: funct

ID: 3858653 • Letter: S

Question

So this is not working correctly, this is my sheet:

and my google scripts:

function myFunction() {

var sheet = SpreadsheetApp.getActiveSheet(); //getting the active sheet that you are on -- returns spreadsheet object

var draft = MailApp.getDraftMessages()[0]; //make sure that it is the first draft in your drafts folder -- returns mailMessage object

var body = draft.getPlainBody(); //getting body of email - HTML formatting -- returns string object

var subject = draft.getSubject();

  

for (var i = 1; i < sheet.getMaxRows(); i++) {

var newDraft = body; //copying everything into new draft string object

  

var data = sheet.getSheetValues(i, 1, 1, 4);

  

var name = sheet.getRange(i,1).getValue();

var email = sheet.getRange(i,2).getValue();

var country = sheet.getRange(i,3).getValue();

var university = sheet.getRange(i,4).getValue();

  

newDraft.replace("firstName", name); //replacing everything

newDraft.replace("thisCountry", country);

newDraft.replace("thisSchool", university);

  

var myMessage = newDraft;

  

MailApp.sendEmail(email, subject, myMessage); //sends email (recipient's email, subject, body)

delete newDraft, myMessage;

}

}

it's not replacing it in the message, so I don't understand what is happening or why it is not working

YASMIN aafsh--@mail.com ARGENTINA ARGENTINA UNIVERSITY Roshanne 15--@yahoo.com United Kingdom University of Oxford Sade a--@bu.edu Nigeria Yale University Department of African Studies

Explanation / Answer

Yes, you are saying is correct.

There's only 1 thing that need to be considered...

replace() method you are using right.

As per the documentation of replace() method, replace() method returns a new string with specified replacement(s).

So, as you are writing the method as

newDraft.replace("firstName", name); it replaces as per the specifications, but this returned string is not assigned to any varaible.

So, it is just replaced but new value is not assigned to newDraft.

So, write all the lines as: newDraft = newDraft.replace("firstName", name);

So now you are assigning the string to the variable newDraft and so all the values are updated and the task is done in a perfect way.

So do these replacements and run the script again.

Do comment if there is any query. Thank you. :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote