1
votes

I'm just starting out with the Sheets API, and I'm following this setup to create a new sheet: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/create

In this code, the spreadsheetBody object is empty. For this I've used the example on this page: https://developers.google.com/sheets/api/samples/writing and added this code to the spreadsheetBody variable:

{
  "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)"]
  ],
}

However, when I post this I get the error "Invalid JSON payload received. Unknown name "range" at 'spreadsheet': Cannot find field.". What might be wrong here?

1
Post your full codeTheMaster
Note that the Google APIs Explorer can let you interactively build and test the associated resource parameters, and examine the responses.tehhowch

1 Answers

2
votes

The request body you are using is for spreadsheets.values.update.

As a sample, the request body for creating Spreadsheet is as follows. In this sample request body, ["Item", "Cost", "Stocked", "Ship Date"], ["Wheel", "$20.50", "4", "3/1/2016"] was used from the document you use.

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:

  • When this request body is used for spreadsheets.create, a Spreadsheet with the filename of sampleSpreadsheet is created. The sheet has the values of ["Item", "Cost", "Stocked", "Ship Date"], ["Wheel", "$20.50", "4", "3/1/2016"] at "A1:D2".

References: