user15509026
user15509026

Reputation: 11

Invalid JSON payload received. Unknown name "requests" at 'requests[0]': Cannot find field

My first post, related to the following post How do I set a basic filter in php using google sheets api?

I have implemented code below but get this message and I'm not sure what's the cause:

Invalid JSON payload received. Unknown name "requests" at 'requests[0]': Cannot find field.

$criteria = new \stdClass();
$criteria->{'2'} = array(
    'condition' => array(
        'type' => 'NUMBER_EQ',
        'values' => array(
            'userEnteredValue' => '5'
        )
    )
);

$requests = [
    new Google_Service_Sheets_Request( array(
        'requests' => array(
            'setBasicFilter' => array(
                'filter' => array(
                    'range' => [
                        'sheetId'           => 0,
                        'startRowIndex'     => 4,
                        'endRowIndex'       => 20,
                        'startColumnIndex'  => 0,
                        'endColumnIndex'    => 11
                    ],
                    'criteria' => $criteria
                ),
            ),
        'includeSpreadsheetInResponse'  => true,
        'responseIncludeGridData'       => true
        )
    )
  )
];
  
$requestBody = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest(array('requests' => $requests));
$response = $sheets->spreadsheets->batchUpdate($spreadsheetId, $requestBody);

Upvotes: 1

Views: 1590

Answers (1)

Tanaike
Tanaike

Reputation: 201428

Modification points:

  • Please include 'userEnteredValue' => '5' to an array.
  • 'requests' => array(,,,) is not required in new Google_Service_Sheets_Request(). Because array('requests' => $requests) is used.
  • Please put 'includeSpreadsheetInResponse' => true, and 'responseIncludeGridData' => true outside of $requests.

When above points are reflected to your script, it becomes as follows.

Modified script:

$criteria = new \stdClass();
$criteria->{'2'} = array(
    'condition' => array(
        'type' => 'NUMBER_EQ',
        'values' => [array('userEnteredValue' => '5')]
    )
);
$requests = [
    new Google_Service_Sheets_Request( array(
            'setBasicFilter' => array(
                'filter' => array(
                    'range' => [
                        'sheetId'           => 0,
                        'startRowIndex'     => 4,
                        'endRowIndex'       => 20,
                        'startColumnIndex'  => 0,
                        'endColumnIndex'    => 11
                    ],
                    'criteria' => $criteria
                )
            )
        )
    )
];
$requestBody = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest(array(
    'requests' => $requests,
    'includeSpreadsheetInResponse' => true,
    'responseIncludeGridData' => true
));
$response = $sheets->spreadsheets->batchUpdate($spreadsheetId, $requestBody);
  • In this case, $criteria->{'2'} can be used, and also, $criteria->{'1'} instead of $criteria->{'2'} can be used.

References:

Upvotes: 1

Related Questions