94
votes

I'm trying to find a link which allows me to download a CSV formatted version of my Google Spreadsheet. Currently I'm using:

https://docs.google.com/spreadsheets/d/DOCID/export?format=csv

This works great except that it only download the first Sheet. My document has multiple sheets. Does anyone know how to format this link so that it downloads either all the sheets or a specific sheet? Something like:

&sheet=all

or

&sheet=3
8
The accepted answer should be the last one: stackoverflow.com/a/50802790/462152Filippo De Luca

8 Answers

193
votes

Every document in Google Sheets supports the "Chart Tools datasource protocol", which is explained (in a rather haphazard way) in these articles:

  1. "Creating a Chart from a Separate Spreadsheet"
  2. "Query Language Reference"
  3. "Implementing the Chart Tools Datasource Protocol"

To download a specific sheet as a CSV file, replace {key} with the document's ID and {sheet_name} with the name of the sheet to export:

https://docs.google.com/spreadsheets/d/{key}/gviz/tq?tqx=out:csv&sheet={sheet_name}

The datasource protocol is quite flexible. Various other options include:

Response Format: Options include tqx=out:csv (CSV format), tqx=out:html (HTML table), and tqx=out:json (JSON data).

Export part of a sheet: Supply the range={range} option, where the range can be any valid range specifier, e.g. A1:C99 or B2:F.

Execute a SQL query: Supply the tq={query} option, such as tq=SELECT a, b, (d+e)*2 WHERE c < 100 AND x = 'yes'.

Export textual data: Supply the headers=0 option in case your fields contain textual data, otherwise they might be cut out during export.

37
votes
  1. Open your Google sheet
  2. Click the "Share" button and configure "Anyone with the link can view"
  3. Press F12 to launch debugging tools in your web browser and select the "Net" tab.
  4. Select "File -> Download As -> Comma-separated values .csv current sheet" (or whatever format you want, e.g. xlsx, ods, pdf, html, csv, or tsv)
  5. Copy the URL of the GET request from the "Net" tab

It will look something like this: https://docs.google.com/spreadsheets/d/KEY/export?format=csv&id=KEY&gid=SHEET_ID

Note, the "gid" parameter is the sheet ID, which you can find at the end of the URL of the open Google sheet. Reference: https://developers.google.com/sheets/api/guides/concepts#sheet_id

25
votes

This is a sample csv data can be downloaded. Download link for this data was made like this

  1. Open google sheet-> Blank ->File->Open
  2. Open file from "My Drive" or "Upload"
  3. File -> Publish to the web -> "Sheet name" option and "csv" option
9
votes

As of November 2020, the most elegant and simplest solution seems to be buried in @jrc's reply:

https://docs.google.com/spreadsheets/d/KEY/export?format=csv&gid=SHEET_ID

Here's a live example; given the Google Sheet that has a KEY of 1CTgM1g_aYoWFFpHU6A_qyqWGH0ulCFhs67uAcRVf1Rw

i.e: https://docs.google.com/spreadsheets/d/1CTgM1g_aYoWFFpHU6A_qyqWGH0ulCFhs67uAcRVf1Rw

To get the permalink to a CSV export of its first sheet (i.e. gid=0), append: /export?format=csv&gid=0:

https://docs.google.com/spreadsheets/d/1CTgM1g_aYoWFFpHU6A_qyqWGH0ulCFhs67uAcRVf1Rw/export?format=csv&gid=0

Note: if you're just exporting the first sheet, i.e. gid=0, then the URL is the same as the URL format posited in the original question, i.e.

(by default, the endpoint will assume gid=0 if it's left unspecified)

https://docs.google.com/spreadsheets/d/DOCID/export?format=csv
4
votes

2021 update... Question has been well answered in another post. I'll add some things to look out for ...

On the "publish to web" there are:

  1. Drop downs with options to choose the format and,
  2. A choice on which sheets you want included,
  3. Lastly a checkbox to ensure changes are republished

Publish to Web Google Sheets

3
votes

Based mostly on @kiwidrew's answer I made this little tool so you don't have to do it all by hand: Sheet2data.com

0
votes

The following URL format (as a template string) is the only one that worked from me:

`https://docs.google.com/spreadsheets/d/e/${id}/pub?output=csv&id=${id}&gid=${gid}`

This assumes the entire document has been published to the web.

0
votes

I've developed an handy python command line application called google-sheets-to-csv few month ago: https://pypi.org/project/google-sheets-to-csv/ which allow to download multiple sheets at once.

Basic usage on linux (probably works on windows as well, I haven't test):

pip install google-sheets-to-csv
gs-to-csv <spreadsheet ID> <sheet selector (regex)> <output directory>

where:

To download all the sheets at once you can do:

mkdir output/
gs-to-csv DOCID ".*" output/

You'll find one file per sheet.

Do not miss details in the project README ( regarding authentification the current published token is not yet validated by google you'll get an authentification warning like in this video used for the validation process: https://youtu.be/7zacMyv_ooU?t=73) If are afraid you can read the code here https://gitlab.com/micro-entreprise/google-sheets-to-csv (it's Open source software!) or use a google service account.