下载 Google 电子表格 Google API PHP

海吉森白兰道

我正在尝试下载 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法使用python通过Google Drive API下载Google电子表格

使用Google云端硬盘API从文件夹下载电子表格

使用 PHP API 在 Google Drive 电子表格中对表格进行排序

使用Python从Google文档下载电子表格

下载公共Google电子表格

表单中是否存在检查表Google电子表格API PHP

Google Api Php客户端-电子表格权限错误

如何使用 php google api 更改电子表格的列宽

从Google电子表格PHP API的多个工作表中获取行

使用Google Sheets API公开电子表格?

使用 google 电子表格 api 转义文本

从Android上的Google云端硬盘下载Google表格电子表格

如何通过 PHP 中的 Google Spreadsheet API 创建带有 json 字符串的电子表格

带有PHP的Google电子表格API v4-如何将空行插入开头

将所有用户别名下载到Google电子表格

Google电子表格直接下载链接(仅一张)为Excel

创建链接以从Google云端硬盘下载整个电子表格

特定工作表的Json下载链接(Google电子表格)

如何从 Google 电子表格单元格下载纯文本?

Google电子表格脚本

Google电子表格公式

如何通过表格API v4更新Google电子表格

PHP Google 电子表格选择要写入的表格

使用Google API向Google电子表格“写入”时出错

在Java的Google驱动器中使用Google Spreadsheet API创建电子表格

使用Google云端硬盘中的Google Spreadsheets API创建电子表格

如何通过Python中的Google Drive API重命名Google电子表格?

如何在 google 电子表格中为 google api 项目打开访问权限?

使用Google Sheets API V4 API密钥创建电子表格