Skip to main content

Excel

VIKTOR provides an integration with Excel to be able to use an Excel sheet with macros within your app. If you want to use an Excel sheet without macros as a calculation tool, you can also consider using SpreadsheetCalculation.

VIKTOR's Excel integration requires a specific Excel worker which can be downloaded here.

Instances of NamedInputCell (cells which can be found using a certain key) and DirectInputCell (cell which can be found by a direct address {sheet, column, row}) can be used to fill the template. Similarly, instances of NamedOutputCell and DirectOutputCell can be used to specify which cells should be returned.

Below example assumes the template (represented by bytes) is available. The sheet can be filled with predefined values:

from viktor.external.spreadsheet import NamedInputCell, DirectOutputCellfrom viktor.external.excel import Excel, Macrotemplate = BytesIO(b'my_template')def run_excel():    named_input_cells = [        NamedInputCell('named_cell_1', 'text_to_be_placed'),        NamedInputCell('named_cell_2', 5),    ]    macros = [Macro('caculate_mean'), Macro('calculate_area')]    area_cell = DirectOutputCell('sheet1', 'B', 9)    direct_output_cells = [        area_cell    ]    excel = Excel(template=template, named_input_cells=named_input_cells,                  macros=macros, direct_output_cells=direct_output_cells)    # The timeout in excel.execute() is the time in seconds the whole cycle of    # sending the job, executing the job, and receiving the job result may take.    excel.execute(timeout=120)    return area_cell.result  # or excel.get_direct_cell_result('sheet1', 'B', 9)

The macros are run in the order in which they are present in the list. Thus, in above example 'calculate_mean' is run first and 'calculate_area' second.

caution

Excel.execute needs to be mocked within the context of (automated) testing.