0

So I have a listing of plants in a Google spreadsheet that I want to render in HTML. I have a script that will gather the information in either an object or an array with each element of the array being an object. Each plant have a 3 digit id that is used as a key in both the object and the array as follows: object[143].create_date = 12/15/2012 array[143].create_date = 12/15/2012

Both the object and array are being created properly.

PROBLEM:

var pRet = HtmlService.createTemplateFromFile('Plant Listing');

I am having trouble getting the data into html using HtmlService and a html template. When using the object in the form of

pRet.data = myObject;

Within the html template code such as:

<? for(var x in data) { ?>
<? data[x].create_date ?>

renders as TypeError: Cannot read property "create_date" from null. But if I pass in the data to the template like this:

pRet.Data = myObject[143];

then the rendered html loops through and displays the data elements of the plant with id of 143. If I use the function that returns the plants as an array index by the plant id then I get an array with elements 0 to 99 being undefined with the first element having any data being element 100 corresponding to the plant with id of 100.

My code to render the data in the apreadsheet as a object returns an object with a listing of elements as follows:

{143={stage_7=-1.0, index=20.0, stage_5=-1.0, stage_6=-1.0, stage_3=1.0, current_stage=1.0, strain=1.0, stage_4=0.0, location=1.0, stage_1=51.0, stage_2=61.0, id=143.0, create_date=Sat Dec 08 2012 00:00:00 GMT-0700 (MST), group=null}

And the code to return the data set as an array indexed on plant id returns this with the first 100 elements being null due to the first plant index being 100:

{stage_7=-1.0, index=1.0, stage_5=-1.0, stage_6=-1.0, stage_3=5.0, current_stage=2.0, strain=1.0, stage_4=-1.0, location=2.0, stage_1=Mon Jul 09 23:00:00 PDT 2012, stage_2=56.0, id=100.0, create_date=Sat Dec 08 2012 00:00:00 GMT-0700 (MST), group=null}

QUESTION: What are other methods that I can try to get the data from the spreadsheet to the rendered html? My initial thoughts are to drop the idea of having the elements in either the array or object index on plant id. Another option would be to use the array and check for a null entry in the template file.

1 Answers1

1

There is a known bug in HtmlService that you can't assign subproperties to an object on the template. So this fails:

var = HtmlService.createTemplate();
t.x = {};
t.x.y = 1;  // This throws no error, but t.x.y will still be undefined!

This however works:

var = HtmlService.createTemplate();
var x = {};
x.y = 1;
t.x = x;    // t.x.y will now correctly be 1

We know what the problem is and plan to fix it, but it's nontrivial and will take some time to roll out.

Corey G
  • 7,754
  • 1
  • 28
  • 28