Skip to content
Snippets Groups Projects
Commit b1038bfd authored by qiuolivi's avatar qiuolivi
Browse files

Changed Step 3

parent 0788d5fc
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# Google Sheets API Tutorial
---
---
%% Cell type:markdown id: tags:
### How to Get Started
%% Cell type:markdown id: tags:
1. Create a project inside Google Developer Console
1. This can be found at https://console.developers.google.com/projectselector2/apis/credentials?pli=1&supportedpurview=project
(you must use your personal email not your MSU email)
2. Inside that project enable the Google sheets API and Google Drive for your new project
1. This is found by using the search bar to search for Google API and Google Drive
3. Create a Google service account through the Google developer portal
4. Go to “APIs & Services > Credentials” and choose “Create credentials > Service account key”.
5. Fill out the form (making sure to add editor privledges to the service account)
6. Click “Create” and “Done”.
7. Press “Manage service accounts” above Service Accounts.
8. Press on ⋮ near recently created service account and select “Manage keys” and then click on “ADD KEY > Create new key”.
9. Select JSON key type and press “Create”.
10. In your python file import gspread
11. Copy Json key into python file as a dictionary
3. Go to “APIs & Services > Credentials” and choose “Create credentials > Service Account".
4. Fill out the form (making sure to add editor privledges to the service account)
5. Click “Create” and “Done”.
6. Press “Manage service accounts” above Service Accounts.
7. Press on ⋮ near recently created service account and select “Manage keys” and then click on “ADD KEY > Create new key”.
8. Select JSON key type and press “Create”.
9. In your python file import gspread
10. Copy Json key into python file as a dictionary
for more information/ documentation on gspread head here
https://docs.gspread.org/en/latest/oauth2.html
%% Cell type:code id: tags:
``` python
!pip install gspread
```
%% Output
Requirement already satisfied: gspread in c:\programdata\anaconda3\lib\site-packages (5.1.1)
Requirement already satisfied: google-auth>=1.12.0 in c:\programdata\anaconda3\lib\site-packages (from gspread) (2.6.0)
Requirement already satisfied: google-auth-oauthlib>=0.4.1 in c:\programdata\anaconda3\lib\site-packages (from gspread) (0.5.0)
Requirement already satisfied: pyasn1-modules>=0.2.1 in c:\programdata\anaconda3\lib\site-packages (from google-auth>=1.12.0->gspread) (0.2.8)
Requirement already satisfied: cachetools<6.0,>=2.0.0 in c:\programdata\anaconda3\lib\site-packages (from google-auth>=1.12.0->gspread) (5.0.0)
Requirement already satisfied: rsa<5,>=3.1.4; python_version >= "3.6" in c:\programdata\anaconda3\lib\site-packages (from google-auth>=1.12.0->gspread) (4.8)
Requirement already satisfied: six>=1.9.0 in c:\programdata\anaconda3\lib\site-packages (from google-auth>=1.12.0->gspread) (1.12.0)
Requirement already satisfied: requests-oauthlib>=0.7.0 in c:\users\sam\appdata\roaming\python\python37\site-packages (from google-auth-oauthlib>=0.4.1->gspread) (1.3.0)
Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in c:\programdata\anaconda3\lib\site-packages (from pyasn1-modules>=0.2.1->google-auth>=1.12.0->gspread) (0.4.8)
Requirement already satisfied: requests>=2.0.0 in c:\users\sam\appdata\roaming\python\python37\site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib>=0.4.1->gspread) (2.27.1)
Requirement already satisfied: oauthlib>=3.0.0 in c:\programdata\anaconda3\lib\site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib>=0.4.1->gspread) (3.1.1)
Requirement already satisfied: charset-normalizer~=2.0.0; python_version >= "3" in c:\programdata\anaconda3\lib\site-packages (from requests>=2.0.0->requests-oauthlib>=0.7.0->google-auth-oauthlib>=0.4.1->gspread) (2.0.10)
Requirement already satisfied: certifi>=2017.4.17 in c:\programdata\anaconda3\lib\site-packages (from requests>=2.0.0->requests-oauthlib>=0.7.0->google-auth-oauthlib>=0.4.1->gspread) (2021.5.30)
Requirement already satisfied: idna<4,>=2.5; python_version >= "3" in c:\programdata\anaconda3\lib\site-packages (from requests>=2.0.0->requests-oauthlib>=0.7.0->google-auth-oauthlib>=0.4.1->gspread) (2.8)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\users\sam\appdata\roaming\python\python37\site-packages (from requests>=2.0.0->requests-oauthlib>=0.7.0->google-auth-oauthlib>=0.4.1->gspread) (1.26.8)
%% Cell type:code id: tags:
``` python
import gspread
import json
```
%% Cell type:markdown id: tags:
## Testing out the API
%% Cell type:code id: tags:
``` python
credentials = {
"type": "service_account",
"project_id": "loyal-weaver-341720",
"private_key_id": "639a8bb76d0d9f96ae595ee0c3d68b7f51c46c2a",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDWR9gprOoJZUw7\nVIa7qVxjXUEBCZb9Jd912Cr/rrFQ6b8Pk7vQl3tx8jUJxOC6/V9eSc7qdKZzED8I\nXR0YTWradtu4Kd7AzMwZB+zoBCNFJyVNyLyZCmOKFpsnIDNElIsQ2uRg2jqQbFyS\nyYD0Y97oRAbitJyemJ2SfUfYd+Qj2Sp3mFgsZQaufHR3vZIDWxR7C3feu74nfWud\nJAPUMf3Uku/kl/MHniZo3f5zXAnQXwMC39MSYxu7N9UNxu0s0Y+AwmbsLjkMJ2JB\ngOq7ki+HPIwsy+GN+nVcHpXlwKGs9irDV2c5rQhcL1Yiw4xCVRJwGS0Hp39z73tk\nfHEU/gs9AgMBAAECggEADLcss6RoKvCQ6oBkkmRFcDwLbHVWOWVy8jpGqu+B5CxK\nwzDgF0u17pgR36eIMnBwpVa56Z3Mds2DAAsOe++0JIyjEzWg204L2nxm6Ym9UhA1\nnX0I/Afqyjn3MQYvJKWv7useXET27rUPdUJx3Y7DnoLS6ccP+DVUPGokxkqvBhV0\nK+L3wuS2IOIXo/q/I6t5jTBZu9FD44+f2+Zug+KfAC96ZyAwlsQZu2pNKSkUfSUU\nQSpyqNW0MxX6W7bLRp1Iv3l1QcWwMhYd8aQ7W4OByV3c0ZrdjrRiFR6IJcniX9Yo\nQgB3JHLC0sgWfA8wkggHBENTkS0vRy9m/LMJBIYpYQKBgQDtnKZkB58X32zTaNwv\nF9gQPA+0ZQlhBVcRxvUKAAP4MtsWqSAJxxAunqXN3aHMdlExhQxa+Ojz/IBOgnla\n+Z6nouqK4bUleHfqHtBsPBcSW5krneDp+VXXzkiHeg86eo/Il0UyJzUcMj9r6Nrq\nhODlANxLivyc3Tjx9lgYUCnV2QKBgQDm3PoB+fRnsX2GdZxCtAuXp3T0owk1YrEm\n2i4G6FBTQQ2U/wAgFrWQFDTcS9qmQ8RlMMM4sJdhSpZA/FFCD+D+VFFT1JCDTLQk\nFzlZDzCDNcxw2j6OavqIhbL0AMnQaAOBbeqBG48TXF3DHY8Q6CLQAeCJgmko/7pH\nEfuvOnMOBQKBgD0DCEaPHm7G0+7E8iS1fqlMgsvTVcEv2deaurjCoEJO7Kq46p0Y\nHDOcUP63diBGETsh6rZBS6+6mkeOgRK3HeH1lzvqENV6RXA8UVA5bcMs+fuVIndx\nqnUrZrxCvz5I0jqzfPUP1DdVP6u5stydGhIzUl2jZpHwy/p1QtGbY+KhAoGBANuE\nSwUOE2N11ZHAyzBx2oP9aqwjSmJE0YXL9/ABfj9rn+qI/imE7mdtj0XZqrJlxZ0i\nXL7Oy2JrdaudP/a8+odazWoUuMTgbnnSy2jvlIVu5lJ4cvsgpdCisKJrDXrpLEwp\no83X0pfqQA2DUcIVuORopIScSDn/z7Q8WUEpJrWxAoGALtMl2x0fJZYO1NSuwKwr\n6tE039nGh4gTy+Z67SH10j5Ih3DUvMik9yvEenzO0ERspqSqfbK+X8fbMLtk3Zvk\nRIV7e+oN4LlKgTxfWiS3KwGef35MhcJn2UAil/9jL0lG3XQ6vNBXVOsSDFBm2Urj\n0+aMRdDk+3AlkIy1hgKkXRM=\n-----END PRIVATE KEY-----\n",
"client_email": "sheets-access@loyal-weaver-341720.iam.gserviceaccount.com",
"client_id": "111494268122403059655",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/sheets-access%40loyal-weaver-341720.iam.gserviceaccount.com"
}
```
%% Cell type:code id: tags:
``` python
gc = gspread.service_account_from_dict(credentials)
sheet_id = "1vt5SnUuAojBLPug41dBs3_LKpyVM2rxrJw6MwAQ1_c0"
gsheet = gc.open_by_key(sheet_id)
worksheet_list = gsheet.worksheets()
```
%% Cell type:code id: tags:
``` python
for sheet in worksheet_list:
sheet_name = sheet.title
# if not check_date(sheet_name): # if attendance from different month
# continue
sheet_data = gsheet.worksheet(sheet_name).get_all_records()
print(sheet_data)
```
%% Output
[{'Name': 'Eric Morris', 'Age': 22, 'City': 'East Lansing', 'Hobby': 'Games'}, {'Name': 'Sai Ramesh', 'Age': 20, 'City': 'East Lansing', 'Hobby': 'Watching Basketball'}, {'Name': 'Pouria Khoushehchin', 'Age': 22, 'City': 'East Lansing', 'Hobby': 'Soccer'}, {'Name': 'Sam Kowalski', 'Age': 23, 'City': 'East Lansing', 'Hobby': 'watching Football'}]
%% Cell type:code id: tags:
``` python
import pandas as pd
dataframe = pd.DataFrame(gsheet.worksheet(sheet_name).get_all_records())
dataframe.loc[1, "Hobby"] = "Watching K-Dramas"
gsheet.worksheet(sheet_name).update([dataframe.columns.values.tolist()] + dataframe.values.tolist())
```
%% Output
{'spreadsheetId': '1vt5SnUuAojBLPug41dBs3_LKpyVM2rxrJw6MwAQ1_c0',
'updatedRange': 'Sheet1!A1:D5',
'updatedRows': 5,
'updatedColumns': 4,
'updatedCells': 20}
%% Cell type:markdown id: tags:
any other questions refer to https://docs.gspread.org/en/latest/user-guide.html
%% Cell type:code id: tags:
``` python
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment