e.parameter Google Apps脚本教程未定义错误

时间:2014-10-07 04:34:02

标签: javascript google-apps-script

我正在尝试使用以下代码浏览UI服务教程[link] [1]:当我运行它时,我收到一条错误,指出e.parameter.userName中的参数未定义。我将处理程序从app.createServerClickHandler更改为app.createServerHandler,因为前一个显示已弃用。我如何通过这个?这个教程是错误的还是误导了我?我查看并查看,最后复制并粘贴代码,但仍然是同样的错误。

unction doGet(e) {
  var doc = SpreadsheetApp.openById(SPREADSHEET_ID_GOES_HERE);
  var app = UiApp.createApplication().setTitle('New app');

  // Create the entry form, a 3 x 2 grid with text boxes for name, age, and city that is then added to a vertical panel
  var grid = app.createGrid(3, 2);
  grid.setWidget(0, 0, app.createLabel('Name:'));
  grid.setWidget(0, 1, app.createTextBox().setName('userName').setId('userName'));
  grid.setWidget(1, 0, app.createLabel('Age:'));
  grid.setWidget(1, 1, app.createTextBox().setName('age').setId('age'));
  grid.setWidget(2, 0, app.createLabel('City'));
  grid.setWidget(2, 1, app.createTextBox().setName('city').setId('city'));

  // Create a vertical panel and add the grid to the panel
  var panel = app.createVerticalPanel();

  panel.add(grid);

  // Here's where this script diverges from the previous script.
  // We create a horizontal panel called buttonPanel to hold two buttons, one for submitting the contents of the form
  // to the Spreadsheet, the other to close the form.

  var buttonPanel = app.createHorizontalPanel();

  // Two buttons get added to buttonPanel: button (for submits) and closeButton (for closing the form)
  // For the submit button we create a server click handler submitHandler and pass submitHandler to the button as a click handler.
  // the function submit gets called when the submit button is clicked.
  var button = app.createButton('submit');
  var submitHandler = app.createServerClickHandler('submit');
  submitHandler.addCallbackElement(grid);
  button.addClickHandler(submitHandler);
  buttonPanel.add(button);

  // For the close button, we create a server click handler closeHandler and pass closeHandler to the close button as a click handler.
  // The function close is called when the close button is clicked.
  var closeButton = app.createButton('close');
  var closeHandler = app.createServerClickHandler('close');
  closeButton.addClickHandler(closeHandler);
  buttonPanel.add(closeButton);


  // Create label called statusLabel and make it invisible; add buttonPanel and statusLabel to the main display panel.
  var statusLabel = app.createLabel().setId('status').setVisible(false);
  panel.add(statusLabel);

  panel.add(buttonPanel);

  app.add(panel);
  return app;
}

// Close everything return when the close button is clicked
function close() {
  var app = UiApp.getActiveApplication();
  app.close();
  // The following line is REQUIRED for the widget to actually close.
  return app;
}

// function called when submit button is clicked
function submit(e) {

  // Write the data in the text boxes back to the Spreadsheet
  var doc = SpreadsheetApp.openById(SPREADSHEET_ID_GOES_HERE);
  var lastRow = doc.getLastRow();
  var cell = doc.getRange('a1').offset(lastRow, 0);
  cell.setValue(e.parameter.userName);
  cell.offset(0, 1).setValue(e.parameter.age);
  cell.offset(0, 2).setValue(e.parameter.city);

  // Clear the values from the text boxes so that new values can be entered
  var app = UiApp.getActiveApplication();
  app.getElementById('userName').setValue('');
  app.getElementById('age').setValue('');
  app.getElementById('city').setValue('');
  // Make the status line visible and tell the user the possible actions
  app.getElementById('status').setVisible(true).setText('User ' + e.parameter.userName + ' entered.' +
  'To add another, type in the information and click submit. To exit, click close.');
  return app;
}​


  [1]: https://developers.google.com/apps-script/guides/ui-service#doGetParams

1 个答案:

答案 0 :(得分:0)

您可能以错误的方式测试此脚本...

您使用.dev网址测试代码吗?

每个webapp实际上有2个网址: 显示为app的网址(以.exec结尾)和测试网址,返回脚本的最新保存版本,并且必须用于测试代码"因为它是"在您编辑/保存它的那一刻。

.exec版本对应于您在部署窗口中选择的版本,如下所示。 enter image description here

此代码中的另一个问题是close function,在部署的Web应用程序中,应用程序永远不会关闭也不会隐藏...这只适用于嵌入文档(或电子表格等)而不是独立的应用

你最终可以模拟' a'关闭'是为了使网格内容不可见,但这是关于所有...