Getting UI form to render inside HTML table in GAS

2019-08-31 00:08发布

问题:

In Google Sites, I am trying to add a short form consisting of a text box and a submit button as a cell in a row inside an html table. One copy of the form for each row in the table. I am trying to collect input data for each table row.

HTML File:

<html>
  ...
  function showForm(){ // https://developers.google.com/apps-script/gui_builder#including
    var app=UiApp.createApplication();
    app.add(app.loadComponent("myGui"));
    return app;
  }
  ...
  <table><tr><td><?=showForm()?></td></tr></table>
  ...
</html>

I then call the .html file from my doGet() function in my .gs file using HtmlService.createTemplateFromFile() method.

The table renders properly, except where I expect the form to appear, I instead get the text/string "UiApplication" instead of the text box + submit button combo.

Am I on the right track? Please help.

回答1:

It's the wrong track.

You can't mix & match components from HtmlService and UiApp. GUI Builder is a packaged UiApp component.

Just stick with a FlexTable and fill the table cells with your builder component. But don't forget to set a prefix:

var flextab = app.createFlexTable();
for (row=0; ...)
  for (col=0; ...)
    flextab.setWidget(row, col, app.loadComponent("myGui", {"prefix": "row"+row+"col"+col});

BTW - you can only have one UiInstance in your web app. Call UiApp.createApplication() only once. If you need the UiInstance later on, you can always find it with UiApp.getActiveApplication().