-
Notifications
You must be signed in to change notification settings - Fork 11
/
googl.py
53 lines (41 loc) · 2.11 KB
/
googl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from typing import List
from google.oauth2 import service_account
from googleapiclient import discovery
from pandas import DataFrame
class Googl:
def __init__(self, service_account_file: str, scopes: List[str]):
self.service_account_file = service_account_file
self.scopes = scopes
self.google_sheet_type = "application/vnd.google-apps.spreadsheet"
self.creds = service_account.Credentials.from_service_account_file(self.service_account_file,
scopes=self.scopes)
self.drive = discovery.build("drive", "v3", credentials=self.creds)
self.sheets = discovery.build("sheets", "v4", credentials=self.creds)
def get_folder_id(self, folder_name: str) -> str:
folders: dict = self.drive.files().list(q="mimeType='application/vnd.google-apps.folder'").execute()
print(folders)
folder_id = [x.get("id") for x in folders.get("files") if x.get("name") == folder_name]
return folder_id[0]
def create_new_sheet(self, file_name: str, parent_folder_id: str) -> str:
new_sheet_metadata = {
"name": file_name,
"parents": [parent_folder_id],
"mimeType": self.google_sheet_type
}
new_sheet = self.drive.files().create(body=new_sheet_metadata).execute()
print(new_sheet)
return new_sheet.get("id")
def insert_df_to_sheet(self, google_sheet_id: str, df: DataFrame) -> dict:
response = self.sheets.spreadsheets().values().append(
spreadsheetId=google_sheet_id,
valueInputOption="RAW",
range="A1",
body={"majorDimension": "ROWS",
"values": df.T.reset_index().T.values.tolist()}
).execute()
return response
def get_sheet_link(self, google_sheet_id: str,
return_all_fields: bool = False, fields_to_return: str = "webViewLink"):
fields = "*" if return_all_fields else fields_to_return
response = self.drive.files().get(fileId=google_sheet_id, fields=fields).execute()
return response