【发布时间】:2020-07-23 12:18:18
【问题描述】:
我正在使用 Google API 向 Google 表格添加行。这一切都运行良好,但新行被添加到末尾(使用 append 方法)。
我想在标题下方的顶部插入行,在我的例子中是第 2 行。
这是我添加到末尾的工作代码。如何指定要在给定位置插入行的位置。
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . $CredentialFile);
$client = new \Google_Client;
$client->useApplicationDefaultCredentials();
$client->setApplicationName("MyApp");
$client->setScopes(['https://www.googleapis.com/auth/drive','https://spreadsheets.google.com/feeds']);
if ($client->isAccessTokenExpired()) {
$client->refreshTokenWithAssertion();
}
$accessToken = $client->fetchAccessTokenWithAssertion()["access_token"];
ServiceRequestFactory::setInstance(new DefaultServiceRequest($accessToken));
$SheetId = "MySheetId";
$service = new \Google_Service_Sheets($client);
$sheet = $service->spreadsheets->get($SheetId);
$sheets = $sheet->getSheets();
$SheetTitle = $sheets[0]->properties->title;
$range = "{$SheetTitle}!A:D";
$values = [];
$row = [];
$row[] = "Tom";
$row[] = "Thumb";
$row[] = "tomthumb";
$row[] = "tom@thumb.com"
$values[] = $row;
$body = new \Google_Service_Sheets_ValueRange([
'values' => $values
]);
$params = [
'valueInputOption' => "RAW"
];
$result = $service->spreadsheets_values->append($SheetId, $range, $body, $params);
【问题讨论】:
标签: php google-sheets google-sheets-api