gpt4 book ai didi

php - 通过 Google Drive API 从本地 CSV 文件创建 Google Drive 电子表格

转载 作者:可可西里 更新时间:2023-10-31 22:13:03 25 4
gpt4 key购买 nike

我正在尝试将本地 CSV 文件上传到 Google 云端硬盘,并在其中像 Google 电子表格一样显示它。但是,当我转到我的 Google 云端硬盘并单击指向我的文件的链接时,我只能下载它,而不能将其作为电子表格查看。我试过使用 ?convert=true 但文件没有得到转换。我也尝试过使用 application/vnd.google-apps.spreadsheet 作为 mime 类型,但注意更改或者我收到 400 Bad request 响应。

当我右键单击该文件时,我可以选择使用 Google 电子表格打开,然后它会正确显示该文件。我在 Google 的当前文档中找不到任何关于此的内容,并且在 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 引用,但我添加了转换参数:

/**
* 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();
}
}

关于php - 通过 Google Drive API 从本地 CSV 文件创建 Google Drive 电子表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14115468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com