php – 通过Google Drive API从本地CSV文件创建Google云端硬盘电
发布时间:2020-12-13 17:43:48 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试将本地CSV文件上传到Google云端硬盘并将其显示为Google电子表格.但是,当我转到我的Google云端硬盘并点击指向我文件的链接时,我只能下载它,而不是将其视为电子表格.我尝试过使用?convert = true但文件没有转换.我还尝试使用application / vnd.goog
|
我正在尝试将本地CSV文件上传到Google云端硬盘并将其显示为Google电子表格.但是,当我转到我的Google云端硬盘并点击指向我文件的链接时,我只能下载它,而不是将其视为电子表格.我尝试过使用?convert = true但文件没有转换.我还尝试使用application / vnd.google-apps.spreadsheet作为mime类型,但注意到更改或我收到400 Bad请求响应.
当我右键单击该文件时,我可以选择使用Google Spreadsheets打开,然后正确显示该文件.我在Google目前的文档中找不到任何相关内容,谷歌搜索也没有多大帮助. 到目前为止我所做的是创建一个新的空Google电子表格,并尝试用我的CSV文件填充它,但这给了我一个500内部错误. $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云端硬盘的API,但没有找到任何有用的内容.我想知道我是否应该使用Google Spreadsheets API,或者Google Drive API是否可以单独使用? 提前谢谢了, 解决方法
请尝试以下方法.它来自Google文档中的File:insert引用,但我添加了convert参数:
/**
* Insert new file.
*
* @param Google_DriveService $service Drive API service instance.
* @param string $title Title of the file to insert,including the extension.
* @param string $description Description of the file to insert.
* @param string $parentId Parent folder's ID.
* @param string $mimeType MIME type of the file to insert.
* @param string $filename Filename of the file to insert.
* @return Google_DriveFile The file that was inserted. NULL is returned if an API error occurred.
*/
function insertFile($service,$title,$description,$parentId,$mimeType,$filename) {
$file = new Google_DriveFile();
$file->setTitle($title);
$file->setDescription($description);
$file->setMimeType($mimeType);
// Set the parent folder.
if ($parentId != null) {
$parent = new ParentReference();
$parent->setId($parentId);
$file->setParents(array($parent));
}
try {
$data = file_get_contents($filename);
$createdFile = $service->files->insert($file,array(
'data' => $data,'mimeType' => $mimeType,'convert' => true,));
// Uncomment the following line to print the File ID
// print 'File ID: %s' % $createdFile->getId();
return $createdFile;
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
