From 2e42167c7322f1265c39dbf70032540f3f4bfdcd Mon Sep 17 00:00:00 2001 From: Benjamin Admin Date: Sat, 11 Apr 2026 22:04:49 +0200 Subject: [PATCH] Remove empty columns from grid zones Columns with zero cells (e.g. from tertiary detection where the word was assigned to a neighboring column by overlap) are stripped from the final result. Remaining columns and cells are re-indexed. Co-Authored-By: Claude Opus 4.6 (1M context) --- klausur-service/backend/grid_editor_api.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/klausur-service/backend/grid_editor_api.py b/klausur-service/backend/grid_editor_api.py index 1e4f022..2cc04e0 100644 --- a/klausur-service/backend/grid_editor_api.py +++ b/klausur-service/backend/grid_editor_api.py @@ -1731,6 +1731,27 @@ async def _build_grid_core( if fixed != text: cell["text"] = fixed + # --- Remove empty columns (no cells assigned) --- + for z in zones_data: + cells = z.get("cells", []) + used_col_indices = {c.get("col_index") for c in cells} + old_cols = z.get("columns", []) + new_cols = [c for c in old_cols if c.get("col_index", c.get("index", -1)) in used_col_indices] + if len(new_cols) < len(old_cols): + # Re-index columns and cells + old_to_new = {} + for new_i, col in enumerate(new_cols): + old_i = col.get("col_index", col.get("index", new_i)) + old_to_new[old_i] = new_i + col["col_index"] = new_i + col["index"] = new_i + col["label"] = f"column_{new_i + 1}" if len(new_cols) > 1 else "column_text" + for cell in cells: + old_ci = cell.get("col_index", 0) + cell["col_index"] = old_to_new.get(old_ci, old_ci) + cell["col_type"] = f"column_{cell['col_index'] + 1}" if len(new_cols) > 1 else "column_text" + z["columns"] = new_cols + # Clean up internal flags before returning for z in zones_data: for cell in z.get("cells", []):