Use google spread sheets as your database
Tags: python, googlesheets, programming
Published: 2018-11-01 11:40:41

---
title: Use google spread sheets as your database
published: true
description: 
tags: python, googlesheets, programming
---
Today, we will see how we can use google spread sheets as our database.<br>
Like, if you are creating some internal app and you think you might need a database than you are wrong.<br> 
All you need is Google Spread Sheet and Python. <br>
Follow these steps and you will know how.
1. Go to [Google API Manager](https://console.cloud.google.com/apis/dashboard) and create a project.
<img width="1440" alt="google_api_manager" src="https://user-images.githubusercontent.com/20112458/47849012-34a93d80-ddf6-11e8-8cef-cdbdf3edb117.png">
<img width="1440" alt="name_project" src="https://user-images.githubusercontent.com/20112458/47849016-3b37b500-ddf6-11e8-97d9-b2c368be39f5.png">
<br>This is our [spread sheet](https://docs.google.com/spreadsheets/d/1fnOhXNT2vyuguQzGN1YSe-vsxFcDXIgFPIw_PbHaox0/edit?usp=sharing) which we are going to use as our database.
<img width="1440" alt="spread_sheet" src="https://user-images.githubusercontent.com/20112458/47849033-468ae080-ddf6-11e8-84c8-509fff8a9e34.png">
2. Add google drive api to the project which will allow us to access spread sheets inside google sheets account.
<img width="1440" alt="add_api1" src="https://user-images.githubusercontent.com/20112458/47849040-4a1e6780-ddf6-11e8-8c42-44524374d145.png">
<img width="1440" alt="add_api2" src="https://user-images.githubusercontent.com/20112458/47849041-4ab6fe00-ddf6-11e8-8765-098d50c5a15c.png">
<img width="1440" alt="add_api3" src="https://user-images.githubusercontent.com/20112458/47849042-4b4f9480-ddf6-11e8-9ce2-44481dd9a299.png">
3. Once thats added than we need to create some credential.
<img width="1440" alt="create_credential" src="https://user-images.githubusercontent.com/20112458/47849051-51de0c00-ddf6-11e8-9d5b-3a86bfe8f383.png">
Since we are doing this from a web server than we will add the "web server" option and give it access to application data.
<img width="1440" alt="choose_options" src="https://user-images.githubusercontent.com/20112458/47849054-530f3900-ddf6-11e8-8ff5-6424bbc33445.png">
After filling the options click "What credential do I need".
4. Next we will create a service account and allow the role "project editor" which we allow to access and edit data within the api.
5. Click continue and it will generate a json file that i will rename and add to the project as "statup_funding.json".
<img width="567" alt="cmd" src="https://user-images.githubusercontent.com/20112458/47849062-59051a00-ddf6-11e8-8106-2abcdbe8452d.png">
6. Open that file in a text editor than we will find a email address, in a property called "client_email".
7. Copy that and take it over the spread sheet, we can share the spread sheet to that email address to give us the access to the spread sheet from the api.
<img width="1437" alt="share_spreadsheet" src="https://user-images.githubusercontent.com/20112458/47849065-599db080-ddf6-11e8-9fbb-837a42ca5a74.png">
8. Use pip to install gspread and oauth2client packages.
```sh
$ pip install gspread oauth2client
```
<img width="1209" alt="install" src="https://user-images.githubusercontent.com/20112458/47849066-5a364700-ddf6-11e8-9bdb-66311772354e.png">
9. Than we will create a file "tutorial.py" and write our code inside that and use google sheets as our database.

## Python Program
```sh
#import library
import gspread
#Service client credential from oauth2client
from oauth2client.service_account import ServiceAccountCredentials
# Print nicely
import pprint
#Create scope
scope = ['https://spreadsheets.google.com/feeds']
#create some credential using that scope and content of startup_funding.json
creds = ServiceAccountCredentials.from_json_keyfile_name('startup_funding.json',scope)
#create gspread authorize using that credential
client = gspread.authorize(creds)
#Now will can access our google sheets we call client.open on StartupName
sheet = client.open('StartupName').sheet1
pp = pprint.PrettyPrinter()
#Access all of the record inside that
result = sheet.get_all_record()
```
We can do a lot more like access data in a particular row/column/cell.
```sh
result = sheet.row_values(5) #See individual row
# result = sheet.col.values(5) #See individual column
#result = sheet.cell(5,2) # See particular cell
pp = pprint.PrettyPrinter()
```
Update a particular cell and lot more
```sh
#update values
sheet.update_cell(2,9,'500000')  #Change value at cell(2,9) in the sheet
result = sheet.cell(2,9)
pp.pprint(result)
```
Hope you guys find this article helpful.