通过Google Drive API从本地CSV文件转换并创建Google云端硬盘电子表格 [英] Convert and create a Google Drive Spreadsheet from a local CSV file via the Google Drive API

查看:362
本文介绍了通过Google Drive API从本地CSV文件转换并创建Google云端硬盘电子表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


异常'Google_ServiceException'带消息'错误调用POST
https://www.googleapis.com/upload/drive/v2/files?convert=true&uploadType=multipart
(400)
/var/www/html/twittery/lib/google-api-php-client/src/io/Google_REST.php:66
中提供的无效MIME类型'堆栈跟踪:# 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 / c ontrib / 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}


解决方案

用于上传CSV文件的 mimeType 应该是text / csv。

根据文件:插入的文档,请确保您提供的是相同的 mimeType in

  $ file-> setMimeType($ mimeType); 

还有

 < ($ b $ data'=> $ data,
'mimeType'=> $ mimeType,code $ $ createdFile = $ service-> files-&
'convert'=> true,
));

在Google云端硬盘中创建或更新文件时,您可以将上传的文件转换为Google文档,通过使用 convert 查询参数来创建表单或幻灯片文档。

您可以参考页面查看您上传的每种文件类型的支持转换。


I'm trying to upload a local CSV-file to Google Drive and display it like a Google Spreadsheet there. However, when I go to my Google Drive and click the link to my file, I can only download it, not view it as a spreadsheet. I've tried using the ?convert=true but the file doesn't get converted. I've also tried using application/vnd.google-apps.spreadsheet as the mime type but noting changes or I get a 400 Bad request response.

exception 'Google_ServiceException' with message 'Error calling POST https://www.googleapis.com/upload/drive/v2/files?convert=true&uploadType=multipart: (400) Invalid mime type provided' in /var/www/html/twittery/lib/google-api-php-client/src/io/Google_REST.php:66 Stack trace: #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}

解决方案

The mimeType 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

$file->setMimeType($mimeType);

and also

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

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 convert query parameter.

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

这篇关于通过Google Drive API从本地CSV文件转换并创建Google云端硬盘电子表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆