-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting.py
More file actions
44 lines (31 loc) · 1.39 KB
/
Copy pathsorting.py
File metadata and controls
44 lines (31 loc) · 1.39 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
import gspread
import logging
from config import GoogleSheetsApiAuth
class CellSorter:
@staticmethod
def compare_ref_column(ref_column: list[str]):
""" Compare original and sorted ref_column """
sorted_ref_column = sorted(ref_column)
for original, sorted_item in zip(ref_column, sorted_ref_column):
if original != sorted_item:
logging.error(f"[ERROR] {original}\t{sorted_item}")
@staticmethod
def sort_data(spreadsheet):
""" Sort table by (ref_column) """
data, ref_column, type_column = GoogleSheetsApiAuth.get_sheet(spreadsheet)
if ref_column != sorted(ref_column):
""" Check if ref_column is sorted in ascending order """
# Compare original and sorted ref_column
CellSorter.compare_ref_column(ref_column)
# Ref Row
ref_column = 1
# Calculating the number of cells in a table
total_rows = spreadsheet.row_count
total_cols = spreadsheet.col_count
# Find the A1 notation of the last cell
last_cell_a1 = gspread.utils.rowcol_to_a1(total_rows, total_cols)
# Sort from A2 to the last cell
spreadsheet.sort((ref_column, 'asc'), range=f'A2:{last_cell_a1}')
logging.info("Data is sorted successfully!")
else:
logging.info("Data is already sorted!")