Do Form POST in Google Apps Script with HtmlServic

2019-02-15 14:43发布

问题:

I'm trying to do a wizard type application with Google Apps Script. Since styling with UiApp is so pain in the rear, I'm trying with the HtmlService route. I can't get the form POST working. Here's my sample code:

// Code.gs
function doGet(e) {
  return HtmlService.createHtmlOutputFromFile('p1');
}

function doPost(e) {
  var params = Utilities.jsonStringify(e);
  var page = HtmlService.createTemplateFromFile('p2');
  page.params = params;
  return page.evaluate().setSandboxMode(HtmlService.SandboxMode.NATIVE);
}

p1.html

<div>
  <form action="#" method="post">
    <input type="text" id="myContents"></input>
    <input type="submit" value="Submit"></input>
  </form>
</div>

p2.html

<div>
  <?=params?>
</div>

The output I've gotten from clicking "Submit" on p1 is

{"queryString":null,"parameter":{},"contextPath":"","parameters":{},"contentLength":0}

I'm expecting the form to return data to me so that I can use the data in the consequent page. I'm wondering if this had anything to do with Caja poisoning. Any ideas?

回答1:

Posting itself doesn't work in HtmlService due to security restrictions imposed by Google Caja.

The correct way to do "form posts" in HtmlService is using google.script.run syntax to call server side functions with client side context. See here for a detailed example - https://developers.google.com/apps-script/guides/html-service-communication#forms