Calling Excel Export in Tasks

There is no specific script statement to export the data of a scenario as an Excel file. Instead, the scenario data can be retrieved in Excel format by creating a ScenarioDataExpression as follows, where scenarioId is an expression that evaluates to the ID of the scenario.

ScenarioDataExpression.of(scenarioId).withFormat(ScenarioDataFormat.EXCEL)

Here is an example of a task that exports a scenario as an Excel file, using the construction above and then creating a file out of it. Feel free to copy it and augment it as needed. The built-in task accessible as "Export scenario to Excel" in the list of tasks has the same purpose; its ID is SimpleExcelExportTask.

    @Bean
    public ScriptedTaskDescription excelExport() {
        ScriptedTaskDescription task = new ScriptedTaskDescription("ExcelExportExampleTask", "Export scenario to Excel (example)");
        task.setDescription("Exports the selected scenario to an Excel file");

        // Init variables
        VariableAccessExpression scenario = VariableAccessExpression.of("scenario");
        VariableAccessExpression baseFileName = VariableAccessExpression.of("base file name");

        task.getScript()
            // Ask for input from the web client
            .addStatement(AskInputStatement.of(scenario.getVariableName(), true, JobInputType.SCENARIO_ID))
            .addStatement(AskInputStatement.of(baseFileName.getVariableName(), true, JobInputType.TEXT, "Name of the file to export to. Warning: a '.xlsx' extension will be appended."))

            // Export the scenario in Excel format
            .addStatement(SetTaskOutputStatement.of("Excel file",
                FileExpression.of(
                    StringExpression.concat(baseFileName, StringExpression.of(".xlsx")),
                    BlobExpression.of(ScenarioDataExpression.of(scenario).withFormat(ScenarioDataFormat.EXCEL)),
                    StringExpression.of("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
                )));

        return task;
    }