1

So I understand that to put a variable in my HTML template I need to do

var html = HtmlService.createTemplateFromFile('Index')    
html.data = data
html = html.evaluate();

and then call it in the HTML with

<?= data ?>

But then if I include a script tag snippet with (in the Code.gs file)

function include(filename) {
   return HtmlService.createHtmlOutputFromFile(filename)
   .getContent();
}

and then include my script snippet in the HTML with

<?!= include('DataHandler') ?>

How do I call that data variable in the DataHandler script?

2 Answers 2

3

Change the include function to evaluate data:

function include(filename, ...otherData) {
  var html = HtmlService.createTemplateFromFile('Index')    
  otherData.forEach(obj => html[obj.key] = obj.value)
  return html.evaluate();
}

Then use include like:

<?!= include('DataHandler', {key:data, value: [1,2,3]}) ?>
Sign up to request clarification or add additional context in comments.

1 Comment

After trying your fix, I was wondering why my output was only the dialog saying "Html Output" but just needed to add return html.evaluate().getContent(); and it works. Thanks
0

I don't really use templates much but this worked for me:

HTML:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <div id="lnk"><?= data?></div>
    <script>
      var gdata=<?= data ?>
      window.onload=function(){
        google.script.run.writeData(gdata);
      }
      console.log("My Code");
    </script>
  </body>
</html>

GS:

function launchmydialog() {
  let ui=HtmlService.createTemplateFromFile('ah1');
  ui.data="Hello Work";
  SpreadsheetApp.getUi().showModelessDialog(ui.evaluate(),'Title');
}

function writeData(data) {
  SpreadsheetApp.getUi().alert(data);
}

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.