Push Variables Into HTML Using Google Script

2019-07-29 04:09发布

I would like to use Google Script variables into a "mail_template" HTML document. I do not understand why my variables can't be used into this HTML document, while I can use them into the google script itself.

I have 3 documents:

  1. A google script ( .gs ) linked to a Google sheet ( Let's call it "X" )
  2. An html document linked to the above "X" Google sheet
  3. And the "X" *Google sheet itself

The purpose of this work is to send designed email using Google sheet data.

I am beginner, and the documentation seems to be for more advanced users. https://developers.google.com/apps-script/guides/html/templates

Some plugins are doing the same work, but they are not authorized into my workspace. It's very difficult, I am trying to solve that since 3 days, but I still can't.

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

// This constant is written in column O for rows for which an email
// has been sent successfully.
var EMAIL_SENT = "EMAIL_SENT";

function testSchemas() { {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = sheet.getLastRow();  
  var numRows = 1;  
  var dataRange = sheet.getRange(startRow, 1, numRows, 15)
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var name = row[2]; 
    var surname = row[3]; 
    var salesRepEmail = row[4]; 
    var qualityAnalystEmail = "xxx@yahoo.fr"
    var customerEmail = row[5]; 
    var websiteURL = row[6]; 
    var solution1 = row[7];
    function doGet() {
  return HtmlService
      .createTemplateFromFile('mail_template')
      .evaluate();
}

    
    
    var htmlBody = HtmlService.createHtmlOutputFromFile('mail_template').getContent(); 
    var emailSent = row[14];     // Third column
    if (emailSent != "EMAIL_SENT") {  // Prevents sending duplicates
     /*MailApp.sendEmail(customerEmail, subject, message, {
        cc: "",
        bcc: qualityAnalystEmail + ", " + salesRepEmail
      }); */
    MailApp.sendEmail({
    to: customerEmail,
    bcc: qualityAnalystEmail + ", " + salesRepEmail,
    subject: 'Résumé De Notre Consultation Du Site Mobile ' + websiteURL,
    htmlBody: htmlBody,
  }); 
}
      Logger.log(name);
      sheet.getRange(startRow + i, 15).setValue(EMAIL_SENT);
      // Make sure the cell is updated right away in case the script is interrupted
      SpreadsheetApp.flush();
    }
  }
}

1条回答
一夜七次
2楼-- · 2019-07-29 04:37

If you'd like to pass variables from .gs file to your HTML template, try this before calling evaluate() on the template object:

var template = HtmlService.createTemplateFromFile('mail_template');
template.myVar = myVar;
return template.evaluate();

Calling evaluate() executes the JS scriptlets (inline code wrapped in <? ?>). To make the variable accessible to the template, simply attach it as object property. Finally, the correct notation for printing your variable to the page is

<?= myVar ?>
查看更多
登录 后发表回答