Implemented hiding forecast sheets not in the calculated years array
This commit is contained in:
@@ -9,6 +9,7 @@ from zipfile import ZipFile, ZIP_DEFLATED
|
||||
def update_excel_variables(excel_path):
|
||||
"""
|
||||
Update the Variables sheet in the Excel file with values from config.json
|
||||
and hide forecast sheets that aren't in the calculated years array
|
||||
|
||||
Args:
|
||||
excel_path (str): Path to the Excel file to update
|
||||
@@ -112,6 +113,68 @@ def update_excel_variables(excel_path):
|
||||
print("Saving workbook with updated variables...")
|
||||
wb.save(excel_path)
|
||||
|
||||
# Get the calculated years array from config
|
||||
starting_date = user_data.get('starting_date', '')
|
||||
duration = user_data.get('duration', 36)
|
||||
calculated_years = []
|
||||
|
||||
# Import datetime at the module level to avoid scope issues
|
||||
import datetime
|
||||
from dateutil.relativedelta import relativedelta
|
||||
|
||||
# Calculate years array based on starting_date and duration
|
||||
try:
|
||||
# Try to parse the date, supporting both dd/mm/yyyy and dd.mm.yyyy formats
|
||||
if starting_date:
|
||||
if '/' in str(starting_date):
|
||||
day, month, year = map(int, str(starting_date).split('/'))
|
||||
elif '.' in str(starting_date):
|
||||
day, month, year = map(int, str(starting_date).split('.'))
|
||||
elif '-' in str(starting_date):
|
||||
# Handle ISO format (yyyy-mm-dd)
|
||||
date_parts = str(starting_date).split('-')
|
||||
if len(date_parts) == 3:
|
||||
year, month, day = map(int, date_parts)
|
||||
else:
|
||||
# Default to current date if format is not recognized
|
||||
current_date = datetime.datetime.now()
|
||||
year, month, day = current_date.year, current_date.month, current_date.day
|
||||
elif isinstance(starting_date, datetime.datetime):
|
||||
day, month, year = starting_date.day, starting_date.month, starting_date.year
|
||||
else:
|
||||
# Default to current date if format is not recognized
|
||||
current_date = datetime.datetime.now()
|
||||
year, month, day = current_date.year, current_date.month, current_date.day
|
||||
|
||||
# Create datetime object for starting date
|
||||
start_date = datetime.datetime(year, month, day)
|
||||
|
||||
# Calculate end date (starting date + duration months - 1 day)
|
||||
end_date = start_date + relativedelta(months=duration-1)
|
||||
|
||||
# Create a set of years (to avoid duplicates)
|
||||
years_set = set()
|
||||
|
||||
# Add starting year
|
||||
years_set.add(start_date.year)
|
||||
|
||||
# Add ending year
|
||||
years_set.add(end_date.year)
|
||||
|
||||
# If there are years in between, add those too
|
||||
for y in range(start_date.year + 1, end_date.year):
|
||||
years_set.add(y)
|
||||
|
||||
# Convert set to sorted list
|
||||
calculated_years = sorted(list(years_set))
|
||||
print(f"Calculated years for sheet visibility: {calculated_years}")
|
||||
else:
|
||||
# Default to current year if no starting date
|
||||
calculated_years = [datetime.datetime.now().year]
|
||||
except Exception as e:
|
||||
print(f"Error calculating years for sheet visibility: {e}")
|
||||
calculated_years = [datetime.datetime.now().year]
|
||||
|
||||
# Update sheet names - replace {store_name} with actual store name
|
||||
store_name = user_data.get('store_name', '')
|
||||
if store_name:
|
||||
@@ -130,8 +193,21 @@ def update_excel_variables(excel_path):
|
||||
# Store the mapping
|
||||
sheet_name_mapping[sheet_name] = new_sheet_name
|
||||
print(f"Renamed sheet '{sheet_name}' to '{new_sheet_name}'")
|
||||
|
||||
# Check if this is a forecast sheet and if its year is in the calculated years
|
||||
# Forecast sheets have names like "2025 – Forecast {store_name}"
|
||||
if "Forecast" in new_sheet_name:
|
||||
# Extract the year from the sheet name
|
||||
try:
|
||||
sheet_year = int(new_sheet_name.split()[0])
|
||||
# Hide the sheet if its year is not in the calculated years
|
||||
if sheet_year not in calculated_years:
|
||||
sheet.sheet_state = 'hidden'
|
||||
print(f"Hiding sheet '{new_sheet_name}' as year {sheet_year} is not in calculated years {calculated_years}")
|
||||
except Exception as e:
|
||||
print(f"Error extracting year from sheet name '{new_sheet_name}': {e}")
|
||||
|
||||
# Save the workbook with renamed sheets
|
||||
# Save the workbook with renamed and hidden sheets
|
||||
wb.save(excel_path)
|
||||
|
||||
# Use direct XML modification to replace all instances of {store_name} in formulas
|
||||
|
||||
Reference in New Issue
Block a user