我经常尝试将数据附加到我的Google工作表中。大多数情况下,我会收到错误消息。我在Google上进行了搜索,并找到了解决该错误的几种可能方法。由于代码过时,大多数建议的解决方案对我来说都不起作用。不幸的是,我一直无法解决该错误。
这是我的代码:
<?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());