Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

html - Mail merge question: pass a hyperlink into gmail from a google sheet

I am in the middle of a mail merge project, and have created a google sheet that includes all of my standard fields. One field includes a hyperlink. In my current code I am only able to pass the text for the hyperlink into gmail. Below are the HTML code and google app script that I am working with. I suspect that I need to modify my HTML code but I am uncertain how to proceed. The HTML codes in question is <?= fs ?>. Any support would be greatly appreciated.

HTML Code:

<body>
  <p>Please review and update your project management spreadsheet by [insert date] using the link: <?= fs ?></p>
</body>

Google Apps Script

function myFunction() {      
  var school = 0;
  var contact = 2;
  var email = 5;
  var followUpSheet = 6;
  
  var emailTemp = HtmlService.createTemplateFromFile("File_name");
 
  var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet_Name");
  
  var data = ws.getRange("A2:P" + ws.getLastRow()).getValues();
  
  data.forEach(function(row){
  
    emailTemp.contact = row[contact];
    emailTemp.fs = row[followUpSheet];
    
    var htmlMessage = emailTemp.evaluate().getContent();
    
    GmailApp.sendEmail(
      row[email], 
      "Name of eMail", 
      "Your email doesn't support HTML.",
      {name: "name of sender", htmlBody: htmlMessage});
  });
}

The output for this code is:

Please review and update your project management spreadsheet by [insert date] using the link: hyperlink text name

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

I decided to insert the url into the email. This resolved my challenge. The HTML and GAS code are presented below.

//HTML Code

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    
    <p>Dear <?= contact ?>,</p>
    <p>Insert paragraph 1</p>
    <p>Please review and update your project management spreadsheet by [insert date].  Select the link (or attachment) below to open your spreadsheet:</p>
    <ul style="list-style-type:disc;">
    <li><?= fs ?></li></ul>
    <p>If you have any questions please contact [team member name] (email: xxxx, phone: xxxx)</p>
    <p>Have a great day.</p>
    <p>Signature</p>
    
  </body>
</html>
//Google Apps Script

function myFunction() {
  
  var school = 0;
  var contact = 2;
  var email = 5;
  var URL = 7;
  
  var emailTemp = HtmlService.createTemplateFromFile("SpreadsheetName");
 
  var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  
  var data = ws.getRange("A2:P" + ws.getLastRow()).getValues();
  
  data.forEach(function(row){
  
    emailTemp.contact = row[contact];
    emailTemp.fs = row[URL];
    
    var htmlMessage = emailTemp.evaluate().getContent();
    
    GmailApp.sendEmail(
      row[email], 
      "Follow Up Email", 
      "Your email doesn't support HTML.",
      {name: "Representative", htmlBody: htmlMessage})
  
  });
  
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...