4

I am trying to retrieve the data from my Google Spreadsheet, but when I try to add the data object to my htmlTemplate object, I receive the error

'Object does not allow properties to be added or changed'

My code is pretty simple:

function showDialog() {
  var htmlTemplate = HtmlService.createHtmlOutputFromFile('index');

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  var range = sheet.getDataRange();
  var values = range.getValues(); //get the spreadsheet data

  htmlTemplate.data = values; // error here
  ...
}

Could anyone tell me what is wrong with this?

Rubén
  • 34,714
  • 9
  • 70
  • 166
JianNCI
  • 179
  • 1
  • 2
  • 10
  • What are you trying to do? As the error indicates, you are not allowed to add properties to Google's native classes. – tehhowch Apr 09 '18 at 03:33
  • Please review the [HTML Service: Templated HTML](https://developers.google.com/apps-script/guides/html/templates) guide, try to apply that, and then update us & your question if you are still having problems. (We'll need to see your index.html file and maybe a screenshot of your spreadsheet, too.) – Diego Apr 09 '18 at 03:58

2 Answers2

7

Instead of createHtmlOutputFromFile(filename) use createTemplateFromFile(filename)

The above because the first returns a HtmlOutput object which not allow to add properties while the second returns a HtmalTemplate which allows to add properties.

Reference

Rubén
  • 34,714
  • 9
  • 70
  • 166
5

You can't add properties once you have already created the htmlOutput, rather you should populate properties in a template and then evaluate that template so that properties are consumed[if you're consuming] and then final htmlOutput is produced.

In terms of code something like this :

function showDialog() {
  //Create a template
  var htmlTemplate = HtmlService.createTemplateFromFile('index');

  //Fetch the data
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getDataRange().getValues();

  //Plug in those data in template
  htmlTemplate.data = values;

  //Finally evaluate the template, to produce the actually html from the template
  var htmlOutput = htmlTemplate.evaluate();

  //Return [if required]
  return htmlOutput;
}
Umair Mohammad
  • 4,489
  • 2
  • 20
  • 34