首次在 GAS 中部署 UI Web 应用程序。 在尝试查看部署的脚本 url 后,获取"无法调用 null 的方法 'getSheets' "类型错误:
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 中,就像它在 sss 中运行时一样。 这难道不是应该发生的吗?
编辑:哎呀,对不起,我没有注意到其他一些错误,请查看有关 webApp 和电子表格 UI 之间差异的文档,然后阅读我的答案。
你为什么评论这句话?
SpreadsheetApp.openById("0AnJTymuE8awLdHZ6QzhNVXpjeFphM3pMV1cxU0daZ2c");
作为 Web 应用程序运行时,您应该始终按 ID 打开电子表格,因为没有活动电子表格(从电子表格查看没有用户打开电子表格,只有脚本有......
另外:"我希望UI出现在脚本网址中"到底是什么意思?
当你转到部署时将创建的 URL 时,你的 Ui 将显示为独立应用。(您还必须保存它的一个版本,但在执行此操作时会很好地解释)