2013-01-02 136 views
4

我试图将本地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?

提前许多感谢, 瓦尔德马

+0

这属于webapps.stackexchange.com –

回答

4

尝试以下。这是从文件:将谷歌文档中引用,但我添加了转换参数:

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

该方法不存在,'致命错误:调用未定义的方法Google_DriveFile :: setConvert()'。我的坏, – wally

+0

应该看得更近。现在试试。 –

+0

哇,谢谢!这工作! – wally

2

我是尝试上面的代码将无法正常工作对我来说 将登录后只是停止

只使用 $文件 - > setMimeType('应用/ vnd.google-apps.spreadsheet);

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

它会为我

1

工作,我必须添加其他参数,使其工作。 'uploadType'=> '多'

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

现在,它的工作。