Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

javascript - sheet email merge with script and html coding multiple people and different links

I have added many (80+) to a google form to prepopulate the form and have them go in and initial in and fix any missing data on the form. I am sending everyone an email reminder of the program and would like to send their link to the form at the same time using script in google sheets. I put this script together after finding something similar online in youtube, but struggling with the html coding in my email to send out to each person.

function sendEmail() {
var email = 0;
var name = 1;
var link = 2;

var emailTemp = HtmlService.createTemplateFromFile("Email")

var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var wsSettings = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Settings");

var name = wsSettings.getRange("B2").getValue();
var subject = wsSettings.getRange("B1").getValue();

var data = ws.getRange("A2:D" + ws.getLastRow()).getValues();

data = data.filter(function(r){ return r[3] == true });


data.forEach(function(row){
  emailTemp.e = row[email];
  emailTemp.n = row[name];
  emailTemp.l = row[link];
  var htmlMessage = emailTemp.evaluate().getContent();
  GmailApp.sendEmail(
    row[email],
     subject, 
     "Your email doesn't support HTML.",
     {name: "name", htmlBody: htmlMessage}
     );

});

}

Here is my html for my email message:
<!DOCTYPE html>
<html>
<head>  
</head>
<body>  
<p><span style="font-family: arial;"><span style="font-family: 'arial black', sans-serif;">Dear <?= n ?>,</span><br /><br />This email will take you to the pre-populated Google Form used to record attendance for <span style="font-family: 'arial black', sans-serif;"><strong>Supporting Children and Families in our Community</strong></span> on January 22, 2021, held from 12:00-3:00 pm.<br /><br />Please visit this <strong><span style="color: #0000ff;">Link</span></strong> to complete any missing data and initial in before clicking on the submit button. <span style="color: #ba372a;">You will only be able to sign-in for the training on January 22, 2021</span>, so do not forget to complete the form if you would like a certificate for attending or SCECHs. </span></p>
<p><span style="font-family: arial;">If you know <strong><span style="color: #843fa1;">someone that is attending that did not receive an email with a form to complete</span></strong>, please have them contact me ASAP so they have a form to complete to sign-in.</span></p>
<p><span style="font-family: arial;">To reach the zoom presentation for <strong>Supporting Children and Families in our Community,</strong> please click here. The link will only work on January 22, 2021, near the start time.</span></p>
<p><span style="font-family: arial;"><br />If you have any questions or concerns, please let me know.</span></p>
<p><span style="font-family: arial;"><br />Best Regards,<br /><span style="font-family: 'arial black';">Erika Rupert</span><br />Bay-Arenac ISD<br /><strong><a href="mailto:[email protected]">[email protected]</a></strong><br /><br />&nbsp;</span></p>
</body>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To include a link in HTML you need an a tag.

You would need to include the below in your html code where the Link is to create a clickable item that brings up the webpage the user needs:

<a href="<?= l ?>">Link</a>

I didn't get on that well with your code for sending emails, one that does work is below:

function sendEmail() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getSheetByName('Sheet1');
  var rng = sh.getDataRange();
  var val = rng.getDisplayValues();
  var lr = rng.getLastRow();
  var name = sh.getRange("B2").getValue();
  var subject = sh.getRange("B1").getValue(); 
  for (var i = 1; i < lr; i++) {
    if (val[i][0] != ""){
      var html = HtmlService.createTemplateFromFile('Email');
      html.e = val[i][0];
      html.n = val[i][1];
      html.l = val[i][2];
      var htmlMessage = html.evaluate().getContent();
      GmailApp.sendEmail(
        val[i][0],
      subject, 
      "Your email doesn't support HTML.",
      {name: name, htmlBody: htmlMessage}
      );
    }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...