2013-01-02 132 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', 
)); 

現在,它的工作。