第一次在 GAS 中部署 UI Web 应用程序。尝试查看已部署的脚本 url 后获取“无法调用 null 的方法“getSheets””TypeError:
https://script.google.com/macros/s/AKfycbzuQNbkTeBpRPz75Q4dRiVLfEYtuLiuBKnWeA5CbD0/dev
这是电子表格:
https://docs.google.com/spreadsheet/ccc?key=0AnJTymuE8awLdHZ6QzhNVXpjeFphM3pMV1cxU0daZ2c
脚本代码:
// https://developers.google.com/apps-script/class_listbox - How to create listBox
function doGet() {
//var ss = SpreadsheetApp.openById("0AnJTymuE8awLdHZ6QzhNVXpjeFphM3pMV1cxU0daZ2c");
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[1];
var app = UiApp.createApplication().setTitle('App to show how ListBox data can update');
var panel = app.createVerticalPanel();
var lb = app.createListBox(true).setId('myLBid').setName('myLBname');
// how many items to show
lb.setVisibleItemCount(10);
// get sorted names
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
for (var i = 0; i <= numRows - 1; i++) {
var row = values[i];
lb.addItem(row);
}
panel.add(lb);
var button = app.createButton('Submit');
var handler = app.createServerClickHandler('respondToSubmit').addCallbackElement(panel);
button.addClickHandler(handler);
panel.add(button);
app.add(panel);
ss.show(app);
}
// http://youtu.be/5VmEPo6Rkq4 - How to have sumbit write data to ss
function respondToSubmit(e) {
var app = SpreadsheetApp.getActiveSpreadsheet();
//reference widget name to capture what user selected
var listBoxValue = e.parameter.myLBname;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[2];
var lastRow = sheet.getLastRow()+1;
var lastCell = sheet.getRange("A"+lastRow);
lastCell.setValue(listBoxValue);
return app.close();
}
/**
* Adds a custom menu to the active spreadsheet, containing a single menu item
* for invoking the readRows() function specified above.
* The onOpen() function, when defined, is automatically invoked whenever the
* spreadsheet is opened.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Names",
functionName : "doGet"
}];
sheet.addMenu("Script Menu", entries);
};
从编辑器运行时完美运行。
想找出我需要在代码中修改哪些内容才能使 Web 应用程序正常工作。我希望 UI 出现在脚本 url 中,就像在 ss.xml 中运行时一样。这不应该发生吗?