我正在尝试下载 Google 电子表格文件,但每次都出现错误。我基本上是在做这里写的:如何使用 php 将谷歌驱动器下载文件写入目录,但我得到: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 表格的二进制类型文件时,您才能这样做,您需要将其导出。此外,您应该创建驱动器服务而不是工作表服务。
$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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句