【发布时间】:2012-12-16 10:21:35
【问题描述】:
我正在尝试将本地 CSV 文件上传到 Google Drive,并在那里像 Google 电子表格一样显示它。但是,当我转到我的 Google 云端硬盘并单击我的文件的链接时,我只能下载它,而不能以电子表格的形式查看它。我试过使用?convert=true,但文件没有被转换。我也尝试使用 application/vnd.google-apps.spreadsheet 作为 mime 类型,但注意到更改,否则我收到 400 Bad request 响应。
当我右键单击该文件时,我可以选择使用 Google 电子表格打开,然后正确显示该文件。我在 Google 的当前文档中找不到任何关于此的内容,而且在 google 上的搜索也没有太大帮助。
到目前为止,我所做的是创建一个新的 空 Google 电子表格,并尝试用我的 CSV 文件填充它,但这给了我一个 500 Internal Error。
$file = new Google_DriveFile();
$file->setTitle('Exported data from ' . $this->event->title);
$file->setDescription('Exported data from ' . $this->event->title);
$file->setMimeType( 'text/csv' );
try {
$createdFile = $this->drive->files->insert($file, array(
'data' => $data,
'mimeType' => 'application/vnd.google-apps.spreadsheet'
), array('convert'=>true));
// Uncomment the following line to print the File ID
// print 'File ID: %s' % $createdFile->getId();
$additionalParams = array(
'newRevision' => false,
'data' => $data,
'convert'=>true //no change if this is true or false
);
$newFile = $this->drive->files->get( $createdFile->getId() );
$newFile->setMimeType('text/csv');
$updated = $this->drive->files->update($createdFile->getId(), $newFile, $additionalParams);
preint_r($updated);
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
我查看了 Google Drive 的 API,但没有发现任何有用的东西。我想知道我是应该使用 Google 电子表格 API 还是单独使用 Google Drive API?
提前非常感谢, 瓦尔德玛
【问题讨论】:
-
这属于 webapps.stackexchange.com
标签: php google-drive-api google-sheets