通过Google Drive API从本地CSV文件转换并创建Google云端硬盘电子表格

时间:2015-05-11 18:36:43

标签: google-drive-api

我正在尝试将本地CSV文件上传到Google云端硬盘并将其显示为Google电子表格。但是,当我转到我的Google云端硬盘并点击指向我文件的链接时,我只能下载它,而不是将其视为电子表格。我尝试过使用?convert = true但文件没有转换。我也尝试使用application/vnd.google-apps.spreadsheet作为mime类型,但注意到更改或我得到400 Bad请求响应。

  

异常'Google_ServiceException',消息'错误调用POST   https://www.googleapis.com/upload/drive/v2/files?convert=true&uploadType=multipart:   (400)提供的无效mime类型   /var/www/html/twittery/lib/google-api-php-client/src/io/Google_REST.php:66   堆栈跟踪:#0   /var/www/html/twittery/lib/google-api-php-client/src/io/Google_REST.php(36):   Google_REST :: decodeHttpResponse(Object(Google_HttpRequest))#1   /var/www/html/twittery/lib/google-api-php-client/src/service/Google_ServiceResource.php(186):   Google_REST :: execute(Object(Google_HttpRequest))#2   /var/www/html/twittery/lib/google-api-php-client/src/contrib/Google_DriveService.php(484):   Google_ServiceResource-> __ call('insert',Array)#3   /var/www/html/twittery/generate_tweets_GSpreadsheet.php(84):   Google_FilesServiceResource-> insert(Object(Google_DriveFile),Array)   #4 {main}

1 个答案:

答案 0 :(得分:1)

The json_last_error for uploading a CSV file should be "text/csv".

As per the documentation for Files:insert, make sure you are giving the same mimeType in

mimeType

and also

$file->setMimeType($mimeType);

When creating or updating a file in Google Drive, you can convert the uploaded file into a Google Docs, Sheets or Slides document by using the $createdFile = $service->files->insert($file, array( 'data' => $data, 'mimeType' => $mimeType, 'convert' => true, )); query parameter.

You can refer to this page to see what are the support conversions for each type of file you upload.