Download/Export Public Google Spreadsheet as TSV from Command Line? Download/Export Public Google Spreadsheet as TSV from Command Line? curl curl

Download/Export Public Google Spreadsheet as TSV from Command Line?


I found this to be frustratingly undocumented. I'm sure it's documented somewhere... but I never found it.

The premise is that your Google Sheet is published publicly. This is not intuitive for many folks. (Choose File -> Publish to Web...)

When you publish a sheet, you are given a url like this to copy:https://docs.google.com/spreadsheets/d/1XsfK2TN418FuEstNGG2eI9FmEV-4eY-FnndigHWIhk4/pubhtml

That url is nicely browsable... but it's not the downloadable CSV I wanted. Through a lengthy combination of search and trial-and-error I came up with this:

curl "https://docs.google.com/spreadsheets/d/1XsfK2TN418FuEstNGG2eI9FmEV-4eY-FnndigHWIhk4/export?gid=0&format=csv"

I find it to be tremendously helpful. I hope somebody comments with a link to the official docs explaining this in more detail.


I can download through the shell in this way:

  1. File => Publish to Web
  2. Choose a Sheet and the format do you want to download.
  3. Click on Publish
  4. Copy the link
  5. and then use it:

    wget -O ./filename.csv "LINK"

    or

    curl -L "LINK" > ./filename.csv

in my case it worked as expected.

Plus I think that it publish all the formats so you can choose what to download changing the last part of the URL without un-publish and re-publish it:

output=tsvoutput=csv


To add to the answer written by @mdahlman: there is a gid=<value> argument that lets you chose the sheet to view (as CSV and TSV support the viewing of just one sheet). This is a sheet ID and you can pick it up from the URL of each sheet.

So, to get a CSV/TSV publish link, do this:

  1. Publish the document to get a URL like https://docs.google.com/spreadsheets/d/e/{key}/pub?output=tsv.

  2. Then for each spreadsheet:

    1. Click on it.

    2. View its URL in your browser's address bar. It'll end with edit#gid={gid}. That's what you want.

    3. Make your URL from the one in step 1. and gid in 2.2.: https://docs.google.com/spreadsheets/d/e/{key}/pub?output=tsv&gid={gid}.

GIDs don't go in sequence (0, 1, 2,...). They are long numbers (9 digits for me), seemingly in no straight order or anything, so they're really more like sheet keys than what one would expect as an "id".

In my document, one of the GIDs was zero. I am assuming it's some sort of a default or a first created sheet. That explains why gid=0 worked for some people above, yet produced an error for others (those who don't have a sheet with such GID... they have possibly deleted it or something).