-
Notifications
You must be signed in to change notification settings - Fork 0
/
gsheet.py
51 lines (36 loc) · 1.91 KB
/
gsheet.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
from __future__ import print_function
import pandas as pd
from googleapiclient.discovery import build
from config import CONFIG
from google_authentication import GOOGLE_CREDENTIALS
# The ID and range of a sample spreadsheet.
SPREADSHEET_ID = CONFIG['spreadsheet']['id']
SHEET_NAME = CONFIG['spreadsheet']['sheet_name']
class Sheets:
def __init__(self, sheet_id):
self.sheet_id = sheet_id
self.creds = GOOGLE_CREDENTIALS
self.service = build('sheets', 'v4', credentials=self.creds)
def get_available_sheet_info(self):
sheet_metadata = self.service.spreadsheets().get(spreadsheetId=self.sheet_id).execute()
properties = sheet_metadata.get('sheets')
for item in properties:
sheet_id = (item.get("properties").get('sheetId'))
print(item)
def get_sheet_names(self):
sheet_metadata = self.service.spreadsheets().get(spreadsheetId=self.sheet_id).execute()
properties = sheet_metadata.get('sheets')
return [prop['properties']['title'] for prop in properties]
def get_dataframe(self, sheet_name: str,
colnames_range: str = "A1:AZ",
values_range: str = "A2:AZ"):
column_names = self.service.spreadsheets().values().get(spreadsheetId=self.sheet_id,
range=f"'{sheet_name}'!{colnames_range}").execute()
values = self.service.spreadsheets().values().get(spreadsheetId=self.sheet_id,
range=f"'{sheet_name}'!{values_range}").execute()
return pd.DataFrame(values['values'], columns=column_names['values'][0])
if __name__ == '__main__':
sheets = Sheets(SPREADSHEET_ID)
# sheets.get_available_sheet_info()
sheet_metadata = sheets.service.spreadsheets().get(spreadsheetId=sheets.sheet_id).execute()
properties = sheet_metadata.get('sheets')