我正在尝试下载一个谷歌电子表格文件,但我每次都得到一个错误。我基本上是在做这里写的:how to write a google drive download file to a directory using 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);我做错了什么?
发布于 2021-04-26 23:51:07
你只能这样做,如果它是一个谷歌工作表的二进制类型的文件,你将需要export它。另外,您应该创建一个驱动服务,而不是一个工作表服务。
$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);https://stackoverflow.com/questions/67269709
复制相似问题