Sheety is an API that allows you to update Google Sheets. It’s fairly straightforward to use but the documentation is fairly minimal. The below examples are in Python. These examples are all with authentication set to Non, the default. Will add authentication section soon hopefully.
Table of Contents
Greate a Google Sheet
Create a simple google sheet with ‘A’ in the top-left cell and ‘B’ in the cell left of this. Copy the URL of the sheet to your clipboard.
Allow Sheety to access Google Sheet.
- Go to https://sheety.co/
- Click on the Connect to Google Sheet button.
- Click on the Sign in with Google button and follow the instructions.
- Click on New Project and select from Google Sheet… from the dropdown
- Past the URL from your spreadsheet (the one you copied above)
- You will then be taken to a page that gives you (fairly cryptic) details of how to use the API. Keep that page open as we will need info from it above.
Adding Date to Sheet
The below is the code ‘to add an item to the sheet.
- Under POST Add a row to your sheet copy the URL and use it to set SHEETY_ENDPOINT below.
- To the right of POST Add a row to your sheet make sure it is set to enabled.
- The key in the dictionary below that is sheet1 needs to be the same as the last word of the URL.
- All keys in the dictionary need to be in lower case.
- The code below should add a row to the sheet.
import requests SHEETY_ENDPOINT = 'https://api.sheety.co/xxxxxxxxxxxxxxxx/sheetyTest/sheet1' data = { "sheet1": { "a": "X", "b": "Y", } } response = requests.post(url=SHEETY_ENDPOINT, json=data) print("response.status_code =", response.status_code) print("response.text =", response.text)
When you run this code you should get the following output in your python counsel
response.status_code = 200 response.text= { "sheet1": { "a": "X", "b": "Y", "id": 4 } }
Your Google Sheet should now be updated.
Case of column names and key name case
Worth mentioning here that the headings above (A/B) if they are multiple works use camelCase. So if the heading was FIRST NAME you would use firesName. This is also the same for the key (in this case shee11).
Reading the Google Sheet
This is even simpler, just do requests.get for the same URL.
- Note it is a GET request.
import requests SHEETY_ENDPOINT = 'https://api.sheety.co/xxxxxxxxxxxxxxxx/sheetyTest/sheet1' response = requests.get(url=SHEETY_ENDPOINT) data = response.jason()) print("response.status_code =", response.status_code) print("response.text= ", response.text) print(data)
It should return
response.status_code = 200 response.text= { "sheet1": [ { "a": "X", "b": "Y", "id": 2 } ] } {'sheet1': [{'a': 'X', 'b': 'Y', 'id': 2}]}
Edit row in the Google Sheet
The id is the row number, here we have hardcoded row 2 (the title is the first row so this is the first row of actual data). The row number is returned in the dictionary as id. So the use-case if often get a row, change its value then update it.
- Note it is a PUT request.
import requests SHEETY_ENDPOINT = f'https://api.sheety.co/xxxxxxxxxxxxxxxx/sheetyTest/sheet1/' id = 2 data = { "sheet1": { "a": "C", "b": "D", } } endpoint = f"{SHEETY_ENDPOINT}/{id}" response = requests.put(url=endpoint, json=data) print("response.status_code =", response.status_code) print("response.text= ", response.text)
It should return
response.status_code = 200 response.text= { "sheet1": [ { "a": "C", "b": "D", "id": 2 } ] }
Deleting a row
Deleting a row is similar to updating one. See the previous section for where to get id.
import requests SHEETY_ENDPOINT = f'https://api.sheety.co/xxxxxxxxxxxxxxxx/sheetyTest/sheet1/' id = 2 endpoint = f"{SHEETY_ENDPOINT}/{id}" response = requests.delete(url=endpoint) print("response.status_code =", response.status_code) print("response.text= ", response.text)
Filtering when get the sheet.
You can do basic filtering when you get the sheet (i.e. column=value). You do this create a dictionary like this:
params = {
“filter[departmentName]”: 10,
“filter[sex]”, M,
}
And add params to the request (i.e. modify get request for getting sheet to)
response = requests.get(url=SHEETY_ENDPOINT, parmas=params)
Ime leaving the documentation from the docs below as I have not tested this yet, will test soon
Sheety supports basic row filtering by using the filter
query string.
Simply add ?filter[property]=value
to the end of the URL (where property is the name of the property to filter on, and value is the value you’d like to match).
For example, to show all the family friend theme parks, the filter query string would look like:
https://api.sheety.co/phill/themeParks/parks?filter[familyFriendly]=true
You can stack multiple filters by joining them with a &
, like so:
?filter[familyFriendly]=true&filter[country]=UK