Search code examples
javascriptgoogle-apps-scriptgoogle-sheetsweb-applicationstypeerror

Accessing spreadsheet in Google Script


I am writing a google script on top of a spreadsheet. I want to deploy it as a web app. It shows some values. Unfortunately, with my current code, google reminds me:

TypeError: Cannot call method "getSheetByName" of null.

I have no idea where the mistake is.

Here's the code

function doGet(e) {
    var app = UiApp.createApplication().setTitle('Details');
    var ss = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
    var dataFromCell = ss.getRange("B4").getValue();

    var mypanel = app.createVerticalPanel();
    var label = app.createLabel(dataFromCell);

    app.add(mypanel);
    app.add(label);

    return app;
}

Solution

  • In standalone webapps you cannot use getActiveSpreadsheet because no one is using it actively... Use SpreadsheetApp.openById('ID') instead, you can get the ID in the url of your spreadsheet like in this example :

    https://docs.google.com/spreadsheet/ccc?key=0AnqSFd3iikE3d-------nZIV0JQQ0c1a3dWX1dQbGc#gid=0
    

    between key= and #, ie 0AnqSFd3iikE3d-------nZIV0JQQ0c1a3dWX1dQbGc