Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
DataTools_Tutorial_Demo
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
vuppalaa
DataTools_Tutorial_Demo
Commits
b1038bfd
Commit
b1038bfd
authored
2 years ago
by
qiuolivi
Browse files
Options
Downloads
Patches
Plain Diff
Changed Step 3
parent
0788d5fc
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
GoogleSheetsTutorial.ipynb
+9
-9
9 additions, 9 deletions
GoogleSheetsTutorial.ipynb
with
9 additions
and
9 deletions
GoogleSheetsTutorial.ipynb
+
9
−
9
View file @
b1038bfd
...
...
@@ -24,6 +24,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "yTqEB1KKhshX"
...
...
@@ -34,15 +35,14 @@
" (you must use your personal email not your MSU email)\n",
"2. Inside that project enable the Google sheets API and Google Drive for your new project\n",
" 1. This is found by using the search bar to search for Google API and Google Drive\n",
"3. Create a Google service account through the Google developer portal\n",
"4. Go to “APIs & Services > Credentials” and choose “Create credentials > Service account key”.\n",
"5. Fill out the form (making sure to add editor privledges to the service account)\n",
"6. Click “Create” and “Done”.\n",
"7. Press “Manage service accounts” above Service Accounts.\n",
"8. Press on ⋮ near recently created service account and select “Manage keys” and then click on “ADD KEY > Create new key”.\n",
"9. Select JSON key type and press “Create”.\n",
"10. In your python file import gspread\n",
"11. Copy Json key into python file as a dictionary\n",
"3. Go to “APIs & Services > Credentials” and choose “Create credentials > Service Account\".\n",
"4. Fill out the form (making sure to add editor privledges to the service account)\n",
"5. Click “Create” and “Done”.\n",
"6. Press “Manage service accounts” above Service Accounts.\n",
"7. Press on ⋮ near recently created service account and select “Manage keys” and then click on “ADD KEY > Create new key”.\n",
"8. Select JSON key type and press “Create”.\n",
"9. In your python file import gspread\n",
"10. Copy Json key into python file as a dictionary\n",
"\n",
"\n",
"for more information/ documentation on gspread head here\n",
...
...
%% 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-----
\n
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDWR9gprOoJZUw7
\n
VIa7qVxjXUEBCZb9Jd912Cr/rrFQ6b8Pk7vQl3tx8jUJxOC6/V9eSc7qdKZzED8I
\n
XR0YTWradtu4Kd7AzMwZB+zoBCNFJyVNyLyZCmOKFpsnIDNElIsQ2uRg2jqQbFyS
\n
yYD0Y97oRAbitJyemJ2SfUfYd+Qj2Sp3mFgsZQaufHR3vZIDWxR7C3feu74nfWud
\n
JAPUMf3Uku/kl/MHniZo3f5zXAnQXwMC39MSYxu7N9UNxu0s0Y+AwmbsLjkMJ2JB
\n
gOq7ki+HPIwsy+GN+nVcHpXlwKGs9irDV2c5rQhcL1Yiw4xCVRJwGS0Hp39z73tk
\n
fHEU/gs9AgMBAAECggEADLcss6RoKvCQ6oBkkmRFcDwLbHVWOWVy8jpGqu+B5CxK
\n
wzDgF0u17pgR36eIMnBwpVa56Z3Mds2DAAsOe++0JIyjEzWg204L2nxm6Ym9UhA1
\n
nX0I/Afqyjn3MQYvJKWv7useXET27rUPdUJx3Y7DnoLS6ccP+DVUPGokxkqvBhV0
\n
K+L3wuS2IOIXo/q/I6t5jTBZu9FD44+f2+Zug+KfAC96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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
```
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment