UI服务错误:请先选择活动表

时间:2014-05-09 20:19:20

标签: user-interface google-apps-script google-sheets

我正在尝试从this page复制代码。当我部署为Web应用程序时,它会打开带有输入框和提交按钮的用户界面。但是,当我单击提交时,它会显示以下错误消息:“请先选择一个活动工作表”。当我在电子表格中调出UI时,我得到了同样的错误。我没有使用openById,而是将其更改为getActive.getSheetByName,并且它从电子表格中运行。但是,回到Web应用程序,新的错误消息现在是“无法调用getSheetByName为null。”

任何人都可以提出为什么我会收到“请先选择活动表格”错误以及我需要采取哪些不同的做法?

这是我复制的代码,我做的唯一改变就是将我的SS密钥放在两个适当的位置。

function doGet(e) {
  var doc = SpreadsheetApp.openById("yyyy"); //I've got my SS key here
  var app = UiApp.createApplication().setTitle('New app');
  // Create a grid with 3 text boxes and corresponding labels
  var grid = app.createGrid(3, 2);
  grid.setWidget(0, 0, app.createLabel('Name:'));

  // Text entered in the text box is passed in to userName
  // The setName method will make those widgets available by
  // the given name to the server handlers later
  grid.setWidget(0, 1, app.createTextBox().setName('userName'));
  grid.setWidget(1, 0, app.createLabel('Age:'));
  grid.setWidget(1, 1, app.createTextBox().setName('age'));

  // Text entered in the text box is passed in to age    
  grid.setWidget(2, 0, app.createLabel('City'));
  grid.setWidget(2, 1, app.createTextBox().setName('city'));

  // Text entered in the text box is passed in to city.

  // Create a vertical panel..
  var panel = app.createVerticalPanel();

  // ...and add the grid to the panel
  panel.add(grid);

  // Create a button and click handler; pass in the grid object as a callback element     and the handler as a click handler
  // Identify the function b as the server click handler

  var button = app.createButton('submit');
  var handler = app.createServerHandler('b');
  handler.addCallbackElement(grid);
  button.addClickHandler(handler);

  // Add the button to the panel and the panel to the application, then display the     application app
  panel.add(button);
  app.add(panel);
  return app;


}

// Function that records the values in a Spreadsheet
function b(e) {
  var doc = SpreadsheetApp.openById("yyyy"); //I've got my SS key here
  var lastRow = doc.getLastRow(); // Determine the last row in the Spreadsheet that     contains any values
  var cell = doc.getRange('a1').offset(lastRow, 0); // determine the next free cell in     column A
  // You can access e.parameter.userName because you used setName('userName') above and
  // also added the grid containing those widgets as a callback element to the server
  // handler.
  cell.setValue(e.parameter.userName); // Set the value of the cell to userName
  cell.offset(0, 1).setValue(e.parameter.age); // Set the value of the adjacent cell to     age
  cell.offset(0, 2).setValue(e.parameter.city); // set the value of the next cell to     city

  // Clean up - get the UiInstance object, close it, and return
  var app = UiApp.getActiveApplication();
  app.close();
  // The following line is REQUIRED for the widget to actually close.
  return app;
}

谢谢!

1 个答案:

答案 0 :(得分:1)

您正在打开电子表格,应该在电子表格中打开工作表

sheet = doc.getSheetByName(“工作表名称”)

documentation