Home > Back-end >  How can I encode JSON objects to pass from Google Appscript over its HTML interface?
How can I encode JSON objects to pass from Google Appscript over its HTML interface?

Time:08-24

Google Appscript has the ability to pass things from the server side scripting, to client side html.

eg

appscript.gs

var htmlServ = HtmlService.createTemplateFromFile("addDetailsBookingForm");
  let order_id = getSheetOrderID(sheet);   //let order_id = "98";
  htmlServ.order = pullOrderStore(order_id) // let's say this is an array
  const html = htmlServ.evaluate();
  html.setWidth(850).setHeight(450);
  const ui = SpreadsheetApp.getUi();
  ui.showModalDialog(html, "form");

What I'd like to do is pass JSON across using this interface. However, objects are not supported from what I can understand. I can send strings, arrays but not objects.

However, it seems to me that I could turn a JSON object into a string, before sending it, and then parse it client side and turn it back into an object again.

enter image description here

CodePudding user response:

Here's a reference the parameters and return values that can cause problem where passing objects with google.script.run.

The date has got me in the past. If you have a date in a cell and the cell is formatted to display it as a string, the use getDisplayValues() and pass it as a string and then use new Date() constructor to return it back to a date on the server side.

Other than that pretty much any object will work that can be stringified.

  • Related