Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export a csv from Google Sheet API?

I can't find any reference to an API that enables Rest API clients to export an existing Google Sheet to a csv file.

https://developers.google.com/sheets/

I believe there should be a way to export them.

like image 737
user1447414 Avatar asked Jun 08 '16 14:06

user1447414


People also ask

How do I Download Google Sheets API?

To get started, select the programming language that you are using for development. Get the latest Google Sheets API client library for Go (alpha). Read the client library's developer's guide. This page contains information about getting started with the Google Sheets API using the Google API Client Library for Java.

How do I export specific data from Google Sheets?

Scroll down to the Send data section and select the type of file you wish to export your tab to. In this example, I want to export my data to another Google Sheet. You can choose to export your data to a new or existing sheet. You can also edit the destination of the file, as well as the file name and new file tab.


3 Answers

The following URL gives you the CSV of a Google spreadsheet per sheet. The sheet must be accessible by the public, by anyone with the link (unlisted).

The parameters you need to provide are:

  • sheet ID (that is simply the ID in the URL of a Google Spreadsheet https://docs.google.com/spreadsheets/d/{{ID}}/edit)
  • sheet name (that is simply the name of the sheet as given by the user)
https://docs.google.com/spreadsheets/d/{{ID}}/gviz/tq?tqx=out:csv&sheet={{sheet_name}}

With that URL you can run a GET-request to fetch the CSV. Or paste it in your browser address bar.

like image 63
Overbryd Avatar answered Sep 19 '22 13:09

Overbryd


You can use the Drive API to do this today -- see https://developers.google.com/drive/v3/web/manage-downloads#downloading_google_documents, however that will limit you to the first sheet of the document. The Sheets API doesn't expose exporting as CSV today, but may offer it in the future.

like image 39
Sam Berlin Avatar answered Sep 17 '22 13:09

Sam Berlin


Nobody's mentioned gspread yet, so here's how I did it:

#open sheet
sheet = gc.open_by_key(sheet_id)

#select worksheet
worksheet = sheet.get_worksheet(0)

#download values into a dataframe
df = pd.DataFrame(worksheet.get_all_records())

#save dataframe as a csv, using the spreadsheet name
filename = sheet.title + '.csv'
df.to_csv(filename, index=False)
like image 38
ScottieB Avatar answered Sep 16 '22 13:09

ScottieB