Cloning line from the table that is inside another table

General TRichView support forum. Please post your questions here
Post Reply
Ceprotec
Posts: 259
Joined: Thu Oct 28, 2010 6:09 pm
Contact:

Cloning line from the table that is inside another table

Post by Ceprotec »

Hi Sergey,

I have the following function:

Code: Select all

procedure ReplicaLinhasTabela ( Editor : TSRichViewEdit; Data : TStringListArray; ShowProgress : Boolean = False; lblProgress : TLabel = nil ) ;
var
    ItemNo : Integer;

    IndexVar : Integer;
    PossuiVar : Boolean;
    PrimaryIndex : Integer;

    count : Integer;

    j , i , k , b : Integer;
    table : TRVTableItemInfo;

    VariablesEx : string;
    columns : array of TMemoryStream;
begin
    try
        VariablesEx := '';

        for i := Low ( Data ) to High ( Data ) do
        begin
            if Assigned ( Data[ i ] ) = True then
            begin
                for j := 0 to ( Data[ i ] .Count - 1 ) do
                    VariablesEx := iif ( VariablesEx = '' , '{' + Data[ i ] .Names[ j ] + '}' , VariablesEx + ',' + '{' + Data[ i ] .Names[ j ] + '}' ) ;
            end;
        end;

        Editor.Format;

        for ItemNo := ( Editor.RichViewEdit.ItemCount - 1 ) downto 0 do
        begin
            if Editor.RichViewEdit.GetItemStyle ( ItemNo ) = rvsTable then
            begin
                table := TRVTableItemInfo ( Editor.RichViewEdit.GetItem ( ItemNo ) ) ;

                IndexVar := -1;
                PossuiVar := False;

                { COPIA AS INFORMAÇÕES DA LINHA ONDE FOI ACHADO AS VARIÁVEIS }
                for j := 0 to ( table.Rows.Count - 1 ) do
                begin
                    PossuiVar := False;

                    for i := 0 to ( table.Rows[ j ] .Count - 1 ) do
                    begin
                        if table.Cells[ j , i ] <> nil then
                        begin
                            if table.Cells[ j , i ] .GetRVData <> nil then
                            begin
                                if VarInTable ( table.Cells[ j , i ] .GetRVData , VariablesEx ) = True then
                                begin
                                    IndexVar := j;
                                    PossuiVar := True;
                                    Break;
                                end;
                            end;
                        end;
                    end;

                    if PossuiVar = True then
                    begin
                        SetLength ( columns , table.Rows[ j ] .Count ) ;

                        for i := 0 to ( table.Rows[ j ] .Count - 1 ) do
                        begin
                            if table.Cells[ j , i ] <> nil then
                            begin
                                columns[ i ] := TMemoryStream.Create;
                                table.Cells[ j , i ] .Edit ( ) ;
                                Editor.RichViewEdit.TopLevelEditor.SaveRVFToStream ( columns[ i ] , False ) ;
                            end;
                        end;

                        Break;
                    end;
                end;
                { FIM }

                if PossuiVar = True then
                begin
                    k := 0;
                    PrimaryIndex := IndexVar;

                    if ( ShowProgress = True ) and ( Assigned ( lblProgress ) = True ) then
                        count := Length ( Data )
                    else
                        count := 0;

                    for b := Low ( Data ) to High ( Data ) do
                    begin
                        if ( ShowProgress = True ) and ( count > 0 ) then
                        begin
                            lblProgress.Caption := 'Replicando linhas (' + IntToStr ( b ) + '/' + IntToStr ( count ) + ') ...';
                            Application.ProcessMessages;
                        end;

                        if ( IndexVar >= 0 ) and ( k > 0 ) then
                        begin
                            table.InsertRows ( ( IndexVar + 1 ) , 1 , PrimaryIndex , False ) ;

                            for j := 0 to ( table.Rows[ ( IndexVar + 1 ) ] .Count - 1 ) do
                            begin
                                if table.Cells[ ( IndexVar + 1 ) , j ] <> nil then
                                    table.Cells[ ( IndexVar + 1 ) , j ] .Clear;
                            end;

                            { REPLICAR OS VALORES E O ESTILO DA PRIMEIRA LINHA }
                            for j := Low ( columns ) to High ( columns ) do
                            begin
                                if ( Assigned ( columns[ j ] ) = True ) and ( table.Cells[ ( IndexVar + 1 ) , j ] <> nil ) then
                                begin
                                    columns[ j ] .Position := 0;
                                    table.Cells[ ( IndexVar + 1 ) , j ] .Edit ( ) ;
                                    Editor.RichViewEdit.TopLevelEditor.RVFOptions := Editor.RichViewEdit.TopLevelEditor.RVFOptions - [ rvfoLoadDocProperties ] ;
                                    Editor.RichViewEdit.TopLevelEditor.LoadRVFFromStream ( columns[ j ] ) ;
                                    Editor.RichViewEdit.TopLevelEditor.RVFOptions := Editor.RichViewEdit.TopLevelEditor.RVFOptions + [ rvfoLoadDocProperties ] ;
                                end;
                            end;
                            { - }

                            Inc ( IndexVar ) ;
                        end;

                        Editor.Format;
                        CustomFillFields ( Editor.RichViewEdit.RVData , Data[ b ] ) ;
                        Editor.Format;

                        Inc ( k ) ;
                    end;
                end;

                for j := Low ( columns ) to High ( columns ) do
                begin
                    if Assigned ( columns[ j ] ) = True then
                    begin
                        columns[ j ] .Free;
                        columns[ j ] := nil;
                    end;
                end;

                SetLength ( columns , 0 ) ;
            end;
        end;

        Editor.Format;
    finally
        ClearArrayList ( Data ) ;
    end;
end;
How to use:

Code: Select all

ReplicaLinhasTabela ( SRichViewEdit , QueryToArrayList ( memTextoTeste , 'descricao=TITULO_VARIAVEL|variavel=VARIAVEL' ) , False , nil ) ;
But when you have a table inside another table, it replicates wrong. Look (In green it would be correct):
Image

How can I adapt my role?

Thanks!
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Cloning line from the table that is inside another table

Post by Sergey Tkachenko »

Do I understand correctly: the problem is in finding a table for cloning?

But really, how do you define this table?
What if the table contains several nested tables in different cells?
Ceprotec
Posts: 259
Joined: Thu Oct 28, 2010 6:09 pm
Contact:

Re: Cloning line from the table that is inside another table

Post by Ceprotec »

Resolved Sergey!

I adapted the function to access the first table found.

only works up to 2 tables.

Code: Select all

procedure ReplicaLinhasTabelaComTabela ( Editor : TSRichViewEdit; Data : TStringListArray ) ;
var
    ItemNo : Integer;

    IndexVar : Integer;
    PossuiVar : Boolean;
    PrimaryIndex : Integer;

    count : Integer;

    j , i , k , b , l , m , n , o , p , q : Integer;
    table , table2 : TRVTableItemInfo;

    VariablesEx : string;
    columns : array of TMemoryStream;
begin
    try
        VariablesEx := '';

        for i := Low ( Data ) to High ( Data ) do
        begin
            if Assigned ( Data[ i ] ) = True then
            begin
                for j := 0 to ( Data[ i ] .Count - 1 ) do
                    VariablesEx := iif ( VariablesEx = '' , '{' + Data[ i ] .Names[ j ] + '}' , VariablesEx + ',' + '{' + Data[ i ] .Names[ j ] + '}' ) ;
            end;
        end;

        Editor.Format;

        for ItemNo := ( Editor.RichViewEdit.ItemCount - 1 ) downto 0 do
        begin
            if Editor.RichViewEdit.GetItemStyle ( ItemNo ) = rvsTable then
            begin
                table := TRVTableItemInfo ( Editor.RichViewEdit.GetItem ( ItemNo ) ) ;

                { COPIA AS INFORMAÇÕES DA LINHA ONDE FOI ACHADO AS VARIÁVEIS }
                for j := 0 to ( table.Rows.Count - 1 ) do
                begin

                    for i := 0 to ( table.Rows[ j ] .Count - 1 ) do
                    begin
                        if ( table.Cells[ j , i ] <> nil ) and ( table.Cells[ j , i ] .GetRVData <> nil ) then
                        begin
                            for p := 0 to ( table.Cells[ j , i ] .GetRVData.ItemCount - 1 ) do
                            begin
                                if table.Cells[ j , i ] .GetItemStyle ( p ) = rvsTable then
                                begin
                                    table2 := TRVTableItemInfo ( table.Cells[ j , i ] .GetItem ( p ) ) ; //TRVTableItemInfo ( Editor.RichViewEdit.GetItem ( ItemNo ) ) ;

                                    IndexVar := -1;
                                    PossuiVar := False;

                                    {J}
                                    for l := 0 to ( table2.Rows.Count - 1 ) do
                                    begin
                                        PossuiVar := False;

                                        {i}
                                        for m := 0 to ( table2.Rows[ l ] .Count - 1 ) do
                                        begin
                                            if ( table2.Cells[ l , m ] <> nil ) and ( table2.Cells[ l , m ] .GetRVData <> nil ) then
                                            begin
                                                if VarInTable ( table2.Cells[ l , m ] .GetRVData , VariablesEx ) = True then
                                                begin
                                                    IndexVar := l;
                                                    PossuiVar := True;
                                                    Break;
                                                end;
                                            end;
                                        end;

                                        if PossuiVar = True then
                                        begin
                                            SetLength ( columns , table2.Rows[ l ] .Count ) ;

                                            for n := 0 to ( table2.Rows[ l ] .Count - 1 ) do
                                            begin
                                                if table2.Cells[ l , n ] <> nil then
                                                begin
                                                    columns[ n ] := TMemoryStream.Create;
                                                    table2.Cells[ l , n ] .Edit ( ) ;
                                                    Editor.RichViewEdit.TopLevelEditor.SaveRVFToStream ( columns[ n ] , False ) ;
                                                end;
                                            end;

                                            Break;
                                        end;
                                    end;

                                    if PossuiVar = True then
                                    begin
                                        k := 0;
                                        PrimaryIndex := IndexVar;

                                        for b := Low ( Data ) to High ( Data ) do
                                        begin
                                            Application.ProcessMessages;

                                            if ( IndexVar >= 0 ) and ( k > 0 ) then
                                            begin
                                                table2.InsertRows ( ( IndexVar + 1 ) , 1 , PrimaryIndex , False ) ;

                                                for o := 0 to ( table2.Rows[ ( IndexVar + 1 ) ] .Count - 1 ) do
                                                begin
                                                    if table2.Cells[ ( IndexVar + 1 ) , o ] <> nil then
                                                        table2.Cells[ ( IndexVar + 1 ) , o ] .Clear;
                                                end;

                                                { REPLICAR OS VALORES E O ESTILO DA PRIMEIRA LINHA }
                                                for o := Low ( columns ) to High ( columns ) do
                                                begin
                                                    if ( Assigned ( columns[ o ] ) = True ) and ( table2.Cells[ ( IndexVar + 1 ) , o ] <> nil ) then
                                                    begin
                                                        columns[ o ] .Position := 0;
                                                        table2.Cells[ ( IndexVar + 1 ) , o ] .Edit ( ) ;
                                                        Editor.RichViewEdit.TopLevelEditor.RVFOptions := Editor.RichViewEdit.TopLevelEditor.RVFOptions - [ rvfoLoadDocProperties ] ;
                                                        Editor.RichViewEdit.TopLevelEditor.LoadRVFFromStream ( columns[ o ] ) ;
                                                        Editor.RichViewEdit.TopLevelEditor.RVFOptions := Editor.RichViewEdit.TopLevelEditor.RVFOptions + [ rvfoLoadDocProperties ] ;
                                                    end;
                                                end;
                                                { - }

                                                Inc ( IndexVar ) ;
                                            end;

                                            Editor.Format;
                                            CustomFillFields ( Editor.RichViewEdit.RVData , Data[ b ] ) ;
                                            Editor.Format;

                                            Inc ( k ) ;
                                        end;
                                    end;

                                    for q := Low ( columns ) to High ( columns ) do
                                    begin
                                        if Assigned ( columns[ q ] ) = True then
                                        begin
                                            columns[ q ] .Free;
                                            columns[ q ] := nil;
                                        end;
                                    end;

                                    SetLength ( columns , 0 ) ;
                                end;
                            end;
                        end;
                    end;
                end;
                { FIM }

            end;
        end;

        Editor.Format;
    finally
        ClearArrayList ( Data ) ;
    end;
end;
Thanks Sergey!
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Cloning line from the table that is inside another table

Post by Sergey Tkachenko »

My suggestion: you can give to this table some name, and search for the table with the given name.
For example, you can use item text (the first parameter of AddItem/InsertItem, can be read using GetItemText)
Post Reply