-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgooglesheet_inserter.py
110 lines (102 loc) · 5.1 KB
/
googlesheet_inserter.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import pygsheets
from typing import Dict, List, Union, Any
class GoogleSheetInserter:
"""Класс для добавления записей в Google Sheet."""
def __init__(
self, credence_service_file:str = "", googlesheet_file_url:str = ""
) -> None:
"""Инициализирует класс.
Args:
credence_service_file (str): Путь до сервисного файла credence.json (Google Sheet API).
googlesheet_file_url (str): Ссылка на Google Sheet.
Returns:
"""
self.credence_service_file = credence_service_file
self.googlesheet_file_url = googlesheet_file_url
def _get_googlesheet_by_url(
self, googlesheet_client: pygsheets.client.Client
) -> pygsheets.Spreadsheet:
"""Получает Google.Docs таблицу по ссылке на документ."""
sheets: pygsheets.Spreadsheet = googlesheet_client.open_by_url(
self.googlesheet_file_url
)
return sheets.sheet1
def _insert_data_back_googlesheet(
self,
sheet: pygsheets.Spreadsheet,
data: List[List[Union[str, bool]]],
start_col: str,
end_col: str,
) -> bool:
"""Находит номер последней заполненной строки и вставляет данные после нее.
Args:
sheet (pygsheets.Spreadsheet): - Объект электронной таблицы Google.Docs.
data (List[List[Union[str, bool]]]): - Cловарь с данными, предназначенными для вставки.
start_col (str): - столбец таблицы, с которого начинается вставка.
end_col (str): - столбец таблицы, на котором заканчивается вставка.
Returns: - True в случае успешной вставки и False в противном случае.
"""
last_filled_row: List[str] = len(
list(filter(lambda cell: cell != "", sheet.get_col(1)))
) # column 1 in googlesheet excel = A-column
try:
sheet.update_values(
f"{start_col}{last_filled_row + 1}:{end_col}{last_filled_row + 1}", data
)
except:
return False
else:
return True
def _get_googlesheet_client(self) -> pygsheets.client.Client:
"""Он авторизуется с помощью служебного ключа и возвращает объект клиента Google Docs."""
return pygsheets.authorize(
service_file=self.credence_service_file
)
def _insert_info_in_googlesheet(
self,
data: List[List[Union[str, bool]]],
start_col: str = "A",
end_col: str = "E",
) -> bool:
"""Вставляет строку данных в таблицу Google.
Args:
data (List[List[Union[str, bool]]]): Cловарь с данными, предназначенными для вставки.
start_col (str): Cтолбец таблицы, с которого начинается вставка.
end_col (str): Cтолбец таблицы, с которого заканчивается вставка.
Returns: True в случае успешной вставки и False в противном случае.
"""
googlesheet_client: pygsheets.client.Client = self._get_googlesheet_client()
wks: pygsheets.Spreadsheet = self._get_googlesheet_by_url(googlesheet_client)
is_inserted: bool = self._insert_data_back_googlesheet(
wks, data, start_col, end_col
)
return is_inserted
def write_information_in_googlesheet(
self,
primary_col: str,
user_poll_data: Dict[str, Union[str, bool, Any]],
current_date: str,
) -> bool:
"""Записывает данные в электронную таблицу Google.
Args:
primary_col (str): Значение, идентифицирующее строку данных.
user_poll_data (Dict[str, Union[str, bool, Any]]): Данные для записи.
current_date (str): Дата записи.
Returns:
"""
print("Результаты опроса в обработчике date_handler: ", user_poll_data)
print("ФИО пользователя, прошедшего опрос:", primary_col)
return self._insert_info_in_googlesheet(
[
[
primary_col,
user_poll_data["diagnosis"],
user_poll_data["medical_certificate_data"],
user_poll_data["date_of_last_class_attendance"],
user_poll_data["medical_certificate"],
current_date,
]
],
start_col="A",
end_col="F",
)