我正在尝试使用Google Sheet API来编写Google Sheet API。
为此,我正在使用以下代码
$Extension = ['jpeg','jpg','png'];
foreach($Extension as $ex){
echo $ex.'<br>';
}
但是当我运行此代码时,此代码给我以下错误
$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
);
但是我不明白为什么会发生此错误。
有人可以帮我吗?
答案 0 :(得分:2)
此修改如何?
$params = [
`valueInputOptions` => "RAW"
];
$insert = [
"insertDataOption" => "INSERT_ROWS"
];
$result = $service->spreadsheets_values->append(
$spreadsheetId,
$range,
$body,
$params,
$insert
);
$params = [
"valueInputOption" => "RAW",
"insertDataOption" => "INSERT_ROWS"
];
$result = $service->spreadsheets_values->append(
$spreadsheetId,
$range,
$body,
$params
);
如果这不是您问题的直接解决方案,我深表歉意。
使用您在讨论中提供的the link of your another question中的脚本时,反映出我修改后的脚本的整个脚本如下。在运行脚本之前,请设置变量$spreadsheetId
和$range
。在运行脚本之前,请确认credentials.json
并删除token.json
的文件。然后,运行脚本。当时,请再次授权。这样,我认为脚本可以工作。
范围从Google_Service_Sheets::SPREADSHEETS_READONLY
更改为Google_Service_Sheets::SPREADSHEETS
。
<?php
require __DIR__ . '/vendor/autoload.php';
if (php_sapi_name() != 'cli') {
throw new Exception('This application must be run on the command line.');
}
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Google Sheets API PHP Quickstart');
$client->setScopes(Google_Service_Sheets::SPREADSHEETS);
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
$tokenPath = 'token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
if ($client->isAccessTokenExpired()) {
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} else {
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
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([
"values" =>$values
]);
$params = [
"valueInputOption" => "RAW",
"insertDataOption" => "INSERT_ROWS"
];
$result = $service->spreadsheets_values->append(
$spreadsheetId,
$range,
$body,
$params
);