Evan
Evan

Reputation: 166

Adding a variable to an anchor tag on google app scripts

I have script that sends emails from google sheets as below:

var Prefiledurl = row[8]; var names = row[2];

The variable "Prefiledurl" is storing urls from a column. How do I send it as a link?

When I try this:

"<a href="+Prefiledurl+"> Click Here </a>"

it sends out a long link instead of a hyperlink`

Upvotes: 1

Views: 1402

Answers (3)

Ernesto Flores
Ernesto Flores

Reputation: 21

The best way to do it is calling the html from .gs file with the following code:

.gs file:

var es = SpreadsheetApp.openById('SPREADSHEET_NAME');
var sheet = es.getSheetByName('SHEET_NAME');
var url = es.getRange("A1").getValue();
const htmlTemplate = HtmlService.createTemplateFromFile('HTML_NAME_FILE');
htmlTemplate.url = url;
const htmlForMail = htmlTemplate.evaluate().getContent();

You might add in your HTML file the following code line:

<html>
<head>
    <base target="_top">
</head>
<body>
<a href= <?= url ?> > link </a>
</body>
</html>

You will will see the blue tipic text underlined with a hyperlink inside, like this:

Link

With the diference that this can change depending the value of the cell that you referred before.

Upvotes: 0

Suyash Gandhi
Suyash Gandhi

Reputation: 936

Try using this :

var mailBody = "<HTML><BODY>"+ "<a href="+Prefiledurl+"> Click Here </a>"+"</HTML></BODY>";

Upvotes: 3

Cameron Roberts
Cameron Roberts

Reputation: 7367

You need to send an HTML body for the email using the htmlBody option, you are currently only sending a plain text body, which is why your entire tag is displayed as plain text.

The best approach is to send a plain text body which doesn't include the HTML tag, but instead includes the full link, and also send an HTML body which includes the tag and any other markup. This guarantees full accessibility regardless of whether the recipient can view html or not.

See the documentation here, if you scroll down to the Options list you'll see the htmlBody option mentioned.

https://developers.google.com/apps-script/reference/mail/mail-app#sendEmail(String,String,String,Object)

Upvotes: 1

Related Questions