Update business case form with currency symbol and improved functionality

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
andrei
2025-10-01 06:37:47 +00:00
parent 2f569ae683
commit d2ff24b6da
4 changed files with 60 additions and 6 deletions

View File

@@ -3,7 +3,59 @@ import json
import os
import re
import openpyxl
from openpyxl.utils import get_column_letter
CURRENCY_LOCALE_MAP = {
'$': '-409', # English (US)
'': '-2', # English (Euro)
}
CURRENCY_BRACKET_PATTERN = re.compile(r'\[\$([^\]-]*)(-[^\]]*)?\]')
def apply_currency_symbol(workbook, symbol):
"""Propagate the selected currency symbol across formats, labels, and charts."""
target_symbol = (symbol or '$').strip() or '$'
def replace_currency_token(fmt):
if not isinstance(fmt, str) or ('$' not in fmt and '' not in fmt):
return fmt
def _replace_match(match):
locale_hint = match.group(2) or ''
if target_symbol in CURRENCY_LOCALE_MAP:
locale_hint = f"-{CURRENCY_LOCALE_MAP[target_symbol].lstrip('-')}" if match.group(2) or locale_hint else ''
return f'[${target_symbol}{locale_hint}]'
updated = CURRENCY_BRACKET_PATTERN.sub(_replace_match, fmt)
updated = updated.replace('"$"', f'"{target_symbol}"')
return updated
for worksheet in workbook.worksheets:
for row in worksheet.iter_rows():
for cell in row:
fmt = cell.number_format
updated_format = replace_currency_token(fmt)
if updated_format != fmt:
cell.number_format = updated_format
value = cell.value
if isinstance(value, str) and not value.startswith('=') and ('' in value or '$' in value):
new_value = value.replace('', target_symbol).replace('$', target_symbol)
if new_value != value:
cell.value = new_value
for chart in getattr(worksheet, '_charts', []):
axes = [getattr(chart, 'y_axis', None), getattr(chart, 'secondary_y_axis', None)]
for axis in axes:
if not axis or not getattr(axis, 'number_format', None):
continue
fmt_obj = axis.number_format
format_code = getattr(fmt_obj, 'formatCode', None)
if not isinstance(format_code, str):
continue
updated_code = replace_currency_token(format_code)
if updated_code != format_code:
fmt_obj.formatCode = updated_code
def update_excel_variables(excel_path):
"""
@@ -153,6 +205,8 @@ def update_excel_variables(excel_path):
except Exception as e:
print(f"Error updating cell {cell_ref}: {e}")
apply_currency_symbol(wb, user_data.get('currency_symbol', ''))
# Force formula recalculation before saving
print("Forcing formula recalculation...")
wb.calculation.calcMode = 'auto'