Formula works 99%, can't get the html reference in the email section to work on google apps script

Viewed 33

so using the resources available here I managed to put together a code to send my colleagues a email when the button is pushed and everything works fine, the only issue I am having is converting a link to something they can just click on to access. Below is the code:

SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Email addresses").activate();
var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2; // First row of data to process
  var numRows = 2; // Number of rows to process
  // Fetch the range of cells A2:B3
  var dataRange = sheet.getRange(startRow, 1, numRows, 2);
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (var i in data) {
    var row = data[i];
    var emailAddress = row[0]; // First column
    var message = "Dear"+" "+row[1]+" "+"here are todays classifications."; // Second column
    var DailyDips = "https://docs.google.com/spreadsheets/d/1ImiuI_osfShHHwA6lXn7npxgwRJ6wdM_l6dPhgcaFt4/edit#gid";
    var message2 = "Click this link to access the sheet"+" "+DailyDips;
    var message3 = <a href="https://docs.google.com/spreadsheets/d/1ImiuI_osfShHHwA6lXn7npxgwRJ6wdM_l6dPhgcaFt4/edit#gid">click here</a>;
   var Sheets = [{fileName:sheetName+".xlsx", content:contents, mimeType:"application//xlsx"},{fileName:sheetName2+".xlsx", content:contents2, mimeType:"application//xlsx"}]
   
MailApp.sendEmail({
    to: emailAddress,
    subject: "Daily Dips",
    htmlBody: message+'<br>'+message2,
    attachments: Sheets
  });
};

the issue being this portion:

   var message3 = <a href="https://docs.google.com/spreadsheets/d/1ImiuI_osfShHHwA6lXn7npxgwRJ6wdM_l6dPhgcaFt4/edit#gid">click here</a>;

the last </a>; is in red and it complains about the token <, having a '" before and after returns the same issue.

Any ideas?

1 Answers

Enclose HTML tags between quotes:

You should enclose the full message with quotes, including the HTML tags like <a>. Otherwise, the script can't recognize this.

Since the href property value should have quotes of its own, though, you should either use single quotes for that (''):

var message3 = "<a href='https://docs.google.com/spreadsheets/d/1ImiuI_osfShHHwA6lXn7npxgwRJ6wdM_l6dPhgcaFt4/edit#gid'>click here</a>";

Or, alternatively, use escape character (\"):

var message3 = "<a href=\"https://docs.google.com/spreadsheets/d/1ImiuI_osfShHHwA6lXn7npxgwRJ6wdM_l6dPhgcaFt4/edit#gid\">click here</a>";
Related