user4888557
user4888557

Reputation:

Convert and create a Google Drive Spreadsheet from a local CSV file via the Google Drive API

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}

Upvotes: 4

Views: 2071

Answers (1)

KRR
KRR

Reputation: 4917

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.

Upvotes: 1

Related Questions