Google script - using template to build table

一笑奈何 提交于 2021-02-15 07:44:00

问题


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('someone@gmail.com',( 'monthly pulse check for month ' + pulsemonth), '',
                     {htmlBody: emailbody.getContent(),
      name: 'test system', 
      from: 'alias@gmail.com', 
      replyTo: 'alias2@gmail.com',
})
}

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:


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

Using Force-printing scriptlets

Nitpick:

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

References:

  • Contextual escaping
  • Printing scriptlets and Force-printing scriptlets
  • Variable scope


来源:https://stackoverflow.com/questions/65294487/google-script-using-template-to-build-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!