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

4
我试图将本地CSV文件上传到Google Drive并在那里像Google电子表格一样显示它。然而,当我进入我的Google Drive并点击文件链接时,我只能下载它,无法将其视为电子表格查看。我尝试使用?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 Spreadsheets API还是同时使用Google Drive API?

非常感谢, Waldemar


这应该放在webapps.stackexchange.com上。 - Justin Dearing
3个回答

4

请尝试以下操作。这是从Google文档中的File:insert reference中提取的,但我添加了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();
  }
}

该方法不存在,致命错误:调用未定义的方法Google_DriveFile :: setConvert() - wally
我的错,应该仔细看一下。现在再试试。 - Jay Lee
@JayLee 现在插入等于创建,转换选项被删除了吗?我收到了未知参数“Convert”的消息。 - JayIsTooCommon

2
我尝试了以上代码,但它对我不起作用。它只会在登录后停止。请使用以下代码:$file->setMimeType('application/vnd.google-apps.spreadsheet');
$createdFile = $service->files->insert($file, array(
  'data' => $data,
  'mimeType' => 'text/csv',
  'convert' => true,
));

它对我有效


1

我需要添加另一个参数才能使它正常工作。 'uploadType' => 'multipart'

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

现在它正在工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接