创建新工作表时“收到无效的JSON有效负载。”

问题描述 投票:0回答:1

我刚刚开始使用Sheets API,我正在按照此设置创建一个新工作表:https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/create

在此代码中,spreadsheetBody对象为空。为此,我使用了此页面上的示例:https://developers.google.com/sheets/api/samples/writing并将此代码添加到spreadsheetBody变量:

{
  "range": "Sheet1!A1:D5",
  "majorDimension": "ROWS",
  "values": [
    ["Item", "Cost", "Stocked", "Ship Date"],
    ["Wheel", "$20.50", "4", "3/1/2016"],
    ["Door", "$15", "2", "3/15/2016"],
    ["Engine", "$100", "1", "30/20/2016"],
    ["Totals", "=SUM(B2:B4)", "=SUM(C2:C4)", "=MAX(D2:D4)"]
  ],
}

但是,当我发布这个时,我收到错误“收到无效的JSON有效负载。未知名称”范围“在'电子表格':找不到字段。”。这可能有什么问题?

google-sheets google-sheets-api
1个回答
1
投票

您使用的请求正文是spreadsheets.values.update

作为示例,创建电子表格的请求主体如下所示。在此示例请求正文中,["Item", "Cost", "Stocked", "Ship Date"], ["Wheel", "$20.50", "4", "3/1/2016"]用于您使用的the document

Sample request body:

{
  "properties": 
  {
    "title": "sampleSpreadsheet"
  },
  "sheets": 
  [
    {
      "data": 
      [
        {
          "startRow": 0,
          "startColumn": 0,
          "rowData": 
          [
            {
              "values": 
              [
                {
                  "userEnteredValue": 
                  {
                    "stringValue": "Item"
                  }
                },
                {
                  "userEnteredValue": 
                  {
                    "stringValue": "Cost"
                  }
                },
                {
                  "userEnteredValue": 
                  {
                    "stringValue": "Stocked"
                  }
                },
                {
                  "userEnteredValue": 
                  {
                    "stringValue": "Ship Date"
                  }
                }
              ]
            },
            {
              "values": 
              [
                {
                  "userEnteredValue": 
                  {
                    "stringValue": "Wheel"
                  }
                },
                {
                  "userEnteredValue": 
                  {
                    "numberValue": 20.5
                  },
                  "userEnteredFormat": 
                  {
                    "numberFormat": 
                    {
                      "type": "NUMBER",
                      "pattern": "$##.00"
                    }
                  }
                },
                {
                  "userEnteredValue": 
                  {
                    "numberValue": 4
                  }
                },
                {
                  "userEnteredValue": 
                  {
                    "numberValue": 42372
                  },
                  "userEnteredFormat": 
                  {
                    "numberFormat": 
                    {
                      "type": "DATE",
                      "pattern": "d/m/yyyy"
                    }
                  }
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

Note:

  • 当此请求正文用于spreadsheets.create时,会创建一个文件名为sampleSpreadsheet的电子表格。该表的值为“A1:D2”的["Item", "Cost", "Stocked", "Ship Date"], ["Wheel", "$20.50", "4", "3/1/2016"]

References:

如果这对你的情况没用,我很抱歉。

© www.soinside.com 2019 - 2024. All rights reserved.