2012-06-29 53 views
2

。获得 “无法调用方法 ”“ 空的” getSheets类型错误试图查看部署脚本URL后:首次在GAS中部署UI Web应用程序时部署UI Web应用程序

https://script.google.com/macros/s/AKfycbzuQNbkTeBpRPz75Q4dRiVLfEYtuLiuBKnWeA5CbD0/dev

这里的电子表格:

https://docs.google.com/spreadsheet/ccc?key=0AnJTymuE8awLdHZ6QzhNVXpjeFphM3pMV1cxU0daZ2c

Script代码:

// 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内运行时一样。这不是应该发生什么?

回答

3

编辑:哎呀,对不起,我没有注意到其他一些错误,请看看关于differences between webApps and spreadsheet UIs的文档,然后阅读我的答案。

你为什么评论这条线?

SpreadsheetApp.openById("0AnJTymuE8awLdHZ6QzhNVXpjeFphM3pMV1cxU0daZ2c");

为web应用程序运行时,应始终打开电子表格的ID,因为没有活动的电子表格(从电子表格中没有用户具有打开电子表格视图,只有脚本有...)

另请参见:您的意思是什么“我期待UI出现在脚本url”

当你去,当你部署将要创建的URL(这样做时,你必须保存一个版本的它太多,但它很好解释)你的用户界面将显示为一个独立的应用程序。

+0

我正在使用openById,不知道为什么我将它留下评论。错过了我需要的“返回应用程序”;在文档中。谢谢,现在好 - http://goo.gl/FTIKB –