Getting TextTable's TextRange

Discussions about using 3rd party extension with OpenOffice.org
Post Reply
b4zic
Posts: 3
Joined: Wed Mar 20, 2019 3:44 pm

Getting TextTable's TextRange

Post by b4zic »

Greetings.

I am trying to write a program, that would take an .odt document, containing tables, modify the tables & replace them with the modified version of the table and save the document, in Java.
I found a way to get access to the tables through XTextTableSupplier class, now having the collection of XTextTable elements. I've created new tables to replace the ones currently in template document. The problem comes from the question of positioning the tables exactly on the same spots as the old ones.

Clearly, the most obvious solution is to fully erase old ones (i solved this problem through getting a table as an XTextContent and calling a removeTextContent() method on the XText representation of the template file's main body text). So the last task left is to paste the new tables in place of the deleted ones. For that I need the XTextRange.


I have been trying obtaining the XTextRange of a table, it turns out (as XTextTable interface inherits XTextContent interface, which has the .getAnchor() method, which returns this TextContent's TextRange), TextTable does not get the Anchor correctly for it to be used in insertTextContent() method of XText. There is the question #1 - is there a way to get XTextRanges of all the tables in the document? If not, the question #2 - is there a way to get XTextRange of right before the table, so I would get it, and when the old ones are deleted, simply pasted the new ones there?
OpenOffice 3.1 on Windows 10/ NeoOffice 2.2.3 with MacOS 10.4 / OpenOffice 2.4 on Ubuntu 9.04
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Getting TextTable's TextRange

Post by FJCC »

I tried to find a way to replace a table with a new table at the same location and I failed. Might it work to modify the existing table in place? A table has methods to get the collections of rows and columns (getRows() and getColumns()) and those objects have methods to insertByIndex() and removeByIndex(). You can also manipulate the cell contents through the Data or DataArray properties of the table.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Getting TextTable's TextRange

Post by JeJe »

I don't know Java but testing in Basic with a table amidst some text, the only unexpected step appears to be to move the cursor down 1 line after getanchor

Code: Select all


ovc =thiscomponent.currentcontroller.getviewcursor

a= thiscomponent.texttables.getbyindex(0).getanchor.getstart
ovc.gotorange(a,false)
ovc.godown 1,false
thiscomponent.texttables.getbyindex(0).dispose

Table = thiscomponent.createInstance("com.sun.star.text.TextTable")
Table.initialize(2, 3)
 
thiscomponent.Text.insertTextContent(ovc, Table, False)

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
b4zic
Posts: 3
Joined: Wed Mar 20, 2019 3:44 pm

Re: Getting TextTable's TextRange

Post by b4zic »

JeJe wrote:I don't know Java but testing in Basic with a table amidst some text, the only unexpected step appears to be to move the cursor down 1 line after getanchor

Code: Select all


ovc =thiscomponent.currentcontroller.getviewcursor

a= thiscomponent.texttables.getbyindex(0).getanchor.getstart
ovc.gotorange(a,false)
ovc.godown 1,false
thiscomponent.texttables.getbyindex(0).dispose

Table = thiscomponent.createInstance("com.sun.star.text.TextTable")
Table.initialize(2, 3)
 
thiscomponent.Text.insertTextContent(ovc, Table, False)

Thank you for your reply. As a newbie in openoffice apache api, I have encountered several misunderstandings, trying to comprehend this style of code, reworking it in the java framework, so I will ask this for my own sake, and the sake of noobs like me who read this but are fearful to be as bold as I am.

1) "ovc =thiscomponent.currentcontroller.getviewcursor" - does this line imply, that ovc is an instance of XTextViewCursor (when the most straightforward way to get one is calling .getViewCursor on an instance of XTextViewCursorSupplier (which gets built with the loaded document representation))?
2) "a= thiscomponent.texttables.getbyindex(0).getanchor.getstart" - what is happening here is we get an instance of TextTable (in Java context, it would be wrapped in an XTextTable interface imprementator instance), and we simply get to the anchor of this XTextTable (which extends XTextContent), which is simply XTextRange, then we use .getStart on XTextRange to get another XTextRange, representing the very beginning of the table (not quite sure, as it is still an XTextRange, does it have both start and end being equal, equal to the "position" of the first cell?)
3) "ovc.gotorange(a,false)" - here, we move the cursor (being the 2-value (the start of the "selection", and the end of it) data structure (in terms of data responsible for position in the text representation)) to that exact textRange, that we have obtained through getting the table's anchor textRange modified. And now, both values of the cursor are equal to that "very beginning of the table".
4)"ovc.godown(1,false)" - here we move the cursor in the text itself, so it goes out of bounds of the table, and not considered "The XTextRange (as XTextViewCursor extends it) gotten from XTextTable through .getAnchor() (the bad type of XTextRange, which does not allow tables to be inserted by it)"?
5)"thiscomponent.texttables.getbyindex(0).dispose" - simply deleting the table from loaded doc. In Java API it would be achieved through getting XTable interface through getting the actual TextTable as an Object through .getTextTables() method of the XTextTablesSupplier (which gets generated through the use of actual loaded document), and then generating the XTable through UNORuntime.queryInterface(), then calling .dispose() on it?
6)"Table = thiscomponent.createInstance("com.sun.star.text.TextTable")
Table.initialize(2, 3)" - generating and initializing the TextTable
7)"thiscomponent.Text.insertTextContent(ovc, Table, False)" - inserting the table to non-table XTextRange in the loaded doc

Is this the correct representation of what's going on in your text (pay little attention to the Java implementation details, what matters - is if I've grasped the concepts correctly)

A great thank you for your time reading this, please reply.
OpenOffice 3.1 on Windows 10/ NeoOffice 2.2.3 with MacOS 10.4 / OpenOffice 2.4 on Ubuntu 9.04
b4zic
Posts: 3
Joined: Wed Mar 20, 2019 3:44 pm

Re: Getting TextTable's TextRange

Post by b4zic »

FJCC wrote:I tried to find a way to replace a table with a new table at the same location and I failed. Might it work to modify the existing table in place? A table has methods to get the collections of rows and columns (getRows() and getColumns()) and those objects have methods to insertByIndex() and removeByIndex(). You can also manipulate the cell contents through the Data or DataArray properties of the table.
Truthful, but the question is - when I get to the XTextTable document, and get the rows, then get the cell & modify it's content, for example, do I simply save the document as, say, "template2.odt" (when I loaded the "template1.odt"), and the changes will be reflected in "template2.odt"? Or, if not, how do I "get a reference (pointer, link)" to the table, so my modifications WOULD be reflected in the output .odt document?
OpenOffice 3.1 on Windows 10/ NeoOffice 2.2.3 with MacOS 10.4 / OpenOffice 2.4 on Ubuntu 9.04
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Getting TextTable's TextRange

Post by JeJe »

1) yes, this is getting the XTextViewCursor

2) The anchor isn't the start of the table. Its the place in the document where the table is anchored. You can see this more easily with a frame than a text table. Insert a frame into your document and you can see the little anchor picture - its not in the frame or the start of it.

3) That line sets the range of the view cursor to the start of the anchor's range (again the place in the document where the table is anchored, not the start of the table)

4) Did you test the code and see if it works in your document? Easy in basic with the IDE. Copy into module and press F5. One way to see what is happening is if you comment out all the lines with a ' at the beginning of them all Then uncomment each line one at a time and run the code you can see what's happening at each step.

I just noticed that moving the view cursor down a line at that point seems to work as a totally ad hoc fix for the simple case I tested for.

5)Yes, removing the table.

6) Yes

7)Yes

When you create a document from a template it includes the tables.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply