Fix formula updates for all columns in rows 25-27 of Graphics sheet
This commit is contained in:
@@ -167,21 +167,79 @@ def update_formulas_in_workbook(workbook, sheet_name_mapping):
|
||||
|
||||
# Special handling for Graphics sheet rows 25-27
|
||||
if sheet_name == "Graphics":
|
||||
# Directly access cells in rows 25-27
|
||||
# We'll create similar formulas for columns H, O, U, AA, AG based on column C
|
||||
# First, let's process column C for rows 25-27 as usual
|
||||
for row_num in range(25, 28): # 25, 26, 27
|
||||
for col_idx in range(1, sheet.max_column + 1):
|
||||
cell_coord = f"{get_column_letter(col_idx)}{row_num}"
|
||||
cell = sheet[cell_coord]
|
||||
if cell.data_type == 'f' and cell.value and isinstance(cell.value, str) and '{store_name}' in cell.value:
|
||||
print(f"Special handling for Graphics cell {cell_coord}: {cell.value}")
|
||||
original_formula = cell.value
|
||||
# Process column C first
|
||||
cell_coord_c = f"C{row_num}"
|
||||
try:
|
||||
cell_c = sheet[cell_coord_c]
|
||||
if cell_c.data_type == 'f' and cell_c.value and isinstance(cell_c.value, str):
|
||||
print(f"Special handling for Graphics cell {cell_coord_c}: {cell_c.value}")
|
||||
original_formula = cell_c.value
|
||||
updated_formula = original_formula
|
||||
for old_name, new_name in sheet_name_mapping.items():
|
||||
updated_formula = updated_formula.replace(old_name, new_name)
|
||||
|
||||
if updated_formula != original_formula:
|
||||
cell.value = updated_formula
|
||||
print(f"Force updated formula in {sheet_name} cell {cell_coord}")
|
||||
# Check if formula contains {store_name} or any of the old sheet names
|
||||
needs_update = '{store_name}' in original_formula
|
||||
if not needs_update:
|
||||
for old_name in sheet_name_mapping.keys():
|
||||
if old_name in original_formula:
|
||||
needs_update = True
|
||||
break
|
||||
|
||||
if needs_update:
|
||||
for old_name, new_name in sheet_name_mapping.items():
|
||||
updated_formula = updated_formula.replace(old_name, new_name)
|
||||
|
||||
if updated_formula != original_formula:
|
||||
cell_c.value = updated_formula
|
||||
sheet_updates += 1
|
||||
total_updates += 1
|
||||
print(f"Force updated formula in {sheet_name} cell {cell_coord_c}: {original_formula} -> {updated_formula}")
|
||||
|
||||
# Now create similar formulas for H, O, U, AA, AG columns
|
||||
# These should reference the specific year's forecast sheet
|
||||
year_cols = {
|
||||
'H': '2025 – Forecast',
|
||||
'O': '2026 – Forecast',
|
||||
'U': '2027 – Forecast',
|
||||
'AA': '2028 – Forecast',
|
||||
'AG': '2029 – Forecast'
|
||||
}
|
||||
|
||||
for col, year_prefix in year_cols.items():
|
||||
cell_coord = f"{col}{row_num}"
|
||||
try:
|
||||
# Create a formula for this cell that references the specific year's forecast sheet
|
||||
cell_ref = f"C{row_num - 15}" if row_num == 25 else f"C{row_num - 14}" # Map to row 10, 11, 12
|
||||
|
||||
# Check if the cell already exists
|
||||
cell = sheet[cell_coord]
|
||||
|
||||
# Create a new formula referencing the specific year's sheet
|
||||
new_sheet_name = f"{year_prefix} {sheet_name_mapping.get('2025 – Forecast {store_name}', '').replace('2025 – Forecast ', '')}"
|
||||
|
||||
# Determine the row reference based on the current row
|
||||
if row_num == 25:
|
||||
row_ref = "10"
|
||||
elif row_num == 26:
|
||||
row_ref = "11"
|
||||
else: # row_num == 27
|
||||
row_ref = "12"
|
||||
|
||||
new_formula = f"=SUM('{new_sheet_name}'!C{row_ref}:E{row_ref})"
|
||||
|
||||
# Only update if needed
|
||||
if cell.value != new_formula:
|
||||
original_value = cell.value
|
||||
cell.value = new_formula
|
||||
sheet_updates += 1
|
||||
total_updates += 1
|
||||
print(f"Created/updated formula in {sheet_name} cell {cell_coord}: {original_value} -> {new_formula}")
|
||||
except Exception as e:
|
||||
print(f"Error processing cell {cell_coord}: {e}")
|
||||
except Exception as e:
|
||||
print(f"Error processing cell {cell_coord_c}: {e}")
|
||||
|
||||
# Iterate through all cells in the sheet
|
||||
for row in sheet.iter_rows():
|
||||
|
||||
Reference in New Issue
Block a user