Fix GSheet column width

Signed-off-by: Emmy D'Anello <emmy.danello@animath.fr>
This commit is contained in:
Emmy D'Anello 2024-07-06 09:57:00 +02:00
parent 5c3b3d26c8
commit 9721898731
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
1 changed files with 21 additions and 19 deletions

View File

@ -575,7 +575,7 @@ class Tournament(models.Model):
format_requests = []
# Set the width of the columns
column_widths = [("A", 300), ("B", 150), ("C", 150), ("D", 150), ("E", 150), ("F", 150), ("G", 150),
column_widths = [("A", 350), ("B", 150), ("C", 150), ("D", 150), ("E", 150), ("F", 150), ("G", 150),
("H", 150), ("I", 150), ("J", 150)]
for column, width in column_widths:
grid_range = a1_range_to_grid_range(column, worksheet.id)
@ -640,7 +640,7 @@ class Tournament(models.Model):
(f"A{participations.count() + 5}:C{2 * participations.count() + 4}", (0.9, 0.9, 0.9)),]
if settings.NB_ROUNDS >= 3:
bg_colors.append((f"F2:G{participations.count() + 1}", (0.9, 0.9, 0.9)))
bg_colors.append((f"H2:I{participations.count() + 1}", (0.9, 0.9, 0.9)))
bg_colors.append((f"I2:J{participations.count() + 1}", (0.9, 0.9, 0.9)))
else:
bg_colors.append((f"F2:G{participations.count() + 1}", (0.9, 0.9, 0.9)))
for bg_range, bg_color in bg_colors:
@ -1214,6 +1214,11 @@ class Pool(models.Model):
def update_spreadsheet(self): # noqa: C901
translation.activate(settings.PREFERRED_LANGUAGE_CODE)
pool_size = self.participations.count()
has_observer = settings.TFJM_APP == "ETEAM" and pool_size >= 4
passage_width = 6 + (2 if has_observer else 0)
passages = self.passages.all()
# Create tournament sheet if it does not exist
self.tournament.create_spreadsheet()
@ -1221,17 +1226,13 @@ class Pool(models.Model):
spreadsheet = gc.open_by_key(self.tournament.notes_sheet_id)
worksheets = spreadsheet.worksheets()
if f"{_('Pool')} {self.short_name}" not in [ws.title for ws in worksheets]:
worksheet = spreadsheet.add_worksheet(f"{_('Pool')} {self.short_name}", 100, 34)
worksheet = spreadsheet.add_worksheet(f"{_('Pool')} {self.short_name}",
30, 2 + pool_size * passage_width)
else:
worksheet = spreadsheet.worksheet(f"{_('Pool')} {self.short_name}")
if any(ws.title == "Sheet1" for ws in worksheets):
spreadsheet.del_worksheet(spreadsheet.worksheet("Sheet1"))
pool_size = self.participations.count()
has_observer = settings.TFJM_APP == "ETEAM" and pool_size >= 4
passage_width = 6 + (2 if has_observer else 0)
passages = self.passages.all()
header = [
sum(([str(_("Problem #{problem}").format(problem=passage.solution_number))] + (passage_width - 1) * [""]
for passage in passages), start=[str(_("Problem")), ""]),
@ -1275,7 +1276,7 @@ class Pool(models.Model):
for passage in passages),
start=[str(_("Coefficient")), ""])
subtotal = [str(_("Subtotal")), ""]
footer = [average, coeffs, subtotal, 34 * [""]]
footer = [average, coeffs, subtotal, (2 + pool_size * passage_width) * [""]]
min_row = 5
max_row = min_row + self.juries.count()
@ -1354,8 +1355,9 @@ class Pool(models.Model):
all_values = header + notes + footer + ranking
worksheet.batch_clear([f"A1:AH{max_row + 5 + pool_size}"])
worksheet.update(all_values, "A1:AH", raw=False)
max_col = getcol(2 + pool_size * passage_width)
worksheet.batch_clear([f"A1:{max_col}{max_row + 5 + pool_size}"])
worksheet.update(all_values, f"A1:{max_col}", raw=False)
format_requests = []
@ -1386,13 +1388,13 @@ class Pool(models.Model):
for i in range(pool_size + 1):
merge_cells.append(f"A{max_row + 5 + i}:B{max_row + 5 + i}")
format_requests.append({"unmergeCells": {"range": a1_range_to_grid_range("A1:AH", worksheet.id)}})
format_requests.append({"unmergeCells": {"range": a1_range_to_grid_range(f"A1:{max_col}", worksheet.id)}})
for name in merge_cells:
grid_range = a1_range_to_grid_range(name, worksheet.id)
format_requests.append({"mergeCells": {"mergeType": MergeType.merge_all, "range": grid_range}})
# Make titles bold
bold_ranges = [("A1:AH", False), ("A1:AH3", True),
bold_ranges = [(f"A1:{max_col}", False), (f"A1:{max_col}3", True),
(f"A{max_row + 1}:B{max_row + 3}", True), (f"A{max_row + 5}:E{max_row + 5}", True)]
for bold_range, bold in bold_ranges:
format_requests.append({
@ -1404,7 +1406,7 @@ class Pool(models.Model):
})
# Set background color for headers and footers
bg_colors = [("A1:AH", (1, 1, 1)),
bg_colors = [(f"A1:{max_col}", (1, 1, 1)),
(f"A1:{getcol(2 + passages.count() * passage_width)}3", (0.8, 0.8, 0.8)),
(f"A{min_row - 1}:B{max_row}", (0.95, 0.95, 0.95)),
(f"A{max_row + 1}:B{max_row + 3}", (0.8, 0.8, 0.8)),
@ -1439,10 +1441,10 @@ class Pool(models.Model):
})
# Set the width of the columns
column_widths = [("A", 300), ("B", 30)]
column_widths = [("A", 350), ("B", 30)]
for passage in passages:
column_widths.append((f"{getcol(3 + passage_width * (passage.position - 1))}"
f":{getcol(8 + passage_width * (passage.position - 1))}", 75))
f":{getcol(2 + passage_width * passage.position)}", 80))
for column, width in column_widths:
grid_range = a1_range_to_grid_range(column, worksheet.id)
format_requests.append({
@ -1493,7 +1495,7 @@ class Pool(models.Model):
})
# Define borders
border_ranges = [("A1:AH", "0000"),
border_ranges = [(f"A1:{max_col}", "0000"),
(f"A1:{getcol(2 + passages.count() * passage_width)}{max_row + 3}", "1111"),
(f"A{max_row + 5}:E{max_row + pool_size + 5}", "1111"),
(f"A1:B{max_row + 3}", "1113"),
@ -1568,9 +1570,9 @@ class Pool(models.Model):
})
# Protect the header, the juries list, the footer and the ranking
protected_ranges = ["A1:AH4",
protected_ranges = [f"A1:{max_col}4",
f"A{min_row}:B{max_row}",
f"A{max_row}:AH{max_row + 5 + pool_size}"]
f"A{max_row}:{max_col}{max_row + 5 + pool_size}"]
for protected_range in protected_ranges:
format_requests.append({
"addProtectedRange": {