Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Added Google-spreadsheet-share-and-retrieve script #609

Merged
merged 1 commit into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions Scripts/API/Google-spreadsheet-share-and-retrieve/README.md
Original file line number Diff line number Diff line change
@@ -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 <mail_id> -csv <csv_file_name> -s <spreadsheet_name>
```

* Retrieve the spreadsheet and store locally in json format

```
python get_sheet.py -j <json_file_name> -s <spreadsheet_name>
```

***

### Screenshot showing the sample use of the script

1. Creating a google spreadsheet and sharing it with the receiver.

```
python create_sheet.py -mail <receiver-mail_id> -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
75 changes: 75 additions & 0 deletions Scripts/API/Google-spreadsheet-share-and-retrieve/create_sheet.py
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions Scripts/API/Google-spreadsheet-share-and-retrieve/credentials.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"type": "service_account",
"project_id": "trans-crawler-261018",
"private_key_id": <your_private_key> ,
"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": ""
}
44 changes: 44 additions & 0 deletions Scripts/API/Google-spreadsheet-share-and-retrieve/get_sheet.py
Original file line number Diff line number Diff line change
@@ -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)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
gspread==3.6.0
oauth2client==4.1.3
pandas==1.0.3
argparse
4 changes: 4 additions & 0 deletions Scripts/API/Google-spreadsheet-share-and-retrieve/sample.csv
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions Scripts/API/Google-spreadsheet-share-and-retrieve/sample.json
Original file line number Diff line number Diff line change
@@ -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"
}
]