How to fix 'TypeError: Cannot call method “getRange” of null' in Google Apps Script

后端 未结 1 985
囚心锁ツ
囚心锁ツ 2021-01-27 01:51

I\'m setting up a script to auto-populate a template document in Google Docs using data from Google Sheets. I keep encountering the \'TypeError: Cannot call method \"getRange\"

相关标签:
1条回答
  • 2021-01-27 02:14

    Issue:

    sheet.getRange("A2:A16")
    

    TypeError: Cannot call method "getRange" of null. (line 14, file "Code")

    The error means sheet is null and null doesn't have a getRange method. Only a real Sheet class does.

    As written in the documentation, There is only one reason, where the sheet returned is null.

    Returns null if there is no sheet with the given name.

    There is only one answer. It's when that sheet name doesn't exist.

    Possible solutions:

    sheet name refers to the tab name in a spreadsheet/workbook. It does not refer to the filename or document name. By default, the first sheet/tab is named "Sheet1". Try

    ss.getSheetByName("Sheet1");
    

    If you still have trouble getting the sheet, check for spaces, non printable characters in the sheet name. Alternatively, Rename the sheet to something simple like S1.

    0 讨论(0)
提交回复
热议问题