Writer/API/Tables

From Apache OpenOffice Wiki
Jump to: navigation, search

Writer Icon.png

Writer Project

Please view the guidelines
before contributing.

Popular Subcategories:

Internal Documentation:

API Documentation:

Ongoing Efforts:

Sw.OpenOffice.org













Inserting Tables

To insert a table by supposing opened the Writer document, with a cursor of writing positioned where you want to insert the table, here how to insert a table of 4 lines and 4 columns with com.sun.star.text.XTextTable interface:

//Listing 28 Inserting a Table in a OOoWriter Document
// C++
// Jump to the end of the text
   xTextCursor->gotoEnd(false);
 
// Create a new text table from the document's factory
// Don't forget to add : #include <com/sun/star/text/XTextTable.hpp>
// Don't forget to add "com.sun.star.text.XTextTable \" in the makefile
// getting MSF of the document
   Reference<XMultiServiceFactory> oDocMSF (xTextDocument,UNO_QUERY);
   Reference <XTextTable> xTable (oDocMSF->createInstance(
			OUString::createFromAscii("com.sun.star.text.TextTable")),UNO_QUERY);
   if (!xTable.is()) {
	printf("Erreur creation XTextTable interface !\n");
	return 1;
   }
 
// Specify that we want the table to have 4 rows and 4 columns
   xTable->initialize(4, 4);
   xTextRange = xText->getEnd();
 
// Don't forget to add : #include <com/sun/star/text/XTextContent.hpp>
// Don't forget to add "com.sun.star.text.XTextContent \" in the makefile
   Reference <XTextContent>xTextContent (xTable,UNO_QUERY);
 
// Insert the table into the document
   xText->insertTextContent(xTextRange, xTextContent,(unsigned char) 0);

Here you can see an empty table in your document as shown below :

















As seen, the com.sun.star.text.XTextTable interface is our friend. Let's see what this interface provide through the corresponding IDL file :

//Listing 29 The XTextTable Service
// IDL
module com {  module sun {  module star {  module text { 
interface XTextTable: com::sun::star::text::XTextContent
{ 
	void initialize( [in] long nRows, 
			 [in] long nColumns ); 
	com::sun::star::table::XTableRows getRows(); 
	com::sun::star::table::XTableColumns getColumns(); 
	com::sun::star::table::XCell getCellByName( [in] string aCellName ); 
	sequence<string> getCellNames(); 
	com::sun::star::text::XTextTableCursor createCursorByCellName( [in] string aCellName );  
}; 
}; }; }; };

When reading this listing we see at means two other interesting interfaces com.sun.star.table.XCell and com.sun.star.text.XTextTableCursor.

//Listing 30 The XTextTableCursor Service
// IDL
module com {  module sun {  module star {  module text { 
interface XTextTableCursor: com::sun::star::uno::XInterface
{ 
	string getRangeName(); 
	boolean gotoCellByName( [in] string aCellName, 
			 [in] boolean bExpand ); 
	boolean goLeft( [in] short nCount, 
			 [in] boolean bExpand ); 
	boolean goRight( [in] short nCount, 
			 [in] boolean bExpand ); 
	boolean goUp( [in] short nCount, 
			 [in] boolean bExpand ); 
	boolean goDown( [in] short nCount, 
			 [in] boolean bExpand ); 
	void gotoStart( [in] boolean bExpand ); 
	void gotoEnd( [in] boolean bExpand ); 
	boolean mergeRange(); 
	boolean splitRange( [in] short nCount, 
			 [in] boolean bHorizontal );  
}; 
}; }; }; };

Navigating the Table

We see now how to navigate through the table. In our example below we shall use the getCellByName to reach a particular cell and obtain the corresponding com.sun.star.table.XCell interface.

//Listing 31 The XCell Service
// IDL
module com {  module sun {  module star {  module table {
interface XCell: com::sun::star::uno::XInterface
{
	string getFormula();
	void setFormula( [in] string aFormula );
	double getValue();
	void setValue( [in] double nValue );
	com::sun::star::table::CellContentType getType();
	long getError();
}; 
}; }; }; };

Changing Appearance

We give now a program to change the background colors in the table and to write something in the cells. It is necessary to add Listing 28 code.

//Listing 32 Changing Colors and writing in the Cells
// C++
 
// Get the property set of the text table
// Don't forget to add : using namespace com::sun::star::beans;
// Don't forget to add : #include <com/sun/star/beans/XPropertySet.hpp>
// Don't forget to add "com.sun.star.beans.XPropertySet \" in the makefile
   Reference<XPropertySet> xTableProps (xTable,UNO_QUERY);
   Any prop;
   prop <<= (sal_Bool)false;
   xTableProps->setPropertyValue(OUString::createFromAscii("BackTransparent"),prop);
   prop <<= (long)0x0000FF; // color blue
   xTableProps->setPropertyValue(OUString::createFromAscii("BackColor"),prop);
 
// I want to change the first row color
// Don't forget to add : using namespace com::sun::star::table;
// Don't forget to add : #include <com/sun/star/table/XTableRows.hpp>
// Don't forget to add "com.sun.star.table.XTableRows \" in the makefile
   Reference<XTableRows> xTableRows = xTable->getRows();
   Reference<XIndexAccess> theRows (xTableRows,UNO_QUERY);
   Reference<XPropertySet> xRowProps (theRows->getByIndex((short)0),UNO_QUERY);
   prop <<= (sal_Bool)false;
   xRowProps->setPropertyValue(OUString::createFromAscii("BackTransparent"),prop);
   prop <<= (long)0x000099;
   xRowProps->setPropertyValue(OUString::createFromAscii("BackColor"),prop);
 
// it's time to add some text now
   Reference<XCell> xCell = xTable->getCellByName(OUString::createFromAscii("A1"));
   xText = Reference<XText>(xCell,UNO_QUERY);
   xTextCursor = xText->createTextCursor();
   xTextCursor->setString(OUString::createFromAscii("Titre1"));   
   xCell = xTable->getCellByName(OUString::createFromAscii("A2"));
   xCell->setValue(17);
   xCell = xTable->getCellByName(OUString::createFromAscii("A3"));
   xCell->setValue(14);
   xCell = xTable->getCellByName(OUString::createFromAscii("A4"));
   xCell->setFormula(OUString::createFromAscii("=sum(<A2>|<A3>)"));
   xCell = xTable->getCellByName(OUString::createFromAscii("B1"));
   xText = Reference<XText>(xCell,UNO_QUERY);
   xTextCursor = xText->createTextCursor();
   Reference<XPropertySet> oCPS(xTextCursor,UNO_QUERY);
   prop <<= (long)0xFF0000; // color red  
   oCPS->setPropertyValue(OUString::createFromAscii("CharColor"),prop);
   xTextCursor->setString(OUString::createFromAscii("Titre2"));
   xCell = xTable->getCellByName(OUString::createFromAscii("B2"));
   xCell->setValue(20);
   xCell = xTable->getCellByName(OUString::createFromAscii("B3"));
   xCell->setValue(5);
   xCell = xTable->getCellByName(OUString::createFromAscii("B4"));
   xCell->setFormula(OUString::createFromAscii("=<B2>*<B3>"));

The table above shows us the result obtained with such a code :

Titre1

Titre2



17

20



14

5



31

100



See also com.sun.star.beans.XPropertySet and com.sun.star.table.XTableRows.


We want now to tackle few page properties. This is done starting with a Java program. Go back to Programming OOoWriter

Personal tools