I often try to append data to my google sheet. Most of the time I get an error. I searched at Google and I got a few possible ways of solving this error. Most of the proposed solutions are not working for me due to outdated code. Unfortunately, I've been unable to solve the error.
Here is my code:
<?php
require __DIR__ . '/vendor/autoload.php';
define('SCOPES', implode(' ', array(
Google_Service_Sheets::SPREADSHEETS,
Google_Service_Sheets::DRIVE,
Google_Service_Sheets::DRIVE_FILE)
));
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient() {
$client = new Google_Client();
$client->setAuthConfig('credentials.json');
$client->setApplicationName('theName');
$client->setScopes(SCOPES);
return $client;
}
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Sheets($client);
// Prints the names and majors of students in a sample spreadsheet:
// https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
$spreadsheetId = '1wAxarp4q9yo7kUUMaA3RudgmSTab53Dc9fReSbyreeM';
$range = 'Sheet1!A1:Z';
// $post = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
$firstName = "Mamunur";
$roll = "5200";
$class = "class";
$values = [
'Name' => $firstName,
'Roll' => $roll,
'Class' => $class,
'Dev' =>"dev"
];
$requestBody = new Google_Service_Sheets_ValueRange([
'values' => [$values]
]);
$params = [
'valueInputOption' => 'ROW'
];
$result = $service->spreadsheets_values->append($spreadsheetId, $range, $requestBody, $params);
printf("%d cells appended.", $result->getUpdates()->getUpdatedCells());