diff --git a/src/excel_reader.py b/src/excel_reader.py index a84fb52..5fe2227 100644 --- a/src/excel_reader.py +++ b/src/excel_reader.py @@ -139,6 +139,30 @@ def get_sheet_names(file_path: str | Path) -> List[str]: return names +def resolve_sheet_name( + sheet_name: str, + workbook_sheets: List[str], + month_mapping: Dict[str, str], +) -> str: + """Resolve a month alias to the actual sheet name present in the workbook. + + If *sheet_name* exists in *workbook_sheets*, it is returned unchanged. + Otherwise all aliases in *month_mapping* that map to the same month number + are checked, and the first one present in *workbook_sheets* is returned. + If no alias matches, the original *sheet_name* is returned so that the + normal missing-sheet error can fire. + """ + if sheet_name in workbook_sheets: + return sheet_name + target_month = month_mapping.get(sheet_name) + if target_month is None: + return sheet_name + for alias, month_num in month_mapping.items(): + if month_num == target_month and alias in workbook_sheets: + return alias + return sheet_name + + def extract_persons( file_path: str | Path, sheet_name: str, @@ -158,6 +182,7 @@ def extract_persons( wb = _open_workbook(Path(file_path)) try: + sheet_name = resolve_sheet_name(sheet_name, wb.sheetnames, config["month_mapping"]) data = _sheet_data(wb, sheet_name) finally: wb.close() @@ -217,6 +242,7 @@ def extract_attendance( wb = _open_workbook(Path(file_path)) try: + sheet_name = resolve_sheet_name(sheet_name, wb.sheetnames, month_mapping) data = _sheet_data(wb, sheet_name) finally: wb.close() diff --git a/src/validators.py b/src/validators.py index 4297bdd..7eeb01b 100644 --- a/src/validators.py +++ b/src/validators.py @@ -14,6 +14,7 @@ from pathlib import Path from typing import Any, Dict, List, Optional from .config_loader import load_config +from .excel_reader import resolve_sheet_name from .exceptions import ConfigurationError @@ -71,18 +72,19 @@ def validate( ) sheet_names = wb.sheetnames - # Check 4: sheet name valid + # Check 4: sheet name valid (resolve alias, e.g. Mär → Mrz) if sheet_name not in config["month_mapping"]: errors.append( f"Sheet-Name '{sheet_name}' nicht in month_mapping der config.json." ) - if sheet_name not in sheet_names: + resolved = resolve_sheet_name(sheet_name, sheet_names, config["month_mapping"]) + if resolved not in sheet_names: errors.append( f"Sheet '{sheet_name}' existiert nicht in der Arbeitsmappe. " f"Verfügbar: {sheet_names}" ) else: - ws = wb[sheet_name] + ws = wb[resolved] rows = list(ws.iter_rows(values_only=True)) name_col: int = config["excel_name_column"] @@ -179,11 +181,12 @@ def _extract_samples( try: wb = openpyxl.load_workbook(str(file_path), read_only=True, data_only=True) - if sheet_name not in wb.sheetnames: + resolved = resolve_sheet_name(sheet_name, wb.sheetnames, config["month_mapping"]) + if resolved not in wb.sheetnames: wb.close() return result - ws = wb[sheet_name] + ws = wb[resolved] rows = list(ws.iter_rows(values_only=True)) wb.close()