rafulin
rafulin

Reputation: 99

Google Sheets API - 0 cells updated

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

Answers (1)

Tanaike
Tanaike

Reputation: 201378

How about this modification?

Answer 1:

0 cells updated.

About the reason of above issue, if you want to use "majorDimension" => "ROWS", please modify as follows.

From:

$body = new Google_Service_Sheets_ValueRange(
  ["majorDimension" => "ROWS"],
  ["values" => $value]
);

To:

$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
]);

Answer 2:

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?

From:

$range = 'A1';

To:

$range = 'Sheet1!A1';

and

$range = 'Sheet1!A1:B1';

References:

Upvotes: 2

Related Questions