我试图将本地CSV文件上传到Google Drive并在Google Spreadsheet中显示它。但是,当我访问我的Google云端硬盘并点击指向我的文件的链接时,我只能下载它,而不能将其视为电子表格。我试过使用?convert=true
,但文件没有被转换。我也尝试使用application/vnd.google-apps.spreadsheet
作为mime类型,但注意到更改或我得到400 Bad request
响应。通过Google Drive API从本地CSV文件创建Google Drive电子表格
当我右键单击该文件时,可以选择使用Google Spreadsheets打开,然后正确显示该文件。在谷歌当前的文档中,我无法找到任何有关这方面的信息,谷歌搜索也没有太多帮助。
我到目前为止所做的是创建一个新的空的谷歌电子表格,并试图用我的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();
}
我看过的谷歌驱动器的API,但没有发现任何有用的东西。我想知道我应该使用Google Spreadsheets API,还是仅使用Google Drive API?
提前许多感谢, 瓦尔德马
这属于webapps.stackexchange.com –