4

I want to download one of my google spreadsheet using curl and save it as a .csv file. Following is the command I am using:

(curl --silent --header "Authorization: GoogleLogin auth=AUTH_KEY" https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=SPREADSHEET_KEY&exportFormat=csv) > a.csv

This is downloading a file which is in pdf format. Can anybody help me in resolving this issue I am stuck with for 1 hr...

user739711
  • 1,842
  • 1
  • 25
  • 30

4 Answers4

8

This works: curl 'https://docs.google.com/spreadsheets/d/<yourkey>/export?exportFormat=csv', with adequate link-sharing parameters.

Johann8
  • 649
  • 7
  • 20
5

As of 6/3/2023, the steps are:

In Google Sheet, click the Share button, then Get Link.

Edit the link from:

https://docs.google.com/spreadsheets/d/<LONG-UGLY-CODE>/edit?usp=sharing

To:

https://docs.google.com/spreadsheets/d/<LONG-UGLY-CODE>/export?exportFormat=csv

*) Replace: <LONG-UGLY-CODE> with your code.

Curl the link, -L will follow temporary redirects:

curl -L 'https://docs.google.com/spreadsheets/d/<LONG-UGLY-CODE>/export?exportFormat=csv' -o my_sheet.csv

Slawomir
  • 3,194
  • 1
  • 30
  • 36
  • 2
    if you need to get only one sheet from multi-sheet file, add &gid=nnnnnnnn to the query so it would be .../export?gid=123456789&exportFormat=csv ... Gid number is displayed in the URL if you browse your sheets. – Kristjan Adojaan Jul 06 '22 at 14:57
  • 1
    if you need to get contents of only one cell add &range=A2 to the URL, referring to your required range. – Kristjan Adojaan Jul 06 '22 at 15:07
  • I'm trying that and I just get a bunch of HTML with a redirect. Yet the URL works in a browser. – Dave Hodgkinson Aug 24 '23 at 09:52
2

You need quotes. Without them, the & breaks the command into a background process and a foreground process - at which point, the exportFormat=csvsets a variable in your bash shell named exportFormat with a value of csv; the exportFormat doesn't go to the google page, so it spits it out in the default format, pdf: Which isn't what you want. Try: curl --silent -o a.csv--header "Authorization: GoogleLogin auth=AUTH_KEY" "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=SPREADSHEET_KEY&exportFormat=csv"

Jack Simth
  • 131
  • 1
  • 8
1

Try to add "&hl" and quotes

ie. curl --silent --header "Authorization: GoogleLogin auth=AUTH_KEY" "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=SPREADSHEET_KEY&hl&exportFormat=csv"

Clotho
  • 56
  • 2