Read ParaNo from cells

ScaleRichView support and discussion (TRichView add-on for WYSIWYG editing)
Post Reply
DanielM
Posts: 16
Joined: Thu Mar 27, 2025 5:31 pm

Read ParaNo from cells

Post 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;
DanielM
Posts: 16
Joined: Thu Mar 27, 2025 5:31 pm

Re: Read ParaNo from cells

Post 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 4737 times
Sergey Tkachenko
Site Admin
Posts: 17805
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Read ParaNo from cells

Post by Sergey Tkachenko »

Each cell initially has an empty text item.
Call Cell.Clear before Cell.AddNL.
DanielM
Posts: 16
Joined: Thu Mar 27, 2025 5:31 pm

Re: Read ParaNo from cells

Post 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?
Sergey Tkachenko
Site Admin
Posts: 17805
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Read ParaNo from cells

Post 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.
DanielM
Posts: 16
Joined: Thu Mar 27, 2025 5:31 pm

Re: Read ParaNo from cells

Post by DanielM »

My Project.

Regard.
Attachments
Table.zip
(60.26 KiB) Downloaded 374 times
Sergey Tkachenko
Site Admin
Posts: 17805
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Read ParaNo from cells

Post 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;
DanielM
Posts: 16
Joined: Thu Mar 27, 2025 5:31 pm

Re: Read ParaNo from cells

Post 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.
Post Reply