Reputation: 99
I have a sheet that needs to be updated with some data. Code that should add «hello» and «bye» to my sheet:
require __DIR__ . '/vendor/autoload.php';
function getClient()
{
...
}
$client = getClient();
$service = new Google_Service_Sheets($client);
$spreadsheetId = 'ID';
$range = 'A1';
$value = [
["hello", "bye"]
];
$body = new Google_Service_Sheets_ValueRange(
["majorDimension" => "ROWS"],
["values" => $value]
);
$params = array('valueInputOption' => 'USER_ENTERED');
$requestBody = new Google_Service_Sheets_ValueRange();
$result = $service->spreadsheets_values->update($spreadsheetId, $range,
$body, $params);
printf("%d cells updated.", $result->getUpdatedCells());
result:
0 cells updated.
I also tried to change $range = 'A1’;
to something like Sheet1!A1
or A1-B2
and variety of another ranges; but in that case I receive error:
{ "error":
{ "code": 400, "message": "Unable to parse range: A1-B2",
"errors":
[
{ "message": "Unable to parse range: A1-B2",
"domain": "global",
"reason": "badRequest" } ],
"status": "INVALID_ARGUMENT" } }
Upvotes: 1
Views: 83
Reputation: 201378
How about this modification?
0 cells updated.
About the reason of above issue, if you want to use "majorDimension" => "ROWS"
, please modify as follows.
$body = new Google_Service_Sheets_ValueRange(
["majorDimension" => "ROWS"],
["values" => $value]
);
$body = new Google_Service_Sheets_ValueRange([
"majorDimension" => "ROWS",
"values" => $value
]);
In your case, I think that the following modification can be also used.
$body = new Google_Service_Sheets_ValueRange([
"values" => $value
]);
also tried to change $range = 'A1’; to something like Sheet1!A1 or A1-B2 and variety of another ranges; but in that case I receive error:
About the reason of above issue, if you want to use the range, how about the following modification?
$range = 'A1';
$range = 'Sheet1!A1';
and
$range = 'Sheet1!A1:B1';
Upvotes: 2