Google script - using template to build table

909 views Asked by At

Trying to build a report to be sent via email. My idea was to build the email using an HTML template, but I seem to be missing out on something... The email should contain a report which will include all entries with a specific month in ColB.

Here's a quick demo sheet

Here's my code

function MonthlyPulseCheck() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
  var pulsedate = new Date("02/02/2022"); 
  var pulsemonth = pulsedate.getMonth();
  var Pulsetable = buildmonthlypulse(pulsemonth);
  
  var emailtemplate  = HtmlService.createTemplateFromFile('Monthly pulse check');
    emailtemplate.OOOPULSE = Pulsetable;
  
  var emailbody = emailtemplate.evaluate();
  
  GmailApp.sendEmail('[email protected]',( 'monthly pulse check for month ' + pulsemonth), '',
                     {htmlBody: emailbody.getContent(),
      name: 'test system', 
      from: '[email protected]', 
      replyTo: '[email protected]',
})
}

function buildmonthlypulse(pulsemonth) {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var Pulsearray = spreadsheet.getRange('A2:C').getValues();
var i = 0;
var counter = 1;
var imax = 15;//Pulsearray.length;
for (var i = 0; i < imax; i++) {
var arraydate = new Date(Pulsearray[i][1]);
var arraymonth = arraydate.getMonth();
//var pulseHTML = 0;
//var checkdate = new Date(arraydate);
if (arraymonth === pulsemonth) {
  if (counter === 1) {
  var pulseHTML = '<table style="float: right;"> <body> <tr> <td>&nbsp;TEXT2</td> <td>&nbsp;TEXT</td> <td><a href="ynet.co.il">&nbsp;NAME</a></td> </tr>'
  }
  var name =  spreadsheet.getRange(i+2, 1).getValue();
  var namelink = 'google.com';
  
  var pulseHTML = pulseHTML + '  <tr> <td>&nbsp;1</td> <td>&nbsp;2</td> <td><a href="youtube.com">&nbsp;3</a></td> </tr>';
  var counter = counter + 1;
  }
  
  }
  if (counter != 1) {
  var pulseHTML = pulseHTML + '</tbody> </table>';
  }
  return pulseHTML;
  }

Here's the HTML (used as a template)

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <?=OOOPULSE?>
  </body>
</html>

email is sent with just the digit 0 in the body. Can anyone make sense of this?

1

There are 1 answers

3
Nikko J. On BEST ANSWER

I tried to replicate your code and found some issues:

  1. In your function buildmonthlypulse(), you declared var pulseHTML multiple times inside the for loop which is not accessible to the return statement.
  2. In your HTML file, you are using Printing scriptlets <?= ... ?> which is use for printing values with contextual escaping. The html elements in your var pulseHTML are recognized as string.

Solution:

  1. Declare var pulseHTML = '' above the for loop statement (just like the commented one) and inside the for loop, remove var and retain pulseHTML. Example: pulseHTML = pulseHTML + '</tbody> </table>';
  2. Instead of using Printing scriptlets "<?= ... ?>" use Force-printing scriptlets <?!= ... ?>. It is the same with Printing scriptlets but avoid contextual escaping.

Example Output:

Using Printing scriptlets

enter image description here

Using Force-printing scriptlets

enter image description here

Nitpick:

  • Replace <body> with <tbody> in the pulseHTML = '<table style="float: right;"> <body> <tr>.... of your code.
  • Proper indentation.

References: