Inserting Tables
From Apache OpenOffice Wiki
To create and insert a new text table, a five-step procedure must be followed:
- Get the service manager of the text document, querying the document's factory interface com.sun.star.lang.XMultiServiceFactory.
- Order a new text table from the factory by its service name "
com.sun.star.text.TextTable
", using the factory methodcreateInstance()
. - From the object received, query the com.sun.star.text.XTextTable interface that inherits from com.sun.star.text.XTextContent.
- If necessary, initialize the table with the number of rows and columns. For this purpose,
XTextTable
offers theinitialize()
method. - Insert the table into the text using the
insertTextContent()
method at its com.sun.star.text.XText interface. The methodinsertTextContent()
expects anXTextContent
to insert. SinceXTextTable
inherits fromXTextContent
, pass theXTextTable
interface retrieved previously.
You are now ready to get cells, fill in text, values and formulas and set the table and cell properties as needed.
In the following code sample, there is a small helper function to put random numbers between -1000 and 1000 into the table to demonstrate formulas:
/** This method returns a random double which isn't too high or too low */ protected double getRandomDouble() { return (maRandom.nextInt(1000) * maRandom.nextDouble()); }
The following helper function inserts a string into a cell known by its name and sets its text color to white:
/** This method sets the text colour of the cell refered to by sCellName to white and inserts the string sText in it */ public static void insertIntoCell(String sCellName, String sText, XTextTable xTable) { // Access the XText interface of the cell referred to by sCellName XText xCellText = (XText) UnoRuntime.queryInterface( XText.class, xTable.getCellByName(sCellName)); // create a text cursor from the cells XText interface XTextCursor xCellCursor = xCellText.createTextCursor(); // Get the property set of the cell's TextCursor XPropertySet xCellCursorProps = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, xCellCursor); try { // Set the colour of the text to white xCellCursorProps.setPropertyValue("CharColor", new Integer(16777215)); } catch (Exception e) { e.printStackTrace(System.out); } // Set the text in the cell to sText xCellText.setString(sText); }
Using the above helper functions, create a text table and insert it into the text document.
/** This method shows how to create and insert a text table, as well as insert text and formulae into the cells of the table */ protected void TextTableExample () { try { // Create a new table from the document's factory XTextTable xTable = (XTextTable) UnoRuntime.queryInterface( XTextTable.class, mxDocFactory .createInstance( "com.sun.star.text.TextTable" ) ); // Specify that we want the table to have 4 rows and 4 columns xTable.initialize( 4, 4 ); // Insert the table into the document mxDocText.insertTextContent( mxDocCursor, xTable, false); // Get an XIndexAccess of the table rows XIndexAccess xRows = xTable.getRows(); // Access the property set of the first row (properties listed in service description: // com.sun.star.text.TextTableRow) XPropertySet xRow = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xRows.getByIndex ( 0 ) ); // If BackTransparant is false, then the background color is visible xRow.setPropertyValue( "BackTransparent", Boolean.FALSE); // Specify the color of the background to be dark blue xRow.setPropertyValue( "BackColor", new Integer(6710932)); // Access the property set of the whole table XPropertySet xTableProps = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, xTable ); // We want visible background colors xTableProps.setPropertyValue( "BackTransparent", Boolean.FALSE); // Set the background colour to light blue xTableProps.setPropertyValue( "BackColor", new Integer(13421823)); // set the text (and text colour) of all the cells in the first row of the table insertIntoCell( "A1", "First Column", xTable ); insertIntoCell( "B1", "Second Column", xTable ); insertIntoCell( "C1", "Third Column", xTable ); insertIntoCell( "D1", "Results", xTable ); // Insert random numbers into the first this three cells of each // remaining row xTable.getCellByName( "A2" ).setValue( getRandomDouble() ); xTable.getCellByName( "B2" ).setValue( getRandomDouble() ); xTable.getCellByName( "C2" ).setValue( getRandomDouble() ); xTable.getCellByName( "A3" ).setValue( getRandomDouble() ); xTable.getCellByName( "B3" ).setValue( getRandomDouble() ); xTable.getCellByName( "C3" ).setValue( getRandomDouble() ); xTable.getCellByName( "A4" ).setValue( getRandomDouble() ); xTable.getCellByName( "B4" ).setValue( getRandomDouble() ); xTable.getCellByName( "C4" ).setValue( getRandomDouble() ); // Set the last cell in each row to be a formula that calculates // the sum of the first three cells xTable.getCellByName( "D2" ).setFormula( "sum <A2:C2>" ); xTable.getCellByName( "D3" ).setFormula( "sum <A3:C3>" ); xTable.getCellByName( "D4" ).setFormula( "sum <A4:C4>" ); } catch (Exception e) { e.printStackTrace ( System.out ); } }
The next sample inserts auto text entries into a table, splitting cells during its course.
/** This example demonstrates the use of the AutoTextContainer, AutoTextGroup and AutoTextEntry services and shows how to create, insert and modify auto text blocks */ protected void AutoTextExample () { try { // Go to the end of the document mxDocCursor.gotoEnd( false ); // Insert two paragraphs mxDocText.insertControlCharacter ( mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false ); mxDocText.insertControlCharacter ( mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false ); // Position the cursor in the second paragraph XParagraphCursor xParaCursor = (XParagraphCursor) UnoRuntime.queryInterface( XParagraphCursor.class, mxDocCursor ); xParaCursor.gotoPreviousParagraph ( false ); // Get an XNameAccess interface to all auto text groups from the document factory XNameAccess xContainer = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, mxFactory.createInstance ( "com.sun.star.text.AutoTextContainer" ) ); // Create a new table at the document factory XTextTable xTable = (XTextTable) UnoRuntime.queryInterface( XTextTable.class, mxDocFactory .createInstance( "com.sun.star.text.TextTable" ) ); // Store the names of all auto text groups in an array of strings String[] aGroupNames = xContainer.getElementNames(); // Make sure we have at least one group name if ( aGroupNames.length > 0 ) { // initialise the table to have a row for every autotext group //in a single column + one // additional row for a header xTable.initialize( aGroupNames.length+1,1); // Access the XPropertySet of the table XPropertySet xTableProps = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, xTable ); // We want a visible background xTableProps.setPropertyValue( "BackTransparent", Boolean.FALSE); // We want the background to be light blue xTableProps.setPropertyValue( "BackColor", new Integer(13421823)); // Insert the table into the document mxDocText.insertTextContent( mxDocCursor, xTable, false); // Get an XIndexAccess to all table rows XIndexAccess xRows = xTable.getRows(); // Get the first row in the table XPropertySet xRow = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xRows.getByIndex ( 0 ) ); // We want the background of the first row to be visible too xRow.setPropertyValue( "BackTransparent", Boolean.FALSE); // And let's make it dark blue xRow.setPropertyValue( "BackColor", new Integer(6710932)); // Put a description of the table contents into the first cell insertIntoCell( "A1", "AutoText Groups", xTable); // Create a table cursor pointing at the second cell in the first column XTextTableCursor xTableCursor = xTable.createCursorByCellName ( "A2" ); // Loop over the group names for ( int i = 0 ; i < aGroupNames.length ; i ++ ) { // Get the name of the current cell String sCellName = xTableCursor.getRangeName (); // Get the XText interface of the current cell XText xCellText = (XText) UnoRuntime.queryInterface ( XText.class, xTable.getCellByName ( sCellName ) ); // Set the cell contents of the current cell to be //the name of the of an autotext group xCellText.setString ( aGroupNames[i] ); // Access the autotext group with this name XAutoTextGroup xGroup = ( XAutoTextGroup ) UnoRuntime.queryInterface ( XAutoTextGroup.class,xContainer.getByName(aGroupNames[i])); // Get the titles of each autotext block in this group String [] aBlockNames = xGroup.getTitles(); // Make sure that the autotext group contains at least one block if ( aBlockNames.length > 0 ) { // Split the current cell vertically into two seperate cells xTableCursor.splitRange ( (short) 1, false ); // Put the cursor in the newly created right hand cell // and select it xTableCursor.goRight ( (short) 1, false ); // Split this cell horizontally to make a seperate cell // for each Autotext block if ( ( aBlockNames.length -1 ) > 0 ) xTableCursor.splitRange ( (short) (aBlockNames.length - 1), true ); // loop over the block names for ( int j = 0 ; j < aBlockNames.length ; j ++ ) { // Get the XText interface of the current cell xCellText = (XText) UnoRuntime.queryInterface ( XText.class, xTable.getCellByName ( xTableCursor.getRangeName() ) ); // Set the text contents of the current cell to the // title of an Autotext block xCellText.setString ( aBlockNames[j] ); // Move the cursor down one cell xTableCursor.goDown( (short)1, false); } } // Go back to the cell we originally split xTableCursor.gotoCellByName ( sCellName, false ); // Go down one cell xTableCursor.goDown( (short)1, false); } XAutoTextGroup xGroup; String [] aBlockNames; // Add a depth so that we only generate 200 numbers before // giving up on finding a random autotext group that contains autotext blocks int nDepth = 0; do { // Generate a random, positive number which is lower than // the number of autotext groups int nRandom = maRandom.nextInt( aGroupNames.length ); // Get the autotext group at this name xGroup = ( XAutoTextGroup ) UnoRuntime.queryInterface ( XAutoTextGroup.class, xContainer.getByName ( aGroupNames[ nRandom ] ) ); // Fill our string array with the names of all the blocks in this // group aBlockNames = xGroup.getElementNames(); // increment our depth counter ++nDepth; } while ( nDepth < 200 && aBlockNames.length == 0 ); // If we managed to find a group containg blocks... if ( aBlockNames.length > 0 ) { // Pick a random block in this group and get it's // XAutoTextEntry interface int nRandom = maRandom.nextInt( aBlockNames.length ); XAutoTextEntry xEntry = ( XAutoTextEntry ) UnoRuntime.queryInterface ( XAutoTextEntry.class, xGroup.getByName ( aBlockNames[ nRandom ] ) ); // insert the modified autotext block at the end of the document xEntry.applyTo ( mxDocCursor ); // Get the titles of all text blocks in this AutoText group String [] aBlockTitles = xGroup.getTitles(); // Get the XNamed interface of the autotext group XNamed xGroupNamed = ( XNamed ) UnoRuntime.queryInterface ( XNamed.class, xGroup ); // Output the short cut and title of the random block //and the name of the group it's from System.out.println ( "Inserted the Autotext '" + aBlockTitles[nRandom] + "', shortcut '" + aBlockNames[nRandom] + "' from group '" + xGroupNamed.getName() ); } } // Go to the end of the document mxDocCursor.gotoEnd( false ); // Insert new paragraph mxDocText.insertControlCharacter ( mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false ); // Position cursor in new paragraph xParaCursor.gotoPreviousParagraph ( false ); // Insert a string in the new paragraph mxDocText.insertString ( mxDocCursor, "Some text for a new autotext block", false ); // Go to the end of the document mxDocCursor.gotoEnd( false ); } catch (Exception e) { e.printStackTrace ( System.out ); } }
Content on this page is licensed under the Public Documentation License (PDL). |