谷歌电子表格API,400错误错误请求:无法解析范围

2022-08-31 16:47:10

我正在尝试使用电子表格示例访问 Google 电子表格。当我运行示例代码时,它工作正常。我只是更改电子表格ID和范围。它开始给我:

Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "message" : "Unable to parse range: Class Data!A2:A4",
    "reason" : "badRequest"
  } ],
  "message" : "Unable to parse range: Class Data!A2:A4",
  "status" : "INVALID_ARGUMENT"
}
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
    at poc.mainPOC.main(mainPOC.java:157)

代码如下:

  String spreadsheetId = "my spread sheet ID";
    String range = "Class Data!A2:A4";
    ValueRange response = service.spreadsheets().values()
        .get(spreadsheetId, range)
        .execute();

答案 1

尝试替换为Class Data!A2:A4A2:A4


答案 2

如果您查看工作表本身,您会注意到工作表的标题为“类数据”。因此,只需将工作表的名称放在“类数据”的位置即可。示例:字符串范围 = “工作表名称!A1:C“;


推荐