I'm trying to apply a basic filter to a Google Sheet from my website using the Google-API-PHP-Client. I only want to display rows where the value in column A equals "global_city_actors".
Unfortunately, I cannot seem to construct the request correctly. The source of the error appears to be in the "criteria" specification, particularly the "0" key for the column index (doesn't work whether using "" or not). I get the following error message but cannot make sense of it:
Invalid value at 'requests[0].set_basic_filter.filter' (Map), Cannot bind a list to map for field 'criteria'.", "errors": [ { "message": "Invalid value at 'requests[0].set_basic_filter.filter' (Map), Cannot bind a list to map for field 'criteria'.", "reason": "invalid" } ], "status": "INVALID_ARGUMENT" } }
I would be grateful for any leads. Here is my code:
// The first bit is inspired by: https://www.fillup.io/post/read-and-write-google-sheets-from-php/
require_once(APPPATH.'third_party/google-api-php-client-2.2.3/vendor/autoload.php');
$this->client = new Google_Client();
$this->client->setApplicationName('My Test API');
$this->client->setScopes([Google_Service_Sheets::SPREADSHEETS]);
$this->client->setAccessType('offline');
$this->client->setAuthConfig(APPPATH.'third_party/google-api-php-client-2.2.3/credentials/.....json');
$this->service = new Google_Service_Sheets($this->client);
$this->spreadsheetId = '1yRg6Kai6MwKcE8ZKdCggfWwPZVIOu-MfByvgWLSEiUI';
// This is the faulty request:
$requests = [
new Google_Service_Sheets_Request([
'setBasicFilter' => [
'filter' => [
'range' => [
'sheetId' => 0,
'startColumnIndex' => 0,
'endColumnIndex' => 0
],
'criteria' => [
"0" => [
'condition' => [
'type' => 'TEXT_EQ',
'values' => [
'userEnteredValue' => 'global_city_actors'
]
]
]
]
]
]
])
];
$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
'requests' => $requests
]);
try {
$response_object = $this->service->spreadsheets->batchUpdate($this->spreadsheetId, $batchUpdateRequest);
}
catch (Exception $e) {
return show_error('An error occurred: <strong>'.$e->getMessage().'</strong></p>');
}