Hi, On Thu, Sep 19, 2013 at 02:36:24PM +0300, K.Misha wrote: > Can you help me with changing column properties in text table?
The columns width is handled by the property TableColumnSeparators of the table, see http://wiki.openoffice.org/wiki/Documentation/DevGuide/Text/Text_Table_Properties An example, assuming that you have a 6 columns table, each column will have by default the same with, 16.6 %; we want the first column to be only the 10%, and the other columns have the same width; the following code snippet does this: Reference< XPropertySet > rTablePropertySet ( rTextTable, UNO_QUERY_THROW ); sal_Int16 nReltativeSum( 0 ); rTablePropertySet->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "TableColumnRelativeSum" ) ) ) >>= nReltativeSum; // by default, every column has the same width // with 6 columns, every column is 16.6 % of the column relative sum // make the first column a 10 % sal_Int16 nFirstColumn( nReltativeSum / 10 ); sal_Int16 nOtherCols( ( nReltativeSum - nFirstColumn ) / 5 ); sal_Int16 nPosition( nFirstColumn ); // in general, you get the sequence from the table's TableColumnSeparators // but as we've just created the table and know the column count, // there is no need to do so Sequence< TableColumnSeparator > aColSeparators( 5 ); aColSeparators[0].Position = nPosition; aColSeparators[0].IsVisible = sal_True; aColSeparators[1].Position = nPosition += nOtherCols; aColSeparators[1].IsVisible = sal_True; aColSeparators[2].Position = nPosition += nOtherCols; aColSeparators[2].IsVisible = sal_True; aColSeparators[3].Position = nPosition += nOtherCols; aColSeparators[3].IsVisible = sal_True; aColSeparators[4].Position = nPosition += nOtherCols; aColSeparators[4].IsVisible = sal_True; rTablePropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "TableColumnSeparators" ) ), makeAny( aColSeparators ) ); The full example is attached. Regards -- Ariel Constenla-Haile La Plata, Argentina
#include <iostream> #include <cppuhelper/bootstrap.hxx> #include <rtl/ustring.hxx> /** === begin UNO includes === **/ #include <com/sun/star/beans/PropertyValue.hpp> #include <com/sun/star/beans/XPropertySet.hpp> #include <com/sun/star/chart/XChartData.hpp> #include <com/sun/star/chart/XChartDataArray.hpp> #include <com/sun/star/chart/XChartDocument.hpp> #include <com/sun/star/document/XEmbeddedObjectSupplier.hpp> #include <com/sun/star/frame/XComponentLoader.hpp> #include <com/sun/star/lang/XComponent.hpp> #include <com/sun/star/lang/XMultiComponentFactory.hpp> #include <com/sun/star/lang/XMultiServiceFactory.hpp> #include <com/sun/star/sheet/XCellRangeData.hpp> #include <com/sun/star/sheet/XCellRangeData.hpp> #include <com/sun/star/table/XCell.hpp> #include <com/sun/star/table/XTableRows.hpp> #include <com/sun/star/text/ControlCharacter.hpp> #include <com/sun/star/text/TableColumnSeparator.hpp> #include <com/sun/star/text/XText.hpp> #include <com/sun/star/text/XTextContent.hpp> #include <com/sun/star/text/XTextCursor.hpp> #include <com/sun/star/text/XTextDocument.hpp> #include <com/sun/star/text/XTextTable.hpp> #include <com/sun/star/text/XTextTableCursor.hpp> #include <com/sun/star/uno/XComponentContext.hpp> /** === end UNO includes === **/ /** === begin UNO using === **/ using ::com::sun::star::beans::PropertyValue; using ::com::sun::star::beans::XPropertySet; using ::com::sun::star::chart::XChartData; using ::com::sun::star::chart::XChartDataArray; using ::com::sun::star::chart::XChartDocument; using ::com::sun::star::document::XEmbeddedObjectSupplier; using ::com::sun::star::frame::XComponentLoader; using ::com::sun::star::lang::XComponent; using ::com::sun::star::lang::XMultiComponentFactory; using ::com::sun::star::lang::XMultiServiceFactory; using ::com::sun::star::sheet::XCellRangeData; using ::com::sun::star::sheet::XCellRangeData; using ::com::sun::star::table::XCell; using ::com::sun::star::table::XTableRows; using ::com::sun::star::text::TableColumnSeparator; using ::com::sun::star::text::XText; using ::com::sun::star::text::XTextContent; using ::com::sun::star::text::XTextCursor; using ::com::sun::star::text::XTextDocument; using ::com::sun::star::text::XTextTable; using ::com::sun::star::text::XTextTableCursor; using ::com::sun::star::uno::Any; using ::com::sun::star::uno::Exception; using ::com::sun::star::uno::Reference; using ::com::sun::star::uno::RuntimeException; using ::com::sun::star::uno::Sequence; using ::com::sun::star::uno::UNO_QUERY_THROW; using ::com::sun::star::uno::UNO_QUERY_THROW; using ::com::sun::star::uno::XComponentContext; using ::com::sun::star::uno::XInterface; using ::com::sun::star::uno::makeAny; /** === end UNO using === **/ using namespace std; using rtl::OUString; namespace ControlCharacter = ::com::sun::star::text::ControlCharacter; int SAL_CALL main( int argc, char *argv[] ) { try { // bootstrap the office Reference< XComponentContext > rContext ( ::cppu::bootstrap() ); Reference< XMultiComponentFactory > rMCF = rContext->getServiceManager(); // instantiate the Desktop and get a reference to XComponentLoader Reference < XComponentLoader > rComponentLoader( rMCF->createInstanceWithContext( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.Desktop" ) ), rContext ), UNO_QUERY_THROW ); // load a new empty OOo Writer document Reference< XTextDocument > rTextDocument ( rComponentLoader->loadComponentFromURL( OUString( RTL_CONSTASCII_USTRINGPARAM( "private:factory/swriter" ) ), OUString( RTL_CONSTASCII_USTRINGPARAM( "_blank" ) ), 0, Sequence < ::com::sun::star::beans::PropertyValue >() ), UNO_QUERY_THROW ); // get the XText interface Reference< XText > rText = rTextDocument->getText(); // create a text cursor Reference< XTextCursor > rTextCursor = rText->createTextCursor(); rTextCursor->gotoStart( sal_False ); // insert a paragraph brake rText->insertControlCharacter( rTextCursor->getEnd(), ControlCharacter::PARAGRAPH_BREAK, sal_False ); // get the document's factory Reference< XMultiServiceFactory > rDocFactory ( rTextDocument, UNO_QUERY_THROW ); // create a text table Reference< XTextTable > rTextTable ( rDocFactory->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.text.TextTable" ) ) ), UNO_QUERY_THROW ); // initalize it with 5 rows and 6 columns rTextTable->initialize( sal_Int32( 5 ), sal_Int32( 6 ) ); // insert it in the document rText->insertTextContent( rTextCursor->getEnd(), ( Reference< XTextContent > & ) rTextTable , sal_False ); // data OUString ustrJan ( RTL_CONSTASCII_USTRINGPARAM( "January" ) ); OUString ustrFeb ( RTL_CONSTASCII_USTRINGPARAM( "February" ) ); OUString ustrMar ( RTL_CONSTASCII_USTRINGPARAM( "March" ) ); OUString ustrApr ( RTL_CONSTASCII_USTRINGPARAM( "April" ) ); OUString ustrMay ( RTL_CONSTASCII_USTRINGPARAM( "May" ) ); OUString ustrJohn ( RTL_CONSTASCII_USTRINGPARAM( "John" ) ); OUString ustrPaula ( RTL_CONSTASCII_USTRINGPARAM( "Paula" ) ); OUString ustrAlan ( RTL_CONSTASCII_USTRINGPARAM( "Alan" ) ); OUString ustrDean ( RTL_CONSTASCII_USTRINGPARAM( "Dean" ) ); Any aRow0[6] = { Any(), makeAny( ustrJan ), makeAny( ustrFeb ), makeAny( ustrMar ), makeAny( ustrApr ), makeAny( ustrMay ) }; Any aRow1[6] = { makeAny( ustrJohn ), makeAny( 5.4 ), makeAny( 6.4 ), makeAny( 3.4 ), makeAny( 5.6 ), makeAny( 9.4 ) }; Any aRow2[6] = { makeAny( ustrPaula ), makeAny( 7.8 ), makeAny( 4.5 ), makeAny( 7.0 ), makeAny( 6.1 ), makeAny( 6.1 ) }; Any aRow3[6] = { makeAny( ustrAlan ), makeAny( 3.4 ), makeAny( 6.3 ), makeAny( 4.7 ), makeAny( 5.4 ), makeAny( 3.4 ) }; Any aRow4[6] = { makeAny( ustrDean ), makeAny( 6.7 ), makeAny( 5.2 ), makeAny( 5.4 ), makeAny( 6.1 ), makeAny( 4.1 ) }; Sequence< Sequence< Any > > aDataArray( 5 ); aDataArray[0] = Sequence< Any > ( aRow0, 6 ); aDataArray[1] = Sequence< Any > ( aRow1, 6 ); aDataArray[2] = Sequence< Any > ( aRow2, 6 ); aDataArray[3] = Sequence< Any > ( aRow3, 6 ); aDataArray[4] = Sequence< Any > ( aRow4, 6 ); Reference< XCellRangeData > rCellRangeData ( rTextTable, UNO_QUERY_THROW ); rCellRangeData->setDataArray( aDataArray ); Reference< XPropertySet > rTablePropertySet ( rTextTable, UNO_QUERY_THROW ); sal_Int16 nReltativeSum( 0 ); rTablePropertySet->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "TableColumnRelativeSum" ) ) ) >>= nReltativeSum; // by default, every column has the same width // so every column is 16.6 % of the column relative sum // make the first column a 10 % sal_Int16 nFirstColumn( nReltativeSum / 10 ); sal_Int16 nOtherCols( ( nReltativeSum - nFirstColumn ) / 5 ); sal_Int16 nPosition( nFirstColumn ); Sequence< TableColumnSeparator > aColSeparators( 5 ); aColSeparators[0].Position = nPosition; aColSeparators[0].IsVisible = sal_True; aColSeparators[1].Position = nPosition += nOtherCols; aColSeparators[1].IsVisible = sal_True; aColSeparators[2].Position = nPosition += nOtherCols; aColSeparators[2].IsVisible = sal_True; aColSeparators[3].Position = nPosition += nOtherCols; aColSeparators[3].IsVisible = sal_True; aColSeparators[4].Position = nPosition += nOtherCols; aColSeparators[4].IsVisible = sal_True; rTablePropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "TableColumnSeparators" ) ), makeAny( aColSeparators ) ); /** Determines if the first row of the table should be treated as axis labels when a chart is to be created. */ rTablePropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "ChartRowAsLabel" ) ), Any( sal_True ) ); /** Determines if the first column of the table should be treated as axis labels when a chart is to be created. */ rTablePropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "ChartColumnAsLabel" ) ), Any( sal_True ) ); Reference< XTableRows > rTableRows = rTextTable->getRows(); // get the first linen (index == 0), and its XPropertySet to change its properties Reference< XPropertySet > rCellProps ( rTableRows->getByIndex( sal_Int32( 0 ) ), UNO_QUERY_THROW ); rCellProps->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "BackTransparent" ) ), Any( sal_False ) ); rCellProps->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "BackColor" ) ), makeAny( sal_Int32( 16744576 ) ) ); // create an embedded object // get its XTextContent interface Reference< XTextContent > rTextContent ( rDocFactory->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.text.TextEmbeddedObject" ) ) ), UNO_QUERY_THROW ); // and its XPropertySet Reference< XPropertySet > rPropertySet ( rTextContent, UNO_QUERY_THROW ); // the type of embedded object is determined by the property named "CLSID" rPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "CLSID" ) ), makeAny( OUString( RTL_CONSTASCII_USTRINGPARAM( "12DCAE26-281F-416F-a234-c3086127382e" ) ) ) ); // set its Width and Height rPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Width" ) ), makeAny( sal_Int32( 12000 ) ) ); rPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Height" ) ), makeAny( sal_Int32( 7000 ) ) ); rTextCursor->gotoEnd( sal_False ); // insert a paragraph brake rText->insertControlCharacter( rTextCursor->getEnd(), ControlCharacter::PARAGRAPH_BREAK, sal_False ); // insert the text content rText->insertTextContent( rTextCursor->getEnd(), rTextContent, sal_False ); // access the component of the embedded object Reference< XEmbeddedObjectSupplier > rEmbeddedObjectSupplier ( rTextContent, UNO_QUERY_THROW ); Reference< XComponent > rEmbeddedObjectComponent = rEmbeddedObjectSupplier->getEmbeddedObject(); Reference< XPropertySet > rChartProperties ( rEmbeddedObjectComponent, UNO_QUERY_THROW ); rChartProperties->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "DataSourceLabelsInFirstColumn" ) ), Any( sal_False ) ); rChartProperties->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "DataSourceLabelsInFirstRow" ) ), Any( sal_True ) ); rChartProperties->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "HasMainTitle" ) ), Any( sal_True ) ); rChartProperties->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "HasSubTitle" ) ), Any( sal_True ) ); rChartProperties->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "HasLegend" ) ), Any( sal_True ) ); Reference< XChartDocument > rChartDocument ( rEmbeddedObjectComponent, UNO_QUERY_THROW ); Reference< XPropertySet > rPropSet ( rChartDocument->getTitle(), UNO_QUERY_THROW ); rPropSet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "String" ) ), makeAny( OUString( RTL_CONSTASCII_USTRINGPARAM( "January-May" ) ) ) ); rPropSet = Reference< XPropertySet >( rChartDocument->getSubTitle(), UNO_QUERY_THROW ); rPropSet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "String" ) ), makeAny( OUString( RTL_CONSTASCII_USTRINGPARAM( "Old API Chart Test " ) ) ) ); rPropSet = Reference< XPropertySet >( rChartDocument->getDiagram(), UNO_QUERY_THROW ); rPropSet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Dim3D" ) ), Any( sal_True ) ); rPropSet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "HasYAxisGrid" ) ), Any( sal_True ) ); Reference< XChartData > rChartData = rChartDocument->getData(); Reference< XChartDataArray > rChartDataArray ( rChartData, UNO_QUERY_THROW ); // get table data as Sequence< Sequence<double> > Reference< XChartDataArray > rTableDataArray ( rTextTable, UNO_QUERY_THROW ); Sequence< Sequence<double> > aTableData = rTableDataArray->getData(); rChartDataArray->setData( aTableData ); Sequence<OUString> aMonths( 5 ); aMonths[0] = ustrJan; aMonths[1] = ustrFeb; aMonths[2] = ustrMar; aMonths[3] = ustrApr; aMonths[4] = ustrMay; rChartDataArray->setColumnDescriptions( aMonths ); Sequence<OUString> aPersons( 4 ); aPersons[0] = ustrJohn; aPersons[1] = ustrPaula; aPersons[2] = ustrAlan; aPersons[3] = ustrDean; rChartDataArray->setRowDescriptions( aPersons ); cout << "Press ENTER to finish the example"; cin.get(); } catch ( Exception &e ) { cerr << "caught UNO exception: " << OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() << '\n'; return 1; } return 0; }
pgptJgpgut1nz.pgp
Description: PGP signature