使用 API 在 Google 工作表中写入的问题

我正在尝试使用 google sheet API 来编写 google sheet API。


为此,我使用下面的代码


$spreadsheetId = 'XXXX';

$range = "Zoho Emails 2";

$values = [["This","is","a","new","row"],];

$body = new Google_Service_Sheets_ValueRange([

   "values" =>$values

]);

$params = [

   `valueInputOptions` => "RAW"

];

$insert = [

   "insertDataOption" => "INSERT_ROWS"

];

$result = $service->spreadsheets_values->append(

  $spreadsheetId,

  $range,

  $body,

  $params,

  $insert

);

但是当我运行这段代码时,这段代码给了我以下错误


[26-Nov-2019 22:45:36 America/Chicago] PHP Fatal error:  Uncaught exception 'Google_Exception' 

with message '(append) unknown parameter: ''' in 

/Google_sheet/vendor/google/apiclient/src/Google/Service/Resource.php:147

Stack trace:

#0 /Google_sheet/vendor/google/apiclient- 

services/src/Google/Service/Sheets/Resource/SpreadsheetsValues.php(65): 

Google_Service_Resource->call('append', Array, 'Google_Service_...')

#1 /Google_sheet/quickstart.php(99): Google_Service_Sheets_Resource_SpreadsheetsValues- 

>append('1k8sR-aV8O5W7jP...', 'Zoho Emails 2', Object(Google_Service_Sheets_ValueRange), Array, 

Array)

#2 {main}

thrown in /Google_sheet/vendor/google/apiclient/src/Google/Service/Resource.php on line 147

但我不明白为什么会发生这个错误。


有人可以帮我吗?


小怪兽爱吃肉
浏览 123回答 1
1回答

当年话下

这个改装怎么样?从:$params = [&nbsp; &nbsp;`valueInputOptions` => "RAW"];$insert = [&nbsp; &nbsp;"insertDataOption" => "INSERT_ROWS"];$result = $service->spreadsheets_values->append(&nbsp; $spreadsheetId,&nbsp; $range,&nbsp; $body,&nbsp; $params,&nbsp; $insert);至:$params = [&nbsp; "valueInputOption" => "RAW",&nbsp; "insertDataOption" => "INSERT_ROWS"];$result = $service->spreadsheets_values->append(&nbsp; $spreadsheetId,&nbsp; $range,&nbsp; $body,&nbsp; $params);笔记:此修改后的脚本假设您已经能够使用 Sheets API 获取和放置电子表格的值。参考:方法:电子表格.values.append如果这不是您问题的直接解决方案,我深表歉意。添加:当使用您在讨论中提供的另一个问题的链接中的脚本时,整个脚本反映了我修改的脚本如下。在运行脚本之前,请设置 和 的$spreadsheetId变量$range。在运行脚本之前,请确认credentials.json并删除token.json. 然后,运行脚本。届时,请重新授权。通过这一点,我认为脚本有效。范围从 更改Google_Service_Sheets::SPREADSHEETS_READONLY为Google_Service_Sheets::SPREADSHEETS。整个脚本:<?phprequire __DIR__ . '/vendor/autoload.php';if (php_sapi_name() != 'cli') {&nbsp;throw new Exception('This application must be run on the command line.');}function getClient(){&nbsp; $client = new Google_Client();&nbsp; $client->setApplicationName('Google Sheets API PHP Quickstart');&nbsp; $client->setScopes(Google_Service_Sheets::SPREADSHEETS);&nbsp; $client->setAuthConfig('credentials.json');&nbsp; $client->setAccessType('offline');&nbsp; $client->setPrompt('select_account consent');&nbsp; $tokenPath = 'token.json';&nbsp; if (file_exists($tokenPath)) {&nbsp; &nbsp; $accessToken = json_decode(file_get_contents($tokenPath), true);&nbsp; &nbsp; $client->setAccessToken($accessToken);&nbsp; }&nbsp; if ($client->isAccessTokenExpired()) {&nbsp; &nbsp; if ($client->getRefreshToken()) {&nbsp; &nbsp; &nbsp; &nbsp; $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $authUrl = $client->createAuthUrl();&nbsp; &nbsp; &nbsp; &nbsp; printf("Open the following link in your browser:\n%s\n", $authUrl);&nbsp; &nbsp; &nbsp; &nbsp; print 'Enter verification code: ';&nbsp; &nbsp; &nbsp; &nbsp; $authCode = trim(fgets(STDIN));&nbsp; &nbsp; &nbsp; &nbsp; $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);&nbsp; &nbsp; &nbsp; &nbsp; $client->setAccessToken($accessToken);&nbsp; &nbsp; &nbsp; &nbsp; if (array_key_exists('error', $accessToken)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new Exception(join(', ', $accessToken));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (!file_exists(dirname($tokenPath))) {&nbsp; &nbsp; &nbsp; &nbsp; mkdir(dirname($tokenPath), 0700, true);&nbsp; &nbsp; }&nbsp; &nbsp; file_put_contents($tokenPath, json_encode($client->getAccessToken()));}&nbsp; return $client;}$client = getClient();$service = new Google_Service_Sheets($client);$spreadsheetId = "###"; // Spreadsheet ID$range = "###"; // Sheet name$values = [["This","is","a","new","row"],];$body = new Google_Service_Sheets_ValueRange([&nbsp; &nbsp;"values" =>$values]);$params = [&nbsp; "valueInputOption" => "RAW",&nbsp; "insertDataOption" => "INSERT_ROWS"];$result = $service->spreadsheets_values->append(&nbsp; $spreadsheetId,&nbsp; $range,&nbsp; $body,&nbsp; $params);
打开App,查看更多内容
随时随地看视频慕课网APP