Difference between revisions of "Writer/API/Tables"
SergeMoutou (Talk | contribs) |
m (→Changing Appearance) |
||
(21 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
− | + | {{Writer Project|Category=Writer/API}} | |
+ | __TOC__<br><br><br><br><br><br><br><br><br><br><br><br><br> | ||
+ | == 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 <idl>com.sun.star.text.XTextTable</idl> interface: | ||
+ | <syntaxhighlight lang="cpp"> | ||
+ | //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); | ||
+ | </syntaxhighlight> | ||
+ | Here you can see an empty table in your document as shown below : | ||
+ | |||
+ | <TABLE WIDTH=100% BORDER=1 BORDERCOLOR="#000000" CELLPADDING=4 CELLSPACING=0> | ||
<TR VALIGN=TOP> | <TR VALIGN=TOP> | ||
<TH WIDTH=25%> | <TH WIDTH=25%> | ||
Line 15: | Line 43: | ||
<P><BR> | <P><BR> | ||
</P> | </P> | ||
− | |||
</TH> | </TH> | ||
<TH WIDTH=25%> | <TH WIDTH=25%> | ||
Line 25: | Line 52: | ||
</P> | </P> | ||
</TH> | </TH> | ||
− | |||
</TR> | </TR> | ||
<TR VALIGN=TOP> | <TR VALIGN=TOP> | ||
Line 35: | Line 61: | ||
<P><BR> | <P><BR> | ||
</P> | </P> | ||
− | |||
</TD> | </TD> | ||
<TD WIDTH=25%> | <TD WIDTH=25%> | ||
Line 45: | Line 70: | ||
</P> | </P> | ||
</TD> | </TD> | ||
− | |||
</TR> | </TR> | ||
<TR VALIGN=TOP> | <TR VALIGN=TOP> | ||
Line 55: | Line 79: | ||
<P><BR> | <P><BR> | ||
</P> | </P> | ||
− | |||
</TD> | </TD> | ||
<TD WIDTH=25%> | <TD WIDTH=25%> | ||
Line 65: | Line 88: | ||
</P> | </P> | ||
</TD> | </TD> | ||
− | |||
</TR> | </TR> | ||
<TR VALIGN=TOP> | <TR VALIGN=TOP> | ||
Line 88: | Line 110: | ||
</TR> | </TR> | ||
</TABLE> | </TABLE> | ||
+ | As seen, the <idl>com.sun.star.text.XTextTable</idl> interface is our friend. Let's see what this interface provide through the corresponding IDL file : | ||
+ | <syntaxhighlight lang="idl"> | ||
+ | //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 ); | ||
+ | }; | ||
+ | }; }; }; }; | ||
+ | </syntaxhighlight> | ||
+ | When reading this listing we see at means two other interesting interfaces <idl>com.sun.star.table.XCell</idl> and <idl>com.sun.star.text.XTextTableCursor</idl>. | ||
+ | <syntaxhighlight lang="idl"> | ||
+ | //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 ); | ||
+ | }; | ||
+ | }; }; }; }; | ||
+ | </syntaxhighlight> | ||
− | + | == 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 <idl>com.sun.star.table.XCell</idl> interface. | |
+ | <syntaxhighlight lang="idl"> | ||
+ | //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(); | ||
+ | }; | ||
+ | }; }; }; }; | ||
+ | </syntaxhighlight> | ||
− | + | == 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. | |
− | + | <syntaxhighlight lang="cpp"> | |
− | + | //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>")); | ||
+ | </syntaxhighlight> | ||
+ | The table above shows us the result obtained with such a code : | ||
+ | |||
+ | <TABLE WIDTH=100% BORDER=1 BORDERCOLOR="#000000" CELLPADDING=4 CELLSPACING=0 BGCOLOR="#0000ff"> | ||
<TR BGCOLOR="#000099" VALIGN=TOP> | <TR BGCOLOR="#000099" VALIGN=TOP> | ||
<TH WIDTH=25%> | <TH WIDTH=25%> | ||
Line 169: | Line 302: | ||
</TR> | </TR> | ||
</TABLE> | </TABLE> | ||
+ | |||
+ | See also <idl>com.sun.star.beans.XPropertySet</idl> and <idl>com.sun.star.table.XTableRows</idl>. | ||
+ | |||
+ | <noinclude> | ||
+ | We want now to tackle few page properties. This is done starting with a Java program. Go back to [[Programming_OOoWriter#Translate_Java_Code_into_C.2B.2B_.28Page_Properties.29| Programming OOoWriter]] | ||
+ | </noinclude> | ||
+ | |||
+ | [[Category:Tutorial]] |
Latest revision as of 13:45, 5 March 2021
Please view the guidelines
|
---|
Popular Subcategories: Extension:DynamicPageList (DPL), version 2.3.0 : Warning: No results. Internal Documentation: Extension:DynamicPageList (DPL), version 2.3.0 : Warning: No results. API Documentation: Ongoing Efforts: Extension:DynamicPageList (DPL), version 2.3.0 : Warning: No results. Projects on this Wiki: (edit list)
|
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 ); }; }; }; }; };
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