下载 Google 电子表格 Google API PHP
Download Google SpreadSheet Google API PHP
我正在尝试下载 Google 电子表格文件,但每次都出现错误。我基本上是按照这里写的做:,但是我得到:Warning: Undefined property: Google_Service_Sheets::$files in
当我 运行 以下代码时:
$client = new \Google_Client();
$client->setApplicationName('GoogleSheets');
$client->setScopes([\Google_Service_Drive::DRIVE]);
$client->setAccessType('offline');
$client->setAuthConfig('credentials.json');
$service = new Google_Service_Sheets($client);
$fileId = mySpreadSheetID;
// Retrieve filename.
$file = $service->files->get($fileId);
$fileName = 'Test_'.time();
// Download a file.
$content = $service->files->get($fileId, array("alt" => "media"));
$handle = fopen("./".$fileName, "w+");
while (!$content->getBody()->eof()) {
fwrite($handle, $content->getBody()->read(1024));
}
fclose($handle);
我做错了什么?
如果它是 Google sheet 的二进制类型文件,则您只能那样做,您将需要 export 它。此外,您应该创建驱动服务而不是 sheets 服务。
$service = new Google_Service_Drive($client);
$response = $service->files->export($createdFile->id, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', array('alt' => 'media' ));
$content = $response->getBody()->getContents();
file_put_contents('test.xlsx',$content);
我正在尝试下载 Google 电子表格文件,但每次都出现错误。我基本上是按照这里写的做:Warning: Undefined property: Google_Service_Sheets::$files in
当我 运行 以下代码时:
$client = new \Google_Client();
$client->setApplicationName('GoogleSheets');
$client->setScopes([\Google_Service_Drive::DRIVE]);
$client->setAccessType('offline');
$client->setAuthConfig('credentials.json');
$service = new Google_Service_Sheets($client);
$fileId = mySpreadSheetID;
// Retrieve filename.
$file = $service->files->get($fileId);
$fileName = 'Test_'.time();
// Download a file.
$content = $service->files->get($fileId, array("alt" => "media"));
$handle = fopen("./".$fileName, "w+");
while (!$content->getBody()->eof()) {
fwrite($handle, $content->getBody()->read(1024));
}
fclose($handle);
我做错了什么?
如果它是 Google sheet 的二进制类型文件,则您只能那样做,您将需要 export 它。此外,您应该创建驱动服务而不是 sheets 服务。
$service = new Google_Service_Drive($client);
$response = $service->files->export($createdFile->id, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', array('alt' => 'media' ));
$content = $response->getBody()->getContents();
file_put_contents('test.xlsx',$content);