1 {***********************************************************************
3 * The Contents of this file are made available subject to the terms of
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *************************************************************************}
39 Windows
, Messages
, SysUtils
, Classes
, Graphics
, Controls
, Forms
, Dialogs
,
40 StdCtrls
, ComObj
, Variants
;
45 function Connect() : boolean;
46 procedure Disconnect();
48 function CreateDocument(bReadOnly
: boolean) : boolean;
50 procedure InsertTable(sTableName
: String; dbPointer
: String);
52 procedure InsertDatabaseTable(
59 function CreateTextTable(
64 iColumn
: Integer) : Variant
;
65 function getCellContent(
66 sBookmarkName
: String ) : Variant
;
67 function getDatabasePointer(
69 sCellname
: String ) : String;
70 procedure InsertBookmark(
72 oTextCursor
: Variant
;
73 sBookmarkName
: String );
74 function CreateBookmarkName(
77 sDatabasepointer
: String ) : String;
78 procedure ChangeCellContent(
83 function GetBookmarkFromDBPointer(
85 sBookmarkName
: String) : Variant
;
86 function GetBookmarkFromAdress(
89 sCellAdress
: String) : Variant
;
90 function JumpToBookmark(
91 oBookmark
: Variant
) : Variant
;
92 function CreateUniqueTablename(oDoc
: Variant
) : String;
98 { Private-Deklarationen }
100 { Public-Deklarationen }
105 { Insert a table texttable and insert in each cell a Bookmark with the address
106 of the cell and database pointer
109 function TSampleCode
.Connect() : boolean;
111 if VarIsEmpty(StarOffice
) then
112 StarOffice
:= CreateOleObject('com.sun.star.ServiceManager');
114 Connect
:= not (VarIsEmpty(StarOffice
) or VarIsNull(StarOffice
));
117 procedure TSampleCode
.Disconnect();
119 StarOffice
:= Unassigned
;
122 function TSampleCode
.CreateDocument(bReadOnly
: boolean) : boolean;
124 StarDesktop
: Variant
;
125 LoadParams
: Variant
;
126 CoreReflection
: Variant
;
127 PropertyValue
: Variant
;
129 StarDesktop
:= StarOffice
.createInstance('com.sun.star.frame.Desktop');
131 if (bReadOnly
) then begin
132 LoadParams
:= VarArrayCreate([0, 0], varVariant
);
133 CoreReflection
:= StarOffice
.createInstance('com.sun.star.reflection.CoreReflection');
136 .forName('com.sun.star.beans.PropertyValue')
137 .createObject(PropertyValue
);
139 PropertyValue
.Name
:= 'ReadOnly';
140 PropertyValue
.Value
:= true;
142 LoadParams
[0] := PropertyValue
;
145 LoadParams
:= VarArrayCreate([0, -1], varVariant
);
147 Document
:= StarDesktop
.LoadComponentFromURL( 'private:factory/swriter', '_blank', 0, LoadParams
);
149 CreateDocument
:= not (VarIsEmpty(Document
) or VarIsNull(Document
));
153 function TSampleCode
.getCellContent(
154 sBookmarkName
: String ) : Variant
;
157 oTextCursor
: Variant
;
159 oBookmark
:= GetBookmarkFromDBPointer( Document
, sBookmarkName
);
160 oTextCursor
:= JumpToBookmark( oBookmark
);
162 getCellContent
:= oTextCursor
.Cell
.Value
;
167 function TSampleCode
.getDatabasePointer(
169 sCellname
: String ) : String;
172 sBookmarkName
: String;
175 oBookmark
:= GetBookmarkFromAdress( Document
, sTableName
, sCellName
);
177 sBookmarkName
:= oBookmark
.getName();
179 iPos
:= Pos('/%', sBookmarkName
);
180 while Pos('/%', sBookmarkName
) > 0 do
182 iPos
:= Pos('/%', sBookmarkName
);
183 sBookmarkName
[iPos
] := '%';
186 Delete( sBookmarkName
, 1, iPos
+1);
187 getDatabasePointer
:= sBookmarkName
;
191 procedure TSampleCode
.InsertTable(sTableName
: String; dbPointer
: String);
195 { create a cursor object on the current position in the document }
196 oCursor
:= Document
.Text.CreateTextCursor();
198 { Create for each table a unique database name }
199 if (sTableName
= '') then
200 sTableName
:= createUniqueTablename(Document
);
202 InsertDatabaseTable( Document
, sTableName
, oCursor
, 4, 2, dbPointer
);
204 ChangeCellContent( Document
, sTableName
, 'B2', 1.12 );
207 procedure TSampleCode
.InsertDatabaseTable(
216 sCellnames
: Variant
;
217 iCellcounter
: Integer;
218 oCellCursor
: Variant
;
219 oTextCursor
: Variant
;
222 oTable
:= CreateTextTable( oDoc
, oCursor
, sTableName
, iRows
, iColumns
);
223 sCellnames
:= oTable
.getCellNames();
225 For iCellcounter
:= VarArrayLowBound( sCellnames
, 1) to VarArrayHighBound(sCellnames
, 1) do
227 sCellName
:= sCellnames
[iCellcounter
];
229 oCellCursor
:= oTable
.getCellByName(sCellName
);
230 oCellCursor
.Value
:= iCellcounter
;
231 oTextCursor
:= oCellCursor
.getEnd();
235 createBookmarkName(sTableName
, sCellName
, dbPointer
));
241 ' Change the content of a cell
244 procedure TSampleCode
.ChangeCellContent(
251 oTextCursor
: Variant
;
252 sBookmarkName
: String;
254 oBookmark
:= GetBookmarkFromAdress( oDoc
, sTableName
, sCellName
);
255 oTextCursor
:= JumpToBookmark( oBookmark
);
256 oTextCursor
.Cell
.Value
:= dValue
;
258 { create a new bookmark for the new number }
259 sBookmarkName
:= oBookmark
.getName();
261 InsertBookmark( oDoc
, oTextCursor
, sBookmarkName
);
265 { ' Jump to Bookmark and return for this position the cursor }
267 function TSampleCode
.JumpToBookmark(
268 oBookmark
: Variant
) : Variant
;
271 JumpToBookmark
:= oBookmark
.Anchor
.Text.createTextCursorByRange(
276 { ' Create a Texttable on a Textdocument }
277 function TSampleCode
.CreateTextTable(
282 iColumn
: Integer) : Variant
;
286 ret
:= oDoc
.createInstance( 'com.sun.star.text.TextTable' );
288 ret
.setName( sName
);
289 ret
.initialize( iRow
, iColumn
);
290 oDoc
.Text.InsertTextContent( oCursor
, ret
, False );
292 CreateTextTable
:= ret
;
296 { 'create a unique name for the Texttables }
297 function TSampleCode
.CreateUniqueTablename(oDoc
: Variant
) : String;
299 iHighestNumber
: Integer;
300 sTableNames
: Variant
;
301 iTableCounter
: Integer;
303 iTableNumber
: Integer;
306 sTableNames
:= oDoc
.getTextTables
.getElementNames();
308 For iTableCounter
:= VarArrayLowBound(sTableNames
, 1) to VarArrayHighBound(sTableNames
, 1) do
310 sTableName
:= sTableNames
[iTableCounter
];
311 i
:= Pos( '$$', sTableName
);
312 iTableNumber
:= strtoint( Copy(sTableName
, i
+ 2, Length( sTableName
) - i
- 1 ) );
314 If iTableNumber
> iHighestNumber
then
315 iHighestNumber
:= iTableNumber
;
317 createUniqueTablename
:= 'DBTable$$' + inttostr(iHighestNumber
+ 1);
321 {' Insert a Bookmark on the cursor }
322 procedure TSampleCode
.InsertBookmark(
324 oTextCursor
: Variant
;
325 sBookmarkName
: String);
327 oBookmarkInst
: Variant
;
329 oBookmarkInst
:= oDoc
.createInstance('com.sun.star.text.Bookmark');
331 oBookmarkInst
.Name
:= sBookmarkName
;
332 oTextCursor
.gotoStart( true );
333 oTextCursor
.text.InsertTextContent( oTextCursor
, oBookmarkInst
, true );
337 function TSampleCode
.CreateBookmarkName(
340 sDatabasepointer
: String ) : String;
342 createBookmarkName
:= '//' + sTableName
+ '/%' + sCellName
+ '/%' + sDatabasePointer
+ ':' + sCellName
;
345 { ' Returns the Bookmark the Tablename and Cellname }
346 function TSampleCode
.GetBookmarkFromAdress(
349 sCellAdress
: String) : Variant
;
351 sTableAddress
: String;
352 iTableNameLength
: Integer;
353 sBookNames
: Variant
;
354 iBookCounter
: Integer;
356 sTableAddress
:= '//' + sTableName
+ '/%' + sCellAdress
;
357 iTableNameLength
:= Length( sTableAddress
);
359 sBookNames
:= oDoc
.Bookmarks
.getElementNames
;
361 for iBookCounter
:= VarArrayLowBound(sBookNames
, 1) to VarArrayHighBound(sBookNames
, 1) do
363 If sTableAddress
= Copy( sBookNames
[iBookCounter
], 1, iTableNameLength
) then
365 GetBookmarkFromAdress
:= oDoc
.Bookmarks
.getByName(sBookNames
[iBookCounter
]);
371 { ' Returns the Bookmark the Tablename and Cellname }
372 function TSampleCode
.GetBookmarkFromDBPointer(
374 sBookmarkName
: String) : Variant
;
376 sBookNames
: Variant
;
377 iBookCounter
: Integer;
379 sBookNames
:= oDoc
.Bookmarks
.getElementNames
;
381 for iBookCounter
:= VarArrayLowBound(sBookNames
, 1) to VarArrayHighBound(sBookNames
, 1) do
383 If Pos(sBookmarkName
, sBookNames
[iBookCounter
]) = (1 + Length(sBookNames
[iBookCounter
]) - Length(sBookmarkName
)) then
385 GetBookmarkFromDBPointer
:= oDoc
.Bookmarks
.getByName(sBookNames
[iBookCounter
]);