0

I have looked at StackOverflow code on passing variables but still get this error.

function doGet() {

var ss = SpreadsheetApp.getActiveSpreadsheet()
var example = ss.getRange("T1").getDisplayValue();
var exampleText = "Some Text " + example; 

html.exampleVar = exampleText;
html.setTitle("Dynamic Webpage");

var html = HtmlService.createTemplateFromFile("Index").evaluate();

return html; 

}

The HTML file in Body tags is as follows

<?= exampleVar ?>

TypeError: Cannot set property "exampleVar" of undefined to "Example Text". (line 8, file "Example", project "Example")

I realise I have not declared exampleVar as a variable. How can I achieve this?

TheMaster
  • 45,448
  • 6
  • 62
  • 85
Jonathan Harker
  • 78
  • 1
  • 11
  • `html` isn't defined until after you've executed that line. Look at the order of your script lines. – tehhowch Jan 07 '19 at 14:24
  • I tried that as well @tehhowch - it then came up with an error that it could not connect to the server. Calling the function has worked well. – Jonathan Harker Jan 07 '19 at 15:26
  • It was likely malformed HTML that was being evaluated. Note that "`'Server Error'` while running this line" is a completely different error than `TypeError`. Your code here shows normal printing scriptlets `=` content. You may have wanted to use [force-printing scriptlets](https://developers.google.com/apps-script/guides/html/templates#force-printing_scriptlets). In any case, your issue is more than likely solved by reviewing the official documentation and the guide on variables with templates: https://developers.google.com/apps-script/guides/html/templates#pushing_variables_to_templates – tehhowch Jan 07 '19 at 15:31
  • Thanks @tehowch I wan't familiar with force-printing scriptlets – Jonathan Harker Jan 08 '19 at 09:56

2 Answers2

0

Made a separate function to call the variables. Then called that function from the HTML file.

How to pass an html variable to htmlservice

Jonathan Harker
  • 78
  • 1
  • 11
0

It's not clear to me what you trying to accomplish but I think this is how your example will work.

function doGet() {
  var ss=SpreadsheetApp.getActive();  
  var sh=ss.getSheetByName('Some Specific Sheet Name');
  var html=HtmlService.createTemplateFromFile("Index").evaluate().setTitle("Dynamic Webpage");
  html.append(Utilities.formatString('<div id="exampletxt"><p>Some Text %s</p></div>', sh.getRange('T1')));
  return html; 
}
Cooper
  • 59,616
  • 6
  • 23
  • 54