简体   繁体   中英

Hello. I am a begineer to javascript and google app script.I have done my coding to send emails by google sheets but the emails are not sending

Here is my coding. Help me out.

var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").activate();
var lr = ss.getLastRow();
   
   
var templateText = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Template").getRange(1,1).getValue();

//for (var i =2;i<=lr;i++){
var currentEmail = column(i,1);
var currentClass = column(i,3).getValues();
var currentName = ss.getRange(i,2).getValues();

var messageBody = templateText.replace("{name}",currentName).replace("{class}",currentClass);
var subjectLine = "Reminder:" + currentClass + " Upcoming Class";
MailApp.sendEmail(currentEmail,subjectLine,messageBody);

Issue:

There are many issues in your code, including undefined variables (eg column , i ), but I think you want to do the following:

Send an email for each row in Sheet1 , taking into account that:

  • recipient is in column A .
  • subject is based on the value of column C .
  • The message body is built by replacing the keywords {name} and {class} from a string template found in Template!A1 with the values of column B and C respectively.

Code snippet:

If the above is correct, you can do the following:

function yourFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet1 = ss.getSheetByName("Sheet1");
  var firstRow = 2;
  var numRows = sheet1.getLastRow() - firstRow + 1; 
  var templateText = ss.getSheetByName("Template").getRange(1,1).getValue();
  var values = sheet1.getRange(firstRow,1,numRows,3).getValues(); // Retrieve data from all rows
  values.forEach(row => { // Iterate through each row
    var [currentEmail, currentName, currentClass] = row; // Get values from columns A-C
    var messageBody = templateText.replace("{name}",currentName)
                                  .replace("{class}",currentClass);
    var subjectLine = "Reminder:" + currentClass + " Upcoming Class";
    MailApp.sendEmail(currentEmail,subjectLine,messageBody);
  });
}

Reference:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM