Using Calculation Algorithm for Calculations in Reports

Consider the example of connecting a calculation algorithm for calculations in regular reports and express reports. For correct work in algorithm calculation blocks and in a report, one should use the same repository data source.

In reports, all calculations are executed in an analytical data area. An analytical data area contains settings for creating single slices based on the data sources that can be connected to the report. An analytical data area can be obtained using the IPrxReport.DataArea property in regular reports and the IEaxAnalyzer.DataArea property in express reports. In the IEaxDataArea.Slices collection the required data slice cast to the IEaxDataAreaPivotSlice interface. The obtained slice has the IEaxDataAreaPivotSlice.ExternalTransformations property that returns the collection of modeling problems for data transformation. This collection will provide a algorithm to be connected.

The calculation algorithm's structure has hidden child objects that are used to provide algorithm work. These objects include a modeling problem that stores calculation block formulas of the algorithm. To get the modeling problem, get the collection of algorithm's child objects using the IMetabaseObjectDescriptor.Children property and find among them an object of the MetabaseObjectClass.KE_CLASS_MSPROBLEM class. The example of function for finding modeling problem:

Public Function GetAlgProblem(ObjDesc: IMetabaseObjectDescriptor): IMsProblem;
Var
    ChildDescs: IMetabaseObjectDescriptors;
    ChildDesc: IMetabaseObjectDescriptor;
    Problem: IMsProblem;
Begin
    ChildDescs := ObjDesc.Children;
    For Each ChildDesc In ChildDescs Do
        If ChildDesc.ClassId = MetabaseObjectClass.KE_CLASS_MSPROBLEM Then
            Problem := ChildDesc.Edit As IMsProblem;
            If Problem.Details Is IMsRtProblem Then
                Return Problem;
            End If;
        Else
            Problem := GetAlgProblem(ChildDesc);
            If Problem <> Null Then
                Return Problem;
            End If;
        End If;
    End For;
End Function GetAlgProblem;

In the IEaxDataAreaPivotSlice.ExternalTransformations collection create new transformation, in the IEaxDataAreaExternalTransformation.Problem property specify the modeling problem found among calculation algorithm's child objects, and also set the IEaxDataAreaExternalTransformation.Enabled property to True. As a result, the calculation algorithm is used for data transformation after calculating analytical data area. If required, determine additional settings for the created transformation according to the executed problem. It is recommended to enable highlighting of cells with calculated values for the data slice by setting the IEaxGrid.HighlightEvaluatedCells property to True. The code that executes the described operations looks as follows:

Sub ConnectAlgoInReport(ReportId: String; AlgoId: String);
Var
    Mb: IMetabase;
    MObj: IMetabaseObject;
    Slice: IEaxDataAreaPivotSlice;
    Grid: IEaxGrid;
    Transform: IEaxDataAreaExternalTransformation;
Begin
    Mb := MetabaseClass.Active;
    // Open report for edit
    MObj := Mb.ItemById(ReportId).Edit;
    // Get slice depending on calculation type
    If MObj.ClassId = MetabaseObjectClass.KE_CLASS_EXPRESSREPORT Then
        Slice := (MObj As IEaxAnalyzer).DataArea.Slices.Item(0As IEaxDataAreaPivotSlice;
    Elseif MObj.ClassId = MetabaseObjectClass.KE_CLASS_PROCEDURALREPORT Then
        Slice := (MObj As IPrxReport).DataArea.Slices.Item(0As IEaxDataAreaPivotSlice;
    Else
        Return;
    End If;
    // Add a transformation
    Transform := Slice.ExternalTransformations.Add;
    // Connect modeling problem from calculation algorithm
    Transform.Problem := GetAlgProblem(Mb.ItemById(AlgoId));
    Transform.Enabled := True;
    // Enable highlighting of cells with calculated values in slice table
    Grid := (Slice As IEaxDataAreaSlice).Views.Item(0As IEaxGrid;
    Grid.HighlightEvaluatedCells := True;
    // Save changes
    MObj.Save;
End Sub ConnectAlgoInReport;

To connect the calculation algorithm, delete the corresponding transformation from the IEaxDataAreaPivotSlice.ExternalTransformations collection or clear the entire collection:

Public Sub ClearTransformations(ReportId: String);
Var
    Mb: IMetabase;
    MObj: IMetabaseObject;
    Slice: IEaxDataAreaPivotSlice;
Begin
    Mb := MetabaseClass.Active;
    // Open report for edit
    MObj := Mb.ItemById(ReportId).Edit;
    // Get slice depending on calculation type
    If MObj.ClassId = MetabaseObjectClass.KE_CLASS_EXPRESSREPORT Then
        Slice := (MObj As IEaxAnalyzer).DataArea.Slices.Item(0As IEaxDataAreaPivotSlice;
    Elseif MObj.ClassId = MetabaseObjectClass.KE_CLASS_PROCEDURALREPORT Then
        Slice := (MObj As IPrxReport).DataArea.Slices.Item(0As IEaxDataAreaPivotSlice;
    Else
        Return;
    End If;
    // Clear collection
    Slice.ExternalTransformations.Clear;
    // Save changes
    MObj.Save;
End Sub ClearTransformations;

See also:

Examples