Skip to content

Google Apps Script: Scriplets on HTML Template

I’m looking for a way to add a section inside my Html template that will be conditionally shown.

I’ve been looking at scriptlets but all the examples I found were related to “for” functions.

This is my code so far

<tbody>
<? console.log("assigment_letter: " + assigment_letter)?>
<? assigment_letter == "" ? ?>
<tr style="font-size:14px;text-align:center;color:black">
<td style="padding:5px 40px;border: 1px solid #ddd;">Assigment Letter</td>
<td style="padding:5px 40px;border: 1px solid #ddd;">NOT FOUND</td>
</tr>
<? : "" ?>    
</tbody>

In the console log I’m validating that indeed the value from the variable assigment letter is blank and depending on that, I’d like to show a new row inside the table I’m buidling.

However I’m getting the following error.

SyntaxError: Unexpected token ‘;’ at RejectedRequestEmail(Rejected Email:16:49) at onChange(Code:691:11)

It does not tell me exactly in which line of the Html the evaluate function is getting the error

Here is my function that evalutes this html template in case needed

function RejectedRequestEmail(email_obj){
  const htmlTemplateAgent = HtmlService.createTemplateFromFile('Email Reject Notification');
  htmlTemplateAgent.requesting_user_name = email_obj.requesting_user_name;
  htmlTemplateAgent.invoice_id = email_obj.invoice_id;
  htmlTemplateAgent.client_name = email_obj.client_name;
  htmlTemplateAgent.beneficiary_name = email_obj.beneficiary_name;
  htmlTemplateAgent.invoice_amount = email_obj.invoice_amount;
  htmlTemplateAgent.assigment_letter = email_obj.assigment_letter;
  htmlTemplateAgent.invoice_product_match = email_obj.invoice_product_match;
  htmlTemplateAgent.prohibited_items = email_obj.prohibited_items;
  htmlTemplateAgent.client_verified = email_obj.client_verified;
  htmlTemplateAgent.beneficiary_verified = email_obj.beneficiary_verified;
  htmlTemplateAgent.bank_verified = email_obj.bank_verified;
  htmlTemplateAgent.reviewer_name = email_obj.reviewer_name;
  
  const htmlForNotification = htmlTemplateAgent.evaluate().getContent();     
  
}

Thanks

Answer

In your situation, how about the following modification?

Modified script:

When you want to use the ternary operator, how about the following modification? In this modification, the scriptlet of <?!= ... ?> is used.

<tbody>
<? console.log("assigment_letter: " + assigment_letter)?>
<?!= assigment_letter == "" ? `<tr style="font-size:14px;text-align:center;color:black">
<td style="padding:5px 40px;border: 1px solid #ddd;">Assigment Letter</td>
<td style="padding:5px 40px;border: 1px solid #ddd;">NOT FOUND</td>
</tr>` : "" ?>
</tbody>

Or, when you want to use the if statement, how about the following modification? In this modification, the scriptlet of <? ... ?> is used.

<tbody>
<? console.log("assigment_letter: " + assigment_letter)?>
<? if (assigment_letter == "") { ?>
<tr style="font-size:14px;text-align:center;color:black">
<td style="padding:5px 40px;border: 1px solid #ddd;">Assigment Letter</td>
<td style="padding:5px 40px;border: 1px solid #ddd;">NOT FOUND</td>
</tr>
<? } ?>
</tbody>

Reference: