[Solved] Nested table - openoffice writer java API

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
liquid
Posts: 5
Joined: Fri Aug 22, 2014 5:27 pm

[Solved] Nested table - openoffice writer java API

Post by liquid »

I want to create nested table by using JAVA api in OpenOffice writer. I have tried many ways so far but didn't get success.But by using the following code, I could insert one table in an existing table cell. But, this happens only once. Say,I have a parent table called T1 and I want to insert one child table (T2) in each cell of the T1. By using the following code, I can insert T2 only once in one cell only.After that, it simply throws the exception.

//create instance of a text table - Child table
//////////////////////////////////////////////////////////////////
com.sun.star.text.XTextTable xTT0 = null;

try {
com.sun.star.lang.XMultiServiceFactory xDocMSF =
UnoRuntime.queryInterface(
com.sun.star.lang.XMultiServiceFactory.class, xTemplateComponent);

Object oInt0 = xDocMSF.createInstance("com.sun.star.text.TextTable");
xTT0 = UnoRuntime.queryInterface(com.sun.star.text.XTextTable.class,oInt0);
} catch (Exception e) {
System.err.println("Couldn't create instance "+ e);
e.printStackTrace(System.err);
}

//initialize the text table with 2 columns an 2 rows
xTT0.initialize(2,2);

////////////////////////////////////////////////
Now, say I want to insert the above table in an existing table (xTextTable), in two positions by using the following code:-

try {

XCellRange xCellRangeHeader = (XCellRange) UnoRuntime.queryInterface(
XCellRange.class, xTextTable);

xCell = xCellRangeHeader.getCellByPosition(1, 3);

XText xCellText0 = (XText) UnoRuntime.queryInterface(XText.class, xCell);

xCellText0.insertTextContent(xCellText0.createTextCursor().getEnd(), xTT0, false);

xCell = xCellRangeHeader.getCellByPosition(1, 4);

XText xCellText1 = (XText) UnoRuntime.queryInterface(XText.class, xCell);

xCellText1.insertTextContent(xCellText1.createTextCursor().getEnd(), xTT0, false);

} catch (Exception e) {
e.printStackTrace();
}

//////////////////////////////////////////////////////////////////

When I execute my code, it inserts one child table in (1,3) position but when it tries to insert in (1,4) or in any other cell it gives the following exception:-
com.sun.star.uno.RuntimeException: SwXTextTable: already attached to range.

Please help.
Last edited by RoryOF on Sun Sep 07, 2014 5:06 pm, edited 1 time in total.
Reason: Added [Solved] and green tick. [RoryOF, Moderator]
OpenOffice 3.1 on Windows Vista
User avatar
karolus
Volunteer
Posts: 1160
Joined: Sat Jul 02, 2011 9:47 am

Re: Nested table - openoffice writer java API

Post by karolus »

Only to make your mess readable with

Code: Select all

[code=php]
tags[/code]:
liquid wrote:I want to create nested table by using JAVA api in OpenOffice writer. I have tried many ways so far but didn't get success.But by using the following code, I could insert one table in an existing table cell. But, this happens only once. Say,I have a parent table called T1 and I want to insert one child table (T2) in each cell of the T1. By using the following code, I can insert T2 only once in one cell only.After that, it simply throws the exception.

Code: Select all

        //create instance of a text table - Child table
//////////////////////////////////////////////////////////////////
        com.sun.star.text.XTextTable xTT0 = null;

        try {
            com.sun.star.lang.XMultiServiceFactory xDocMSF =
                    UnoRuntime.queryInterface(
                    com.sun.star.lang.XMultiServiceFactory.class, xTemplateComponent);

            Object oInt0 = xDocMSF.createInstance("com.sun.star.text.TextTable");
            xTT0 = UnoRuntime.queryInterface(com.sun.star.text.XTextTable.class,oInt0);
        } catch (Exception e) {
            System.err.println("Couldn't create instance "+ e);
            e.printStackTrace(System.err);
        }

        //initialize the text table with 2 columns an 2 rows
        xTT0.initialize(2,2);

////////////////////////////////////////////////
liquid wrote: Now, say I want to insert the above table in an existing table (xTextTable), in two positions by using the following code:-

Code: Select all

        try {

        XCellRange xCellRangeHeader = (XCellRange) UnoRuntime.queryInterface(
                XCellRange.class, xTextTable);

            xCell = xCellRangeHeader.getCellByPosition(1, 3);

            XText xCellText0 = (XText) UnoRuntime.queryInterface(XText.class, xCell);     

            xCellText0.insertTextContent(xCellText0.createTextCursor().getEnd(), xTT0, false);
            
            xCell = xCellRangeHeader.getCellByPosition(1, 4);

            XText xCellText1 = (XText) UnoRuntime.queryInterface(XText.class, xCell);     

            xCellText1.insertTextContent(xCellText1.createTextCursor().getEnd(), xTT0, false);            

        } catch (Exception e) {
            e.printStackTrace();
        }
    
//////////////////////////////////////////////////////////////////
liquid wrote: When I execute my code, it inserts one child table in (1,3) position but when it tries to insert in (1,4) or in any other cell it gives the following exception:-
com.sun.star.uno.RuntimeException: SwXTextTable: already attached to range.

Please help.
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
B Marcelly
Volunteer
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Nested table - openoffice writer java API

Post by B Marcelly »

You must create an instance of a text table each time you want to insert a new table.
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
liquid
Posts: 5
Joined: Fri Aug 22, 2014 5:27 pm

Re: Nested table - openoffice writer java API

Post by liquid »

Hi Bernard,

Thanks!!
OpenOffice 3.1 on Windows Vista
liquid
Posts: 5
Joined: Fri Aug 22, 2014 5:27 pm

Re: Nested table - openoffice writer java API

Post by liquid »

liquid wrote:Hi Bernard,

Thanks!!
:D
OpenOffice 3.1 on Windows Vista
Post Reply