Export Scenario to Excel

This task exports the selected scenario to an Excel file.

    @Bean
    public ScriptedTaskDescription simpleExcelExportTask() {
        ScriptedTaskDescription task = new ScriptedTaskDescription("SimpleExcelExportTask", "Export scenario to Excel");
        task.setDescription("Exports the selected scenario to an Excel file");
        setI18nKeys(task, "EXCEL_EXPORT");

        var scenario = VariableAccessExpression.ofScenario();
        var baseFileName = VariableAccessExpression.of("base file name");
        var filter = VariableAccessExpression.ofFilter();

        VariableAccessExpression scenarioData = VariableAccessExpression.of("scenario-data");

        task.getScript()
            .addStatement(AskInputStatement.ofVariable(scenario, true, JobInputType.SCENARIO_ID))
            .addStatement(AskInputStatement.ofVariable(baseFileName, true, ParameterTypes.TEXT, "Name of the file to export to. Warning: a '.xlsx' extension will be appended."))
            .addStatement(AskInputStatement.ofVariable(filter, false, JobInputType.ENTITIES, "Select the tables to export"))

            // Load the scenario data into a variable
            .addStatement(SetVariableStatement.of(
                    scenarioData.getVariableName(),
                    ScenarioDataExpression.of(scenario).withFormat(ScenarioDataFormat.EXCEL).onlyTables(filter)
            ))

            // Set the Excel file as task output
            .addStatement(SetTaskOutputStatement.of("Excel file",
                FileExpression.of(
                    StringExpression.concat(baseFileName, StringExpression.of(".xlsx")),
                    BlobExpression.of(scenarioData),
                    StringExpression.of("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
                )))

            // If there are errors or warnings, we exit the task with an alert.
            // The only known case is when the scenario content does not fit into an Excel file.
            .addStatement(ExitTaskStatement.alerting(StringExpression.of("Exporting the scenario data was not completed successfully"))
                .when(ScenarioDataExpression.hasErrorsOrWarnings(scenarioData))
            );

        return task;
    }