-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatting.py
More file actions
125 lines (106 loc) · 4.72 KB
/
Copy pathformatting.py
File metadata and controls
125 lines (106 loc) · 4.72 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import gspread
# Call the function to color the first row
blue_color = {"red": 74, "green": 134, "blue": 232}
# Color for painting cells
colors = {
"0": {"red": 201, "green": 218, "blue": 248},
"2": {"red": 183, "green": 225, "blue": 205},
"3": {"red": 87, "green": 187, "blue": 138},
"13": {"red": 217, "green": 234, "blue": 211},
"23": {"red": 234, "green": 209, "blue": 220},
}
class CellFormatter:
""" Format cells bg-color """
@staticmethod
def rgb_to_color(rgb: dict[str, int]) -> dict[str, float]:
""" Converts RGB values to color format accepted by Google Sheets API """
r = rgb.get("red") / 255
g = rgb.get("green") / 255
b = rgb.get("blue") / 255
return {"red": r, "green": g, "blue": b}
@staticmethod
def color_first_row(service, result_sheet: gspread.Worksheet) -> None:
""" Color the entire first row of the worksheet with the specified color """
batch_request = {
"repeatCell": {
"range": {
"sheetId": result_sheet.id,
"startRowIndex": 0,
"endRowIndex": 1,
"startColumnIndex": 0,
"endColumnIndex": result_sheet.row_count
},
"cell": {
"userEnteredFormat": {
"backgroundColor": CellFormatter.rgb_to_color(blue_color),
"horizontalAlignment": "CENTER",
"textFormat": {
"foregroundColor": {"red": 1.0, "green": 1.0, "blue": 1.0},
"fontSize": 11,
"bold": True
}
}
},
"fields": "userEnteredFormat(backgroundColor,textFormat,horizontalAlignment)"
}
}
# Send the request to update the color of the first row
service.spreadsheets().batchUpdate(
spreadsheetId=result_sheet.spreadsheet.id,
body={"requests": [batch_request]}
).execute()
@staticmethod
def color_by_type(service, ref_column: list[str], type_column: list[str], result_sheet: gspread.Worksheet) -> None:
""" Paint the cells background by Type """
batch_requests = []
# GET "Result" page ID
sheet_id = result_sheet.id
print()
# Iterate по ref_column | type_column
for ref, type_value in zip(ref_column, type_column):
type_value_str = str(type_value)
# Check if a color exist for this Type
if type_value_str in colors:
# GET color correspond to Type
color = CellFormatter.rgb_to_color(colors[type_value_str])
# GET index line, which need to color
ref_rows_to_color = [i for i, ref_value in enumerate(ref_column, start=1) if ref_value == ref]
for row in ref_rows_to_color:
row_index = row + 1
request = {
"repeatCell": {
"range": {
"sheetId": sheet_id,
"startRowIndex": row_index - 1,
"endRowIndex": row_index,
"startColumnIndex": 0,
"endColumnIndex": 1
},
"cell": {
"userEnteredFormat": {
"backgroundColor": color,
"horizontalAlignment": "CENTER",
}
},
"fields": "userEnteredFormat(backgroundColor, horizontalAlignment)"
}
}
batch_requests.append(request)
# Sent all requests with updated colors in one batch
(service.spreadsheets().batchUpdate(
spreadsheetId=result_sheet.spreadsheet.id,
body={"requests": batch_requests}
).execute())
@staticmethod
def format_center_data(service, sheet_id: str, sheet):
""" Format the cells value in center """
result_sheet_id = sheet._properties['sheetId'] # Get the sheet ID
requests = [{
"repeatCell": {
"range": {"sheetId": result_sheet_id},
"cell": {"userEnteredFormat": {"horizontalAlignment": "CENTER"}},
"fields": "userEnteredFormat.horizontalAlignment"
}
}]
# Sent all requests with updated colors in one batch
service.spreadsheets().batchUpdate(spreadsheetId=sheet_id, body={"requests": requests}).execute()