google apps script - web app doesn't seem to d

2019-09-01 09:16发布

I'm a Java developer but I did a small site for a non-profit group using Google Sites. I have a form I'd like to be somewhat dynamic and Google Apps Script seemed to be a viable option. As frequently happens when one is learning a new technology, I copied and pasted the code below from a tutorial/documentation. I then published it, and inserted the script widget into a page on the site and saved the page. When I reload the page, the "place holder" for the widget is there, but nothing happens - no buttons, no panel, nothing. Same results when I run it from the script editor. I'm sure I'm missing something obvious, but I haven't been able to get the UI to render at all. A little direction would be greatly appreciated. thanks in advance!

function doGet(e) {
  Logger.log("Executing the doGet() method . . .");

  var app = UiApp.createApplication();
  var aPanelRoot = app.createVerticalPanel();

  var button = app.createButton('Click Me');
  aPanelRoot.add(button);

  var label = app.createLabel('The button was clicked.');
  label.setId('statusLabel');
  aPanelRoot.add(label);

  var handler = app.createServerHandler('myClickHandler');
  handler.addCallbackElement(label);
  button.addClickHandler(handler);

  aPanelRoot.setVisible(true);
  return app;
}

function myClickHandler(e) {
  var app = UiApp.getActiveApplication();

  var label = app.getElementById('statusLabel');
  label.setVisible(true);

  //app.close();
  return app;
}

1条回答
小情绪 Triste *
2楼-- · 2019-09-01 09:36

It seems that you simply forgot to add aPanelRoot to the app in the doGet() function

app.add(aPanelRoot)

also : by default all widgets are visibles so you can remove all the setVisible(true) statements as they are only necessary if you set them to false somewhere else...

And if I may add a last comment, it's generally a good idea to choose the parent widget as callbackElement so you don't risk to forget to add elements when you begin to have lots of them (all children are automatically included in the parent) .

查看更多
登录 后发表回答