我試圖將本地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?
提前許多感謝, 瓦爾德馬
這屬於webapps.stackexchange.com –