2

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 = "[email protected]"
    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();
    }
  }
}

2 Answers 2

3

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 ?>
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Anton, thanks for your help. it's still not working, that's may be something stupid I did. I try this code into the "doGet function, to replace the existant one.
Sorry, Marvin, maybe it's just the formatting, but looking at your code, are you actually trying to call the doGet() function inside another function? If yes, it will not work this way. Your script needs to be deployed as a web app first. When you access the published web app by opening it's URL, the app receives the incoming GET request and executed the doGet() function
1

I had a similar question. Anton's solution got me most of the way, but I had to add .getContent() after calling evaluate() and assign the evaluated template to a variable.

var template = HtmlService.createTemplateFromFile('mail_template');
template.myVar = myVar;
var emailToSend = officeTempl.evaluate().getContent();
htmlBody: emailToSend

https://developers.google.com/apps-script/reference/html/html-template#evaluate

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.