From 4e1b21156837424f8c90af5ab6515874d21a31e9 Mon Sep 17 00:00:00 2001 From: Nathan Tranchant Date: Fri, 31 May 2024 15:13:31 +0200 Subject: [PATCH] Better exception handling in tables (#37) * Fix crash on unsupported cell interfaces * Better exception handling in tables --- lotemplate/Statement/TableStatement.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/lotemplate/Statement/TableStatement.py b/lotemplate/Statement/TableStatement.py index 5eaf726..c4ddc6d 100644 --- a/lotemplate/Statement/TableStatement.py +++ b/lotemplate/Statement/TableStatement.py @@ -1,5 +1,5 @@ from com.sun.star.lang import XComponent -from com.sun.star.sheet import XCellRangeData +from com.sun.star.uno import RuntimeException from typing import Union import lotemplate.errors as errors import regex @@ -47,15 +47,18 @@ def scan_cell(cell) -> None: tab_vars = {} list_tab_vars = [] for i in range(doc.getTextTables().getCount()): - table = doc.getTextTables().getByIndex(i) - if not isinstance(table, XCellRangeData): + try: + table_data = doc.getTextTables().getByIndex(i).getDataArray() + t_name = doc.getTextTables().getByIndex(i).getName() + nb_rows = len(table_data) + for row_i, row in enumerate(table_data): + for column in row: + scan_cell(column) + except errors.TemplateError as e: + raise e + continue + except RuntimeException as e: continue - table_data = table.getDataArray() - t_name = doc.getTextTables().getByIndex(i).getName() - nb_rows = len(table_data) - for row_i, row in enumerate(table_data): - for column in row: - scan_cell(column) return list_tab_vars if get_list else tab_vars