From 76627f47ca75b8da5dc836427012a474dba7685e Mon Sep 17 00:00:00 2001 From: atthaboons Date: Mon, 23 Nov 2020 07:08:06 +0700 Subject: [PATCH] Release 1.2.2 support auto add columns --- .../ExcelParser/ABCParserStrategy.py | 30 +++++++++++++++++++ ExcelDataDriver/ExcelParser/ParserContext.py | 9 ++++-- ExcelDataDriver/__init__.py | 2 +- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/ExcelDataDriver/ExcelParser/ABCParserStrategy.py b/ExcelDataDriver/ExcelParser/ABCParserStrategy.py index cefacef..02ae2f2 100644 --- a/ExcelDataDriver/ExcelParser/ABCParserStrategy.py +++ b/ExcelDataDriver/ExcelParser/ABCParserStrategy.py @@ -35,14 +35,44 @@ def map_data_row_into_test_data_obj(self, ws_column_indexes, ws_title, row_index def get_all_worksheet(self, wb): return list(wb) + def parsing_major_column_indexs(self, ws): + ws_column_indexs = {} + key_index_row = 0 + found_default_column_indexs = False + + # Parse mandatory property + for index, row in enumerate(ws.rows): + if index > self.maximum_column_index_row: + break + for cell in row: + if (cell.value is not None) and (cell.value in self.DEFAULT_COLUMN_INDEXS): + ws_column_indexs[cell.value] = column_index_from_string(coordinate_from_string(cell.coordinate)[0]) + print(str(datetime.now()) + ': Mandatory : ' + str(cell.value) + ' : ' + str( + cell.coordinate) + ' : ' + str( + column_index_from_string(coordinate_from_string(cell.coordinate)[0]))) + key_index_row = index + 1 + found_default_column_indexs = True + + if (cell.value is not None) and (cell.value not in self.DEFAULT_COLUMN_INDEXS) and (found_default_column_indexs is False): + field_name = str(cell.value).lower().strip().replace(" ", "_") + if field_name == self.main_column_key: + key_index_row = index + 1 + + if len(ws_column_indexs) > 0: + break + + return ws_column_indexs, key_index_row + def parsing_column_indexs(self, ws): ws_column_indexs = {} + # Parse mandatory property for index, row in enumerate(ws.rows): if index > self.maximum_column_index_row: break for cell in row: if (cell.value is not None) and (cell.value in self.DEFAULT_COLUMN_INDEXS): + found_mandatory_property = True ws_column_indexs[cell.value] = column_index_from_string(coordinate_from_string(cell.coordinate)[0]) print(str(datetime.now())+': Mandatory : '+str(cell.value) + ' : ' + str(cell.coordinate) + ' : ' + str(column_index_from_string(coordinate_from_string(cell.coordinate)[0]))) self.start_row = index + 1 diff --git a/ExcelDataDriver/ExcelParser/ParserContext.py b/ExcelDataDriver/ExcelParser/ParserContext.py index d4cdebd..4bb3cac 100644 --- a/ExcelDataDriver/ExcelParser/ParserContext.py +++ b/ExcelDataDriver/ExcelParser/ParserContext.py @@ -28,10 +28,13 @@ def parse(self, wb): return ws_test_data_rows def insert_extra_columns(self, wb, columns): - for ws in self.parser_strategy.get_all_worksheet(wb): - ws_column_indexs = self.parser_strategy.parsing_column_indexs(ws) + ws_list = self.parser_strategy.get_all_worksheet(wb) + for ws in ws_list: + print(str(datetime.now()) + ': start parse data...') + ws_column_indexs, key_index_row = self.parser_strategy.parsing_major_column_indexs(ws) for column in reversed(columns): if column in ws_column_indexs: continue ws.insert_cols(1) - ws['A' + str(self.parser_strategy.start_row - 2)] = column + ws['A' + str(key_index_row)] = column + print(str(datetime.now()) + ': Done parse data...') diff --git a/ExcelDataDriver/__init__.py b/ExcelDataDriver/__init__.py index a8a7711..c34872f 100644 --- a/ExcelDataDriver/__init__.py +++ b/ExcelDataDriver/__init__.py @@ -40,7 +40,7 @@ from ExcelDataDriver.Config.CaptureScreenShotOption import CaptureScreenShotOption -__version__ = '1.2.1' +__version__ = '1.2.2' class ExcelDataDriver: