1 回答
TA贡献1890条经验 获得超9个赞
这个改装怎么样?
从:
$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
);
笔记:
此修改后的脚本假设您已经能够使用 Sheets API 获取和放置电子表格的值。
参考:
方法:电子表格.values.append
如果这不是您问题的直接解决方案,我深表歉意。
添加:
当使用您在讨论中提供的另一个问题的链接中的脚本时,整个脚本反映了我修改的脚本如下。在运行脚本之前,请设置 和 的$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
);
- 1 回答
- 0 关注
- 122 浏览
添加回答
举报