I wish to use the following API to update one cell: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update
I was unable to get the API library to work with my package, so I've been using HTTP requests for other situations.
Here is a similar approach for this situation:
$request_url = "https://sheets.googleapis.com/v4/spreadsheets/SPREADSHEET_ID/values/Sheet2!A2";
$access_token = get_access_token();
$headers = array();
$headers[] = 'Authorization: Bearer ' . $access_token;
try {
$data = array('valueInputOption' => 'RAW',
'values' => [0 => strval($post_id)]);
$curl = new \MyApp\Http\Curl($request_url, array(
CURLOPT_HTTPHEADER => $headers,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => http_build_query($data)
));
$response_json = $curl->__toString();
var_dump($response_json);
} catch (\RuntimeException $ex) {
die(sprintf('Http error %s with code %d', $ex->getMessage(), $ex->getCode()));
}
But I get:
string(573) "{
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"values[0]\": Cannot bind query parameter. Field 'values[0]' could not be found in request message.",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"description": "Invalid JSON payload received. Unknown name \"values[0]\": Cannot bind query parameter. Field 'values[0]' could not be found in request message."
}
]
}
]
}
}
"
How do I post the values in this API request?
Here is it working on the Try this API part of the Google page.