[Solved] Nested table - openoffice writer java API

The Application Programming Interface and the OASIS Open Document Format

[Solved] Nested table - openoffice writer java API

Postby liquid » Fri Aug 29, 2014 8:30 pm

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
liquid
 
Posts: 5
Joined: Fri Aug 22, 2014 5:27 pm

Re: Nested table - openoffice writer java API

Postby karolus » Fri Aug 29, 2014 8:40 pm

Only to make your mess readable with
Code: Select all   Expand viewCollapse view
[code=php][/code]tags
:

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   Expand viewCollapse view

        
//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   Expand viewCollapse view

        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 - 5.1 … 5.3.2.2 on Linux Mint17
User avatar
karolus
Volunteer
 
Posts: 852
Joined: Sat Jul 02, 2011 9:47 am

Re: Nested table - openoffice writer java API

Postby B Marcelly » Sat Aug 30, 2014 11:26 am

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
B Marcelly
Volunteer
 
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Nested table - openoffice writer java API

Postby liquid » Sun Sep 07, 2014 4:00 pm

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

Postby liquid » Sun Sep 07, 2014 4:04 pm

liquid wrote:Hi Bernard,

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


Return to UNO API and ODF

Who is online

Users browsing this forum: No registered users and 1 guest