Search code examples
javascriptgoogle-apps-scriptembedgoogle-sites

Google Apps Script Doesn't Work When Embedded. TypeError: Cannot call method "getActiveSheet" of null


The Google Apps Script runs perfectly from the Web App URL but when I embed it into a Google Site via Insert > Apps Script then I get the following error:

TypeError: Cannot call method "getActiveSheet" of null.

This is the code:

var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

I tried enabling the Advanced Google Services Google Sheets API and Google Drive API. It still won't work. Somehow it loses connection to the sheet once embedded. Does anyone have any ideas?

Thanks, Chris


Solution

  • You can't use .getActiveSpreadsheet().getActiveSheet() because you're not running the script from a spreadsheet, you're running it from a site so you don't actually have any spreadsheet/sheet 'active' as such.

    Use SpreadsheetApp.openByUrl("").getSheetByName("") instead.