Sandro
Sandro

Reputation: 463

Access to spreadsheet in deployed web app

I am trying to access a Google spreadsheet via a Google script, that is published as a web app.

How I created the script:

The script:

function doGet() {
  var ss = SpreadsheetApp.getActive();
  // alternative, doesn't work either
  // var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/abcdef/edit");
  
  var sheets = ss.getSheets();

  var text = ... loop over sheets and do some stuff to get the data ...

  return ContentService.createTextOutput(text);
}

The error message when calling the web app:

TypeError: Cannot call method "getSheets" of null.

The function works (minus the ContentService.createTextOutput of course) when run in the editor.

Upvotes: 3

Views: 1479

Answers (1)

Spencer Easton
Spencer Easton

Reputation: 5782

You can't use the getActive() method unless the script is bound to a spreadsheet (ie script was created within the spreadsheet).

In your example with openByUrl() I would check your URL and the permissions to that sheet. Here is working example of what you were trying to do.

function doGet(){
  var ss= SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1XLOVvEo2nzLARwRhsiWUrFMtF0LnofC1PQoWIeLmwgQ/edit#gid=0');
  return ContentService.createTextOutput(ss.getSheets()[0].getName()).setMimeType(ContentService.MimeType.TEXT); 
}

Upvotes: 3

Related Questions