1

I am trying to combine the principles on the following two tutorials: https://developers.google.com/apps-script/articles/mail_merge https://developers.google.com/apps-script/articles/sending_emails

As I want the email to only be sent to each recipient once, and the form will be receiving ongoing responses. Before attempting to add in the "If" function I confirmed that the merge tags were working, but appear to have completed broken the code in my attempts to tweak it!

Heres what I currently have:

    function sendEmails() {var ss = SpreadsheetApp.getActiveSpreadsheet();  var dataSheet = ss.getSheets()[1];  var dataRange = dataSheet.getRange(2, 1, dataSheet.getMaxRows() - 1, 12);  var emailSent = getrange(12)  var templateSheet = ss.getSheets()[3];  var emailTemplate = templateSheet.getRange("A1").getValue();  objects = getRowsData(dataSheet, dataRange);  for (var i = 0; i < objects,length; ++i;   var rowData = objects[objects.length-1];;

   
    if (emailSent !== EMAIL_SENT){
    var emailText = fillInTemplateFromObject(emailTemplate, rowData);
    var emailSubject = "U+ Content Request Confirmation";
    MailApp.sendEmail(rowData.emailAddress, emailSubject, emailText);  sheet.getRange(startRow + i, 12).setValue(EMAIL_SENT);  SpreadsheetApp.flush();} 
    function fillInTemplateFromObject(template, data) {

  var email = template;
  var templateVars = template.match(/\$\{\"[^\"]+\"\}/g);

  for (var i = 0; i < templateVars.length; ++i) {
  
    var variableData = data[normalizeHeader(templateVars[i])];
    email = email.replace(templateVars[i], variableData || "");
  }

  return email;
}
function getRowsData(sheet, range, columnHeadersRowIndex) {
  columnHeadersRowIndex = columnHeadersRowIndex || range.getRowIndex() - 1;
  var numColumns = range.getEndColumn() - range.getColumn() + 1;
  var headersRange = sheet.getRange(columnHeadersRowIndex, range.getColumn(), 1, numColumns);
  var headers = headersRange.getValues()[0];
  return getObjects(range.getValues(), normalizeHeaders(headers));
}


function getObjects(data, keys) {
  var objects = [];
  for (var i = 0; i < data.length; ++i) {
    var object = {};
    var hasData = false;
    for (var j = 0; j < data[i].length; ++j) {
      var cellData = data[i][j];
      if (isCellEmpty(cellData)) {
        continue;
      }
      object[keys[j]] = cellData;
      hasData = true;
    }
    if (hasData) {
      objects.push(object);
    }
  }
  return objects;
}
function normalizeHeaders(headers) {
  var keys = [];
  for (var i = 0; i < headers.length; ++i) {
    var key = normalizeHeader(headers[i]);
    if (key.length > 0) {
      keys.push(key);
    }
  }
  return keys;
}


function normalizeHeader(header) {
  var key = "";
  var upperCase = false;
  for (var i = 0; i < header.length; ++i) {
    var letter = header[i];
    if (letter == " " && key.length > 0) {
      upperCase = true;
      continue;
    }
    if (!isAlnum(letter)) {
      continue;
    }
    if (key.length == 0 && isDigit(letter)) {
      continue; // first character must be a letter
    }
    if (upperCase) {
      upperCase = false;
      key += letter.toUpperCase();
    } else {
      key += letter.toLowerCase();
    }
  }
  return key;
}


function isCellEmpty(cellData) {
  return typeof(cellData) == "string" && cellData == "";
}


function isAlnum(char) {
  return char >= 'A' && char <= 'Z' ||
    char >= 'a' && char <= 'z' ||
    isDigit(char);
}


function isDigit(char) {
  return char >= '0' && char <= '9';
}} )
  • I've taken a look to your code and you have only "broken" three functions: `sendEmails`, `fillInTemplateFromObject` and `isDigit`. The last one is easy to fix, just delete the last two characters. The other two, I recommend you to rewrite them. I think that the important two lines that you have to add to the code of the tutorial (`Tutorial: Sending emails from a Spreadsheet`) are the ones that read (`if (emailSent !== EMAIL_SENT)`) and write (`sheet`: `sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);`) in the sheet. Try implementing it again and if you have any problem post your update. – fullfine Apr 21 '21 at 15:51
  • @fullfine thank you for responding! I've since ended up going down a different route of using what I know to create the emails - a wonderfully messy function with lots of ="Text "&A1&" more text "&B1", but I've flagged this to try in my own time and will let you know how I get on! – Trudi Davies Apr 22 '21 at 10:57

0 Answers0