2014-10-07 90 views
0

我想通過在這個[link] [1]下面的代碼通過UI服務教程:當我運行它時,我得到一個錯誤,指出參數在e.parameter.userName未定義。我將處理程序從app.createServerClickHandler更改爲app.createServerHandler,因爲先前的處理程序顯示已棄用。我如何克服這個問題?教程是否錯誤或誤導了我?我看了看,最終只是複製並粘貼了代碼,但仍然是同樣的錯誤。e.parameter Google Apps腳本教程undefined errer

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 

回答

0

你可能會測試這個腳本錯誤的方式...

您是否使用了.dev URL來測試你的代碼?

實際上有2個網址爲每個Web應用程序: 表示,以應用程序的URL中的一個(與.exec結尾)和測試網址返回最新保存腳本的版本和你有在您編輯/保存時,用它來「按原樣」測試代碼。

.exec版本對應於您在部署窗口中選擇的版本,如下圖所示。 enter image description here

在此代碼的另一個問題是close function,在部署Web應用程序,應用程序將永遠不會關閉,也沒有隱瞞這種方式......它只能嵌入文檔(或電子表格等),在不用時獨立的應用程序。

你最終可以'模擬'一個'關閉'是使網格內容不可見,但這是關於所有...