'Cannot read property 'getDataRange' of null [duplicate]

Im very new working with google script and APIs and Im trying to run a piece of code that makes the sheet in to a Json however I`m facing an issue where it says:

    TypeError: Cannot read property 'getDataRange' of null
json    @ Code.gs:4

The code I`m using is the following:

 function json() {
      const spreadsheet = SpreadsheetApp.getActiveSpreadsheet()
      const sheet = spreadsheet.getSheetByName("APItest")
      const data = sheet.getDataRange().getValues()
      const jsonData = convertToJson(data)
      return ContentService
            .createTextOutput(JSON.stringify(jsonData))
            .setMimeType(ContentService.MimeType.JSON)
    }

I already tried to change the getSheetID but also didn`t work



Solution 1:[1]

Probably you're getting null value after the execution of

const sheet = spreadsheet.getSheetByName("APItest")

And That's why you get the error in the next statement. make sure that the sheet is not null.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Osman