Reputation: 171
I have a Google script that sends out a HTML email from a spreadsheet. One cell in the spreadsheet holds an URL that is changeable as it is composed from values of other cells. In my GS I read this URL into a var. Now I want to use this var in my HTML code that renders the email so that the email receiver can click on a link that opens this custom URL.
I can not find a solution to replace the fixed URL with a variable that holds my custom URL Please check the HTML code where it says " " that's where I'm placing a var, but doing it obviously wrong.
**** Google script*****
var CO_PP = COSheet.getRange(2,11).getValue() ;
var CO_PP_href = ("<a href=\"" + CO_PP + "\">") ; // use a backslash \ to escape the quotation marks.
var EmailBody = HtmlService.createTemplateFromFile(template);
EmailBody.EmailVar1 = CO_Name;
EmailBody.EmailVar2 = CO_Email;
EmailBody.EmailVar13 = CO_PP_href;
var MyHtmlBody = EmailBody.evaluate().getContent()
var EmailSubject = "Credits Order SUCCES .:: CADsherpa Weblink ::."
MailApp.sendEmail(CO_Email, EmailSubject, "Your emailreader does not support HTML." +
" Try opening this message with a different email reader or take contact with [email protected]", {htmlBody: MyHtmlBody, attachments: PDF_ToSend });
*******HTML*******
<!-- button start -->
<div>
<p> </p>
<table align="left" border="0" cellpadding="1" cellspacing="1" style="height:10px;width:185px;">
<tbody>
<tr>
<td style="white-space: nowrap; text-align: center; vertical-align: middle; background-color: rgb(51, 102, 255);">
<h3> +<?= EmailVar13 ?>+ <span style="font-size:18px;"> <!-- PROBLEM ON THIS LINE -->
<strong><em><span style="font-family:arial,helvetica,sans-serif;">
<span style="color:#FFFFFF;">Pay with PayPal </span></span></em></strong></span></a></h3>
</td>
</tr>
</tbody>
</table>
<p> </p>
</div>
<!-- button end -->
Upvotes: 1
Views: 1147
Reputation: 4430
When using HTML templating in GAS, the variables assigned to the template will be escaped. That means that in your case, if CO_PP_href
was set to the expression "<a href=\"" + CO_PP + "\">"
the actual tags will not be placed into the HTML, but rather the escaped version (<a href="...">
) so that it can be treated as printable "text" within your page.
For your needs, I propose a different solution. You can firstly create the <a>
tag inside the html template, and only the href
attribute thereof will be set when evaluating the template. The solution would look as follows:
HTML
<!-- button start -->
<div>
<p> </p>
<table align="left" border="0" cellpadding="1" cellspacing="1" style="height:10px;width:185px;">
<tbody>
<tr>
<td style="white-space: nowrap; text-align: center; vertical-align: middle; background-color: rgb(51, 102, 255);">
<h3> <a href="<?= EmailVar13 ?>"> <span style="font-size:18px;">
<strong><em><span style="font-family:arial,helvetica,sans-serif;">
<span style="color:#FFFFFF;">Pay with PayPal </span></span></em></strong></span></a></h3>
</td>
</tr>
</tbody>
</table>
<p> </p>
</div>
<!-- button end -->
GAS
var CO_PP = COSheet.getRange(2,11).getValue() ;
var EmailBody = HtmlService.createTemplateFromFile(template);
EmailBody.EmailVar1 = CO_Name;
EmailBody.EmailVar2 = CO_Email;
EmailBody.EmailVar13 = CO_PP;
var MyHtmlBody = EmailBody.evaluate().getContent()
var EmailSubject = "Credits Order SUCCES .:: CADsherpa Weblink ::."
MailApp.sendEmail(CO_Email, EmailSubject, "Your emailreader does not support HTML." +
" Try opening this message with a different email reader or take contact with [email protected]", {htmlBody: MyHtmlBody, attachments: PDF_ToSend });
Upvotes: 1