Chart data exported to an Apps Script webapp is null

You probably are sending incompatible datatypes: per the client-server communication documentation, requests with Dates will fail.

My preferred method for google.script.run communication is to 1) send all Dates as milliseconds, via Date().getTime(), as this will also avoid timezone issues and browser-dependent datestring parsing differences. In the client then, you can remap your input data back to a Date object by calling new Date(milliseconds_value), i.e.

function successHandler(rectangularArrayData) {
  // Array indices of column values that need to be converted back to Date objects.
  const dateIndices = [0, 3, 8 /**, etc... */];
  // In general, pass your received data into a transformation function. This particular
  // example assumes you only need to remap milliseconds to Date objects.
  const chartData = rectangularArrayData.map(function (row) {
    return row.map(function (col, index) {
      // If this column index should be a date, make it a Date from the milliseconds input.
      // Otherwise, do nothing to it.
      return (dateIndices.indexOf(index) === -1) ? col : new Date(col);
    });
  });
  ...
}

and 2) serialize it to a JSON string before sending it. Sometimes you’re just sending too complex of an object, and converting it to a string via (JSON.stringify) before return helps ensure it doesn’t get mangled on the wire. In the client, your success handler just needs to reinstantiate it via JSON.parse().

There are a couple other things you do (like ill-advised and non-portable for...in iteration of Arrays, using new Array() instead of [], or performing multiple comparisons to check a specific variable for one of several text values instead of using the Array#indexOf method) that can be improved, but they’re outside the scope of your question and not the source of your issue.

Leave a Comment