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


Create a Google Drive Spreadsheet from a local CSV file via the Google Drive API

我试图将本地csv文件上传到Google Drive并像Google电子表格一样显示它。然而,当我打开我的Google Drive,点击指向我的文件的链接时,我只能下载它,不能以电子表格的形式查看它。我试过使用?convert=true,但文件没有转换。我也试过使用application/vnd.google-apps.spreadsheet作为mime类型,但注意变化或我得到400 Bad request响应。

当我右键单击文件时,我可以选择用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的谷歌驱动器,但没有发现任何有用的。我想知道我是否应该使用谷歌电子表格API或谷歌驱动器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();
  }
}

我尝试上面的代码,它不会为我工作它会在登录后停止

只使用文件-> setMimeType("应用程序/vnd.google-apps.spreadsheet);

$createdFile = $service->files->insert($file, array(
  'data' => $data,
  'mimeType' => 'text/csv',
  'convert' => true,
));

它会为我工作

我必须添加另一个参数才能使其工作。'uploadType' => 'multipart'

$createdFile = $service->files->insert($file,array(
'data' => $data,
'mimeType' => 'text/csv',
'convert' => true,
'uploadType' => 'multipart',
));