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:
request body content-type: 'application/vnd.google-apps.spreadsheet', media_body content-type: 'text/csv'. --> 401 Bad Request
request body content-type: 'application/vnd.google-apps.spreadsheet', media_body content-type: 'application/vnd.google-apps.spreadsheet'. --> 400 Bad Request
... (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)
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With