diff --git a/GoogleSheetsTutorial1.ipynb b/GoogleSheetsTutorial1.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..c9d2d6e51b5d8a1175337a99648a179f9a862239 --- /dev/null +++ b/GoogleSheetsTutorial1.ipynb @@ -0,0 +1,74 @@ +# Google Sheets API Tutorial + +--- + + + +--- + + + +### How to Get Started + +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 + + +for more information/ documentation on gspread head here +https://docs.gspread.org/en/latest/oauth2.html + +!pip install gspread + +import gspread +import json + +## Testing out the API + +credentials = { + "type": "service_account", + "project_id": "d2l-instructor-api", + "private_key_id": "deb126d3a82a7dc84fea187d04cd37e871b5e6fc", + "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDUsc6loi+P4gzn\nkGdemmh3Mg1GIOIHnfbqDI8rZebakwuHDMqGp9cmEk1rP4NehR0PXRoE5j7DZs7z\n3gzTMbV75q1JmL6gRyCl0FQvwTCDEN5lst1NodjH6Pg1j61fpszLsNHe4EVyvaXH\nrTbwJvLARQlP+TQyGaTZiK8IXF3H0tOA/opOtuQEZQt9oIeae1TTM2mcUAg6MhAL\nL2/PkleLXqYdKJT6OhZyJgWwljyxg/0llxqz+yiEmJrkbn6r9taP2GzAFgqVpacN\nFF9idd+SCzgQ2eC6evAkdUibC6xH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Aqb6hP+0Y\nUeMpuB4H6Zs4+HWU+98aqTFBi2XLDZLTaixjPZelAoGBAM+mmJ9Cvnng2z5o8qTq\nMHf5ZRJl86PWQ/RxK/2r/Z8oGzm64O/5QD92YUlVfxA18rj/6pvz9rgX/tO9P7ec\n9xU+xVomRGMehiPdzUwwb0ZVqVnKgTIhACr2HNj2WuBcdcb6sqoYgBmAN1bjLu5n\n3fdTrl8aAH8TUW3uu2Rdeqql\n-----END PRIVATE KEY-----\n", + "client_email": "d2lapi@d2l-instructor-api.iam.gserviceaccount.com", + "client_id": "109123177444430472879", + "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/d2lapi%40d2l-instructor-api.iam.gserviceaccount.com" +} + +gc = gspread.service_account_from_dict(credentials) +sheet_id = "1vt5SnUuAojBLPug41dBs3_LKpyVM2rxrJw6MwAQ1_c0" +gsheet = gc.open_by_key(sheet_id) +worksheet_list = gsheet.worksheets() + + + +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) + +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()) + + + any other questions refer to https://docs.gspread.org/en/latest/user-guide.html +