-
Notifications
You must be signed in to change notification settings - Fork 0
/
tb.py
83 lines (69 loc) · 2.76 KB
/
tb.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import requests
from bs4 import BeautifulSoup
import pandas as pd
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import time
class TableBot:
def __init__(self, working_columns, start_column, table_name, page):
# TableBot info
self.working_columns = working_columns
self.start_row = start_column
self.table_name = table_name
self.page = page
# TableBot objects
self.sbh = None
self.sheet = None
self.worksheet = None
# Main func to add info to table
def addToTable(self, listInfo, rowId):
for key, value in listInfo.items():
if key == "Numbers":
array_of_numbers = listInfo["Numbers"]
for i in range(len(array_of_numbers)):
column_name = f'Number{i+1}'
self.add(rowId, self.working_columns[column_name], array_of_numbers[i])
else:
self.add(rowId, self.working_columns[key], value)
return "End"
# Max size
def getMaxSizeTable(self, column):
return len(self.worksheet.col_values(column))
# Getting of id
def getId(self, rowId):
return self.worksheet.cell(rowId, 3)
# Add elements to table
def add(self, rowId, col, value):
try:
self.worksheet.update([[str(value)]], col+str(rowId))
print(f"Updated {col+str(rowId)} with value: {value}")
except Exception as e:
print(f"Error updating cell: {e}")
# Set table
def setTable(self, name, page):
# GetScopes
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
try:
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
except:
return "credentials.json не був знайдений чи є неправильний"
# authorize the clientsheet
try:
self.sba = gspread.authorize(credentials)
except:
return "Неправильний файл credentials.json"
try:
self.sheet = self.sba.open(name)
except:
return "Таблиця не була знайдена чи ви не маєте доступу, також перевірте інтернет з'єднання."
try:
self.worksheet = self.sheet.worksheet(page)
except:
return "Аркуш в таблиці не був знайдений"
return None
def setStartRow(self, row):
self.start_row = row
def get_info_from_user(self, tableName, tablePage,startRow):
self.table_name = tableName
self.page = tablePage
self.start_row = startRow