Calling Excel Import in Tasks
The ImportScenarioStatement script statement can be included in a task. It takes a scenario ID and an Excel file as inputs, and optionally stores the issues raised during import as a JSON file into a variable of the job. The thenCheckSchema(Expression checkSchema) method can also be applied to this statement. It takes an expression that must evaluate to a Boolean value. It activates/deactivates running the schema checkers on the scenario after importing the data. If absent, the schema checkers are always triggered after data import.
Here is an example of a task that imports an Excel file using this statement. Feel free to copy it and adapt it as needed. The Platform built-in task accessible as "Create scenario from file" in the list of tasks has the same purpose; its ID is ScenarioImportTask.
@Bean
public ScriptedTaskDescription scenarioImportTask() {
ScriptedTaskDescription task = new ScriptedTaskDescription("Import Excel file", "Import Excel data into a new scenario");
task.setDescription("Import an Excel data into a new scenario");
// Init variables
VariableAccessExpression workspace = VariableAccessExpression.of("workspace");
VariableAccessExpression scenarioFile = VariableAccessExpression.of("file");
VariableAccessExpression scenarioName = VariableAccessExpression.of("Scenario Name");
VariableAccessExpression scenarioId = VariableAccessExpression.of("scenarioId");
VariableAccessExpression issues = VariableAccessExpression.of("issues");
VariableAccessExpression doSchemaCheck = VariableAccessExpression.of("Schema Check");
task.getScript()
// Ask for input from the web client
.addStatement(AskInputStatement.of(workspace.getVariableName(), true, JobInputType.WORKSPACE_ID, "The Workspace where to import the new Scenario"))
.addStatement(AskInputStatement.of(scenarioFile.getVariableName(), true, JobInputType.file("xlsx", "dbrf", "gz", "zip"), "The scenario file containing the data (.xlsx, .dbrf, .gz, .zip)"))
.addStatement(AskInputStatement.of(scenarioName.getVariableName(), true, JobInputType.TEXT, "The name of the new scenario"))
.addStatement(AskInputStatement.of(doSchemaCheck.getVariableName(), true, JobInputType.BOOLEAN, "Check this to run a scenario schema check after import"))
// Create a new scenario
.addStatement(SetVariableStatement.of(scenarioId.getVariableName(), StringExpression.idOfNewScenario(scenarioName, workspace)))
// Save the file content in the Data Service
.addStatement(ImportScenarioStatement.of(scenarioId, scenarioFile)
.storingIssuesInto(issues.getVariableName())
//After import, do a scenario schema check (if the user chooses to do so)
.thenCheckSchema(doSchemaCheck))
// Finally, save the issues to output
.addStatement(SetTaskOutputStatement.of("issues", issues));
return task;
}