Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload CSV to Google Drive Spreadsheet using Drive v2 API

Tags:

How can I upload a local CSV file to Google Drive using the Drive API v2 so that the uploaded file is in the native Google Spreadsheet format. Preferably in Python, but a raw HTTP request will suffice.

What I tried:

  1. request body content-type: 'application/vnd.google-apps.spreadsheet', media_body content-type: 'text/csv'. --> 401 Bad Request

  2. request body content-type: 'application/vnd.google-apps.spreadsheet', media_body content-type: 'application/vnd.google-apps.spreadsheet'. --> 400 Bad Request

  3. ... (a couple of others such as leaving a property out and similar, usually got 400 or Drive didn't recognise it as a native spreadsheet)

like image 607
Zuza Avatar asked Oct 07 '12 03:10

Zuza


People also ask

Can you upload CSV to Google Drive?

If the CSV file you want to import is on your Google Drive, click My Drive at the top of the Import file window. Double-click the folder containing the CSV file, select the CSV file, then click the Select button at the bottom of the window.

How do I import a CSV file into Google spreadsheets using Google apps?

Save your script, open your Google Sheet and select the "Import from URL" item from the menu and enter the URL of a CSV file. When the script finishes running, you should see the contents of the CSV file displayed in the Toast notification message.


1 Answers

Your insert request should specify text/csv as the content-type. The trick to get the file converted is to add the ?convert=true query parameter to the request url:

https://developers.google.com/drive/v2/reference/files/insert

like image 62
Claudio Cherubino Avatar answered Oct 02 '22 01:10

Claudio Cherubino