2
votes

first time deploying a UI web app in GAS. Getting a "Cannot call method "getSheets" of null" TypeError after trying to view the deployed script url:

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

Here's the spreadsheet:

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

Script code:

// 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);
};

Works perfect when ran from the editor.

Would like to find out what I need to modify in the code for the web app to work. I'm expecting the UI to appear in the script url just like it does when ran inside the ss. Is that not what should happen?

1

1 Answers

3
votes

EDIT : Ooops, sorry, I didn't notice some other errors, please have a look at the documentation about differences between webApps and spreadsheet UIs and read my answer after that .

Why did you commented this line ?

SpreadsheetApp.openById("0AnJTymuE8awLdHZ6QzhNVXpjeFphM3pMV1cxU0daZ2c");

when running as a webapp you should always open spreadsheets by ID since there is no active spreadsheet (view from the spreadsheet no user has open the spreadsheet, only the script has...)

Also : what do you mean exactly by " I'm expecting the UI to appear in the script url " ?

Your Ui will appear as a standalone app when you go to the url that will be created when you deploy it.(you'll have to save a version of it too but it's quite well explained when doing it)