【问题标题】:Cannot read property 'getDataRange' of null [duplicate]无法读取 null 的属性“getDataRange”[重复]
【发布时间】:2022-01-26 05:46:16
【问题描述】:

m very new working with google script and APIs and Im 试图运行一段代码,将表格放入 Json,但是我遇到了一个问题,它说:

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

我使用的代码如下:

 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)
    }

我已经尝试更改 getSheetID,但也没有成功

【问题讨论】:

    标签: api google-apps-script google-sheets


    【解决方案1】:

    可能你在执行后得到空值

    const sheet = spreadsheet.getSheetByName("APItest")
    

    这就是您在下一条语句中收到错误的原因。确保工作表不为空。

    【讨论】:

    • 感谢您的回复。问题是工作表不为空。我写了一些假数据,比如姓名和年龄,有 5 行,如果算上姓名和年龄,则为 6
    • 嗯,可能没有像 getDataRange() 这样的工作表函数,或者如果是,它返回 null,请您在控制台屏幕中单独检查这些函数吗?
    • @MarianaRodrigues 这意味着不存在具有给定名称的工作表。检查工作表的名称而不是其内容。
    猜你喜欢
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多