Hi Andre, Peter, *,
Peter Eberlein escribió:
Hi Andre,
Andre Heine schrieb:
Ok, the next problem...
Am Montag, 11. Februar 2008 15:37 schrieb Andre Heine:
Am Montag, 11. Februar 2008 13:41 schrieb Andre Heine:
Reference<XTextEmbeddedObject>
object(fac->createInstance("com.sun.star.text.TextEmbeddedObject"),
UNO_QUERY);
Using XTextContent for XTextEmbeddedObject will work for me. Now,
I can see a spreadsheet in my writer component...
Ok, I can see the SpreadsheetDocument in my writer, but how can I
access them?
XTextContent has not any methods like getObject() or setObject()?
How can I get the Object from XTextContent?
An untested Java snippet:
XTextEmbeddedObjectsSupplier xes = (XTextEmbeddedObjectsSupplier)
UnoRuntime.queryInterface(XTextEmbeddedObjectsSupplier.class, document);
XNameAccess xna = xes.getEmbeddedObjects();
if (xna.hasByName("Objekt1")) {
XEmbeddedObjectSupplier xeo;
try {
embeddedObject
=(XTextContent)UnoRuntime.queryInterface(XTextContent.class,
xna.getByName("Objekt1"));
xeo = (XEmbeddedObjectSupplier)
UnoRuntime.queryInterface(XEmbeddedObjectSupplier.class, embeddedObject);
XComponent xComponent = xeo.getEmbeddedObject();
and so on...
there is no need to browse all the embedded object collection of the
document, and get it by name: you have already inserted it, so use
XEmbeddedObjectSupplier, as follows:
//*******************************************************************
#include <iostream>
#include <cppuhelper/bootstrap.hxx>
#include <rtl/ustring.hxx>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/container/XIndexAccess.hpp>
#include <com/sun/star/document/XEmbeddedObjectSupplier.hpp>
#include <com/sun/star/frame/XComponentLoader.hpp>
#include <com/sun/star/lang/XMultiComponentFactory.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
#include <com/sun/star/sheet/XSpreadsheet.hpp>
#include <com/sun/star/sheet/XSpreadsheets.hpp>
#include <com/sun/star/table/XCell.hpp>
#include <com/sun/star/table/XCellRange.hpp>
#include <com/sun/star/text/ControlCharacter.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/uno/XComponentContext.hpp>
using namespace std;
using namespace rtl;
using namespace com::sun::star::beans;
using namespace com::sun::star::container;
using namespace com::sun::star::document;
using namespace com::sun::star::frame;
using namespace com::sun::star::lang;
using namespace com::sun::star::sheet;
using namespace com::sun::star::table;
using namespace com::sun::star::text;
using namespace com::sun::star::uno;
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 );
// 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 );
// create an embedded object
// get its XTextContent interface
Reference< XTextContent > rTextContent (
rDocFactory->createInstance( OUString(
RTL_CONSTASCII_USTRINGPARAM(
"com.sun.star.text.TextEmbeddedObject" ) ) ), UNO_QUERY
);
// and its XPropertySet
Reference< XPropertySet > rPropertySet ( rTextContent,
UNO_QUERY );
// the type of embedded object is determined by the property named
"CLSID"
rPropertySet->setPropertyValue(
OUString( RTL_CONSTASCII_USTRINGPARAM( "CLSID" ) ),
makeAny( OUString( RTL_CONSTASCII_USTRINGPARAM(
"47BBB4CB-CE4C-4E80-a591-42d9ae74950f"
) ) ) );
// insert the text content
rText->insertTextContent( rTextCursor->getEnd(), rTextContent,
sal_False );
// access the component of hte embedded object
Reference< XEmbeddedObjectSupplier > rEmbeddedObjectSupplier (
rTextContent, UNO_QUERY );
Reference< XComponent > rEmbeddedObjectComponent =
rEmbeddedObjectSupplier->getEmbeddedObject();
// this component is an XSpreadsheetDocument
Reference< XSpreadsheetDocument > rSpreadsheetDocument (
rEmbeddedObjectComponent, UNO_QUERY);
// nevertheless, let's be sure...
if ( !rSpreadsheetDocument.is() )
{
cout << "Impossible to get the spreadsheet document!"
<< endl;
return 1;
}
// get the collection of sheets in the document
Reference< XSpreadsheets > rSheets =
rSpreadsheetDocument->getSheets();
// XIndexAccess to get an spreadsheet by index
Reference< XIndexAccess > rIndexedSheets( rSheets, UNO_QUERY );
// get the first sheet (index == 0)
// as XIndexAccess::getByIndex() returns an any
// we can query for a reference, like we do in Java
// Reference< XSpreadsheet > rSpreadsheet(
rIndexedSheets->getByIndex(
sal_Int32( 0 ) ), UNO_QUERY );
// but using the operator>>= is easier
Reference< XSpreadsheet > rSpreadsheet;
rIndexedSheets->getByIndex( sal_Int32( 0 ) ) >>= rSpreadsheet;
// get the cell named A1
Reference< XCell > rCellA1 = rSpreadsheet->getCellByPosition(
sal_Int32( 0 ), sal_Int32( 0 ) );
// set a very long text
rCellA1->setFormula( OUString::createFromAscii("This works!") );
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;
}
//*******************************************************************
As row C++ code is awful and hard to read in a mail, here
http://www.ArielConstenlaHaile.com.ar/ooo/temp/TextEmbeddedObject_CalcDemo.zip
is a ready to build and run version, just place the folder
TextEmbeddedObject_CalcDemo inside $OO_SDK_HOME/examples/cpp/ or simply
run make overriding the variable PRJ in the command line as follows:
shell> make PRJ=$OO_SDK_HOME
where $OO_SDK_HOME is the path to the SDK root directory, for example
shell> make PRJ=/opt/openoffice.org2.3_sdk/
In every case you MUST run FIRST the script setsdkenv_XXX on the SDK
home dir. to set your environment.
Regards
Ariel.
--
Ariel Constenla-Haile
La Plata, Argentina
[EMAIL PROTECTED]
[EMAIL PROTECTED]
http://www.ArielConstenlaHaile.com.ar/ooo/
"Aus der Kriegsschule des Lebens
- Was mich nicht umbringt,
macht mich härter."
Nietzsche Götzendämmerung, Sprüche und Pfeile, 8.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]