Page 1 of 1

Read ParaNo from cells

Posted: Tue Apr 22, 2025 6:19 pm
by DanielM
Hi,

I want to read data from a cell and transfer it to another cell along with Style and ParaNo. How to read ParaNo from source cell ?

Code: Select all

      for i := 0 to table.Cells[0, c].ItemCount - 1 do
      begin
         table.Cells[r, c].AddNL(table.Cells[0, c].GetItemText(i), table.Cells[0, c].GetItemStyle(i), [b]???[/b]);
      end;

Re: Read ParaNo from cells

Posted: Wed Apr 23, 2025 4:50 am
by DanielM
I found how I can read ParaNo, but now I have another problem, it adds a new line after each Item, how can I copy the cell as defined on the first row (0)?

Code: Select all

procedure CopyCell(r, c: Integer) ;
   var
      i: Integer;
   begin
      for i := 0 to table.Cells[0, c].ItemCount - 1 do
      begin
         table.Cells[r, c].AddNL(table.Cells[0, c].GetItemText(i), table.Cells[0, c].GetItemStyle(i), table.Cells[0, c].GetRVData.GetItemPara(i));
      end;
   end;
Clipboard_04-23-2025_01.png
Clipboard_04-23-2025_01.png (20 KiB) Viewed 4901 times

Re: Read ParaNo from cells

Posted: Wed Apr 23, 2025 11:49 am
by Sergey Tkachenko
Each cell initially has an empty text item.
Call Cell.Clear before Cell.AddNL.

Re: Read ParaNo from cells

Posted: Wed Apr 23, 2025 12:22 pm
by DanielM
Yes, I tried and it doesn't work, add a space after each item added to the cell in the source cell (the first row/corresponding column) is the source.
I tried with .AddItemAsIs, but it gives an error when I try to close the program, it's clear to me that something is wrong with how I did it
Can I send you the demo project to look at, it would be easier to run it I think?
Or should I put the source code here?

Re: Read ParaNo from cells

Posted: Wed Apr 23, 2025 2:17 pm
by Sergey Tkachenko
You can attach a test project here, or send it to email richviewgmailcom.
Please make a compliable project, as simple as possible.

Re: Read ParaNo from cells

Posted: Wed Apr 23, 2025 2:26 pm
by DanielM
My Project.

Regard.

Re: Read ParaNo from cells

Posted: Thu Apr 24, 2025 12:48 pm
by Sergey Tkachenko
The main problem in this code is re-adding existing items. You take an existing item (using GetItem method) and add it in another place (using AddItemAsIs or AddItem).
You cannot do it. Items are owned by the document. If an item is inserted several times, when the document is freed, the item will be freed more than once, and the application crashes.

To make it work correctly, you need to create copies of items instead.
There is an undocumented method AppendFrom, it does this work. It creates copies of items of the source document and adds them to the destination document:

Code: Select all

         /// / populate cells with Vars and text FROM THE FIRST ROW
         for r := 1 to table.RowCount - 1 do
            for c := 0 to table.ColCount - 1 do
               table.Cells[r, c].AppendFrom(table.Cells[0, c]);
If all your documents are like the sample one, you can use this solution.

Most item types can be duplicated, but not all of them.

The most universal method for copying documents is using TMemoryStream:

Code: Select all

var
   Stream: TMemoryStream;
   DummyColor: TColor;

         /// / populate cells with Vars and text FROM THE FIRST ROW
         Stream := TMemoryStream.Create;
         for c := 0 to table.ColCount - 1 do
         begin
           Stream.Clear;
           table.Cells[0, c].SaveRVFToStream(Stream, False, clNone, nil, nil);
            for r := 1 to table.RowCount - 1 do
            begin
               Stream.Position := 0;
               table.Cells[r, c].LoadRVFFromStream(Stream, DummyColor, nil, nil, nil, nil)
            end;
         end;
         Stream.Free;

Re: Read ParaNo from cells

Posted: Thu Apr 24, 2025 1:39 pm
by DanielM
Yes, I also thought that because of the items there are problems, but I thought that on Add... it basically increments items.

Method 1 works ok.
Method 2 works, but it moves the table completely into the page, part of it is removed without being in the editing area.

Thank you very much for your help.