Example of transformation from the values tree to the values table and back

I want to share with visitors of the site my approach to transform the values table in the values tree and back.
In general, when developing the branch-wise task, it was necessary almost for all the documents to output information as a tree and store it in the table parts of document, as well as in the interactive data processor as a tree.

Hence, there was a small module to extend the capabilities of work with the values tree, although with some reservations.
The main idea is to use two key attributes/columns RowKey and CommunicationKey.
In my opinion, the code is quite compact and universal.
The purpose of publication - to share with novices the experience.

Function GetNewKeyForRowTree(Tree, ListKeys = Undefined) Export

    If ListKeys = Undefined Then
        ListKeys = New ValueList;
        ListKeys.Add(0);
	EndIf;
	
    For Each StringTree In Tree.Rows Do
        ListKeys.Add(StringTree.RowKey);
        GetNewKeyForRowTree(StringTree, ListKeys);
        ListKeys.SortByValue(SortDirection.Desc);
        MaxKey = ListKeys[0].Value + 1;
	EndDo;
	
    Return MaxKey;

EndFunction 

Procedure UpdateKeysLinksInValueTree(Tree) Export

    For Each StringTree In Tree.Rows Do
        Try
            StringTree.CommunicationKey = StringTree.Parent.RowKey;
        Except
            StringTree.CommunicationKey = 0;
        EndTry;
        UpdateKeysLinksInValueTree(StringTree);
    EndDo;

EndProcedure 

Procedure UpdateRowKeysInValueTree(Tree, RowKey = 1) Export

    For Each StringTree In Tree.Rows Do
        StringTree.RowKey = RowKey;
        RowKey = RowKey + 1;
        UpdateRowKeysInValueTree(StringTree, RowKey);
    EndDo;

EndProcedure 

Function UnloadTreeValuesInTableValues(Tree, Table = Undefined) Export

    If Table = Undefined Then
        Table = New ValueTable;
        For Each Column In Tree.Columns Do
            Table.Columns.Add(Column.Name, Column.ValueType);
        EndDo;
    EndIf;
    For Each StringTree In Tree.Rows Do
        FillPropertyValues(Table.Add(), StringTree);
        UnloadTreeValuesInTableValues(StringTree, Table);
    EndDo;
    Return Table;

EndFunction 

Function UnloadTableValuesInValueTree(Table, RowKey = "RowKey", CommunicationKey = "CommunicationKey") Export

    Tree = New ValueTree;
    For Each Column In Table.Columns Do
        Tree.Columns.Add(Column.Name, Column.ValueType);
    EndDo;
    For Each TableRow In Table Do
        StringGrouping = Tree.Rows.Find(TableRow[CommunicationKey], RowKey,True);
        If StringGrouping = Undefined Then
            FillPropertyValues(Tree.Rows.Add(), TableRow);
        Else
            FillPropertyValues(StringGrouping.Rows.Add(), TableRow);
        EndIf;
    EndDo;
    Return Tree;

EndFunction 

Procedure SetValueColumnTree(Tree, Column, Value) Export

    For Each StringTree In Tree.Rows Do
        StringTree[Column] = Value;
        SetValueColumnTree(StringTree, Column, Value);
    EndDo;

EndProcedure 

Procedure CopySubordinateLine(LineReceiver, SourceString)

    For Each Row In SourceString.Rows Do
        NewRow = LineReceiver.Rows.Add();
        NewRow.CommunicationKey = LineReceiver.RowKey;
        FillPropertyValues(NewRow, Row);
        CopySubordinateLine(NewRow, Row);
    EndDo;

EndProcedure 

Procedure MoveStringValueTree(LineReceiver, SelectedRows) Export

    If Not LineReceiver = Undefined Then
        ArrayStrings = New Array;
        For Each LineTransfer In SelectedRows Do
            ArrayStrings.Add(LineTransfer);
            NewRow = LineReceiver.Rows.Add();
            CopySubordinateLine(NewRow, LineTransfer);
            FillPropertyValues(NewRow, LineTransfer);
            NewRow.CommunicationKey = LineReceiver.RowKey;
        EndDo;
        For Each StringTree In ArrayStrings Do
            If StringTree.Parent = Undefined Then
                StringTree.Rows.Delete(StringTree);
            Else
                StringTree.Parent.Rows.Delete(StringTree);
            EndIf;
        EndDo;
    EndIf;

EndProcedure 

Publications: Example of transformation from the values tree to the values table and back

You can download an example of data processor here: TreeToTable.zip.

[Total: 0    Average: 0/5]

Leave a Reply

Your email address will not be published. Required fields are marked *