Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the google drive API to download a spreadsheet in csv format

I'm sorry if this is an obvious question, I'm still pretty new to the API. I'm using the python drive api library, and trying to download a google spreadsheet as a csv.
When I used files.get, it spat out a file with no downloadUrl, and with no 'text/csv' key in the export links field.
If it's not possible, I can find a workaround, but I'm hoping it is, since it is possible to do manually (file->download_as->csv)

Do I need to use the google document list api?

thanks, Matt

like image 357
user1546842 Avatar asked Jul 23 '12 20:07

user1546842


People also ask

Can you download a Google sheet as a CSV?

How to Export a File From Google Sheets. To export your spreadsheet again as a CSV, you can just go to the “File” menu and select “Download as.” There, select “Comma-separated values”. This will download the file as a CSV.


1 Answers

Update: I have posted another answer that works with the Spreadsheets v4 API.

Old Answer:

The answer from Alain is correct, but you also need to set the gid=parameter to specify which worksheet to export.

For example, if your 'application/pdf' export link is like this:

docs.google.com/feeds/download/spreadsheets/Export?key=<FILE_ID>&exportFormat=pdf 

You can just change it to this to download the first worksheet:

docs.google.com/feeds/download/spreadsheets/Export?key<FILE_ID>&exportFormat=csv&gid=0 

There is a bit of a problem, though as there is no reliable way to get the gid for a given worksheet through the API and they are not zero based indexes. If you delete a worksheet, that gid does not get reused. You can see the gid in the URL in your browser though, so if your worksheet information is constant you can just get that from there. See http://code.google.com/a/google.com/p/apps-api-issues/issues/detail?id=1813 and http://code.google.com/a/google.com/p/apps-api-issues/issues/detail?id=3240 for more info on that problem.

like image 155
Peter Haight Avatar answered Oct 02 '22 10:10

Peter Haight