diff --git a/Scripts/API/Google-spreadsheet-share-and-retrieve/README.md b/Scripts/API/Google-spreadsheet-share-and-retrieve/README.md new file mode 100644 index 000000000..732e3b6f1 --- /dev/null +++ b/Scripts/API/Google-spreadsheet-share-and-retrieve/README.md @@ -0,0 +1,86 @@ +# Google Spreadsheet share and retrieve. + +This script helps the user to upload a local csv file to google drive and share it with the user specified in the command. + +The script also helps to retrieve the spreadsheet form the drive and store it locally in json format. + +*** + +### Prerequisites +``` +pip install -r requirements.txt +``` + +*** + +### How to run the script + +1. #### Create Google Sheets API credentials. + + a. Go to [Google Developer's Console](https://console.developers.google.com/apis/dashboard?project=trans-crawler-261018). + + b. Go to 'Library' from the left navigation panel. + + c. In the search bar type 'Google Sheets API'. + + d. Enable the API if not enabled or click on the manage button. + + e. Similarly enable the 'Google Drive API'. + + f. On the API page, goto 'Credentials' from the Navigation Panel. + + g. Click on 'Create Credentials', select the service account option and name the service account. + + h. Click on 'Add key' and 'Create New'. + + ![Google-sheets-api](outputs/google-sheets-credentials.JPG) + + i. File download will begin, rename this file as 'credentials.json' and place in the same folder as the scripts. + +2. #### Run the Scripts. + + * Share a spreadsheet + + ``` + python create_sheet.py -mail -csv -s + ``` + + * Retrieve the spreadsheet and store locally in json format + + ``` + python get_sheet.py -j -s + ``` + +*** + +### Screenshot showing the sample use of the script + +1. Creating a google spreadsheet and sharing it with the receiver. + + ``` + python create_sheet.py -mail -csv sample.csv -s my_sample_spreadsheet + ``` + + The receiver will receive a mail like this. + + ![successfully-shared](outputs/gmail-noti.JPG) + +2. Retrieving a created spreadhsheet. + + ``` + python get_sheet.py -j sample.json -s my_sample_spreadsheet + ``` + + Check the sample.json for observing the output file generated. + +*** +### Caveats + +* Since this is a free access version for the Google Spreadsheet API, keep the size of the csv file low. + +* If you get an error such as 'resource limit exhausted', simply create a new key as mention in the step 1.h in the 'How to run the script section'. and replace your old credentials.json file with this new one. + +*** + +## Author Name +Priya Mane \ No newline at end of file diff --git a/Scripts/API/Google-spreadsheet-share-and-retrieve/create_sheet.py b/Scripts/API/Google-spreadsheet-share-and-retrieve/create_sheet.py new file mode 100644 index 000000000..b35576aa6 --- /dev/null +++ b/Scripts/API/Google-spreadsheet-share-and-retrieve/create_sheet.py @@ -0,0 +1,75 @@ +import gspread +from oauth2client.service_account import ServiceAccountCredentials +import pandas as pd +import argparse + + +def auth_user(): + # Define a authorized user with the credentials created. + scope = ['https://www.googleapis.com/auth/spreadsheets', + 'https://www.googleapis.com/auth/drive'] + + credentials = ServiceAccountCredentials.from_json_keyfile_name( + 'credentials.json', scope) + + client = gspread.authorize(credentials) + return client + + +def create_and_share_sheet(user_mail, spreadsheet_name, csv_file): + client = auth_user() + sh = client.create(spreadsheet_name) + worksheet = sh.worksheet("Sheet1") + + # Read from the csv file mentioned in the command. + df = pd.read_csv(csv_file) + col = df.columns + # Define the column headings based on the our csv file. + end = ord('A') + len(col) - 1 + cell_range = 'A1:' + chr(end) + '1' + + # Define cells + cell_list = worksheet.range(cell_range) + i = 0 + for cell in cell_list: + cell.value = col[i] + i += 1 + + # Write these column headings to the worksheet + worksheet.update_cells(cell_list) + + # Convert rest of the dataframe to numpy object. (Use pandas version 1.0.3 strictly for this to work!) + df = df.to_numpy().tolist() + + # Write data from numpy object to the worksheet + for i in range(2, len(df) + 2): + pos = 'A' + str(i) + ':' + chr(end) + str(i) + cell_list = worksheet.range(pos) + val = df[i-2] + j = 0 + for cell in cell_list: + cell.value = val[j] + j += 1 + worksheet.update_cells(cell_list) + + # Share the created spreadsheet with the receiver. + sh.share(user_mail, perm_type='user', role='writer') + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description="generate and share google sheet for give csv") + parser.add_argument("-mail", help="Enter the email id of the community admin", + dest="mail_id", type=str, required=True) + parser.add_argument("-csv", help="Enter path of csv file", + dest="csv", type=str, required=True) + parser.add_argument("-spreadsheet_name", help="Enter name of spreadsheet", + dest="ss_name", type=str, required=True) + + args = parser.parse_args() + + community_admin = args.mail_id + csv_file = args.csv + spreadsheet_name = args.ss_name + + create_and_share_sheet(community_admin, spreadsheet_name, csv_file) diff --git a/Scripts/API/Google-spreadsheet-share-and-retrieve/credentials.json b/Scripts/API/Google-spreadsheet-share-and-retrieve/credentials.json new file mode 100644 index 000000000..d055314b1 --- /dev/null +++ b/Scripts/API/Google-spreadsheet-share-and-retrieve/credentials.json @@ -0,0 +1,12 @@ +{ + "type": "service_account", + "project_id": "trans-crawler-261018", + "private_key_id": , + "private_key": "", + "client_email": "", + "client_id": "", + "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": "" +} diff --git a/Scripts/API/Google-spreadsheet-share-and-retrieve/get_sheet.py b/Scripts/API/Google-spreadsheet-share-and-retrieve/get_sheet.py new file mode 100644 index 000000000..7e4892e60 --- /dev/null +++ b/Scripts/API/Google-spreadsheet-share-and-retrieve/get_sheet.py @@ -0,0 +1,44 @@ +import gspread +from oauth2client.service_account import ServiceAccountCredentials +import pandas as pd +import argparse +import json + + +def auth_user(): + # Define a authorized user with the credentials created. + scope = ['https://www.googleapis.com/auth/spreadsheets', + 'https://www.googleapis.com/auth/drive'] + + credentials = ServiceAccountCredentials.from_json_keyfile_name( + 'credentials.json', scope) + + client = gspread.authorize(credentials) + return client + + +def get_sheet_json(spreadsheet_name, json_file): + # Retrieve spreadsheet by name from the user's google drive. + client = auth_user() + sheet = client.open(spreadsheet_name).sheet1 + data = sheet.get_all_records() + + # Dump the retrieved csv file in json format. + with open(json_file, 'w') as fout: + json.dump(data, fout, indent=4) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description="get data from spreadsheet in json format") + parser.add_argument("-json", help="Enter path of json file", + dest="json", type=str, required=True) + parser.add_argument("-spreadsheet_name", help="Enter name of spreadsheet", + dest="ss_name", type=str, required=True) + + args = parser.parse_args() + + spreadsheet_name = args.ss_name + json_file = args.json + + get_sheet_json(spreadsheet_name, json_file) diff --git a/Scripts/API/Google-spreadsheet-share-and-retrieve/outputs/gmail-noti.JPG b/Scripts/API/Google-spreadsheet-share-and-retrieve/outputs/gmail-noti.JPG new file mode 100644 index 000000000..7ab94ac22 Binary files /dev/null and b/Scripts/API/Google-spreadsheet-share-and-retrieve/outputs/gmail-noti.JPG differ diff --git a/Scripts/API/Google-spreadsheet-share-and-retrieve/outputs/google-sheets-credentials.JPG b/Scripts/API/Google-spreadsheet-share-and-retrieve/outputs/google-sheets-credentials.JPG new file mode 100644 index 000000000..474ba3494 Binary files /dev/null and b/Scripts/API/Google-spreadsheet-share-and-retrieve/outputs/google-sheets-credentials.JPG differ diff --git a/Scripts/API/Google-spreadsheet-share-and-retrieve/requirement.txt b/Scripts/API/Google-spreadsheet-share-and-retrieve/requirement.txt new file mode 100644 index 000000000..bdf9c6fa3 --- /dev/null +++ b/Scripts/API/Google-spreadsheet-share-and-retrieve/requirement.txt @@ -0,0 +1,4 @@ +gspread==3.6.0 +oauth2client==4.1.3 +pandas==1.0.3 +argparse \ No newline at end of file diff --git a/Scripts/API/Google-spreadsheet-share-and-retrieve/sample.csv b/Scripts/API/Google-spreadsheet-share-and-retrieve/sample.csv new file mode 100644 index 000000000..e02c57d9c --- /dev/null +++ b/Scripts/API/Google-spreadsheet-share-and-retrieve/sample.csv @@ -0,0 +1,4 @@ +name,ingredients,diet,prep_time,cook_time,flavor_profile,course,state,region +Balu shahi,"Maida flour, yogurt, oil, sugar",vegetarian,45,25,sweet,dessert,West Bengal,East +Boondi,"Gram flour, ghee, sugar",vegetarian,80,30,sweet,dessert,Rajasthan,West +Gajar ka halwa,"Carrots, milk, sugar, ghee, cashews, raisins",vegetarian,15,60,sweet,dessert,Punjab,North diff --git a/Scripts/API/Google-spreadsheet-share-and-retrieve/sample.json b/Scripts/API/Google-spreadsheet-share-and-retrieve/sample.json new file mode 100644 index 000000000..2aefa252b --- /dev/null +++ b/Scripts/API/Google-spreadsheet-share-and-retrieve/sample.json @@ -0,0 +1,35 @@ +[ + { + "name": "Balu shahi", + "ingredients": "Maida flour, yogurt, oil, sugar", + "diet": "vegetarian", + "prep_time": 45, + "cook_time": 25, + "flavor_profile": "sweet", + "course": "dessert", + "state": "West Bengal", + "region": "East" + }, + { + "name": "Boondi", + "ingredients": "Gram flour, ghee, sugar", + "diet": "vegetarian", + "prep_time": 80, + "cook_time": 30, + "flavor_profile": "sweet", + "course": "dessert", + "state": "Rajasthan", + "region": "West" + }, + { + "name": "Gajar ka halwa", + "ingredients": "Carrots, milk, sugar, ghee, cashews, raisins", + "diet": "vegetarian", + "prep_time": 15, + "cook_time": 60, + "flavor_profile": "sweet", + "course": "dessert", + "state": "Punjab", + "region": "North" + } +] \ No newline at end of file