1 {***********************************************************************
3 * $RCSfile: SampleCode.pas,v $
7 * last change: $Author: hr $ $Date: 2003-06-30 15:51:30 $
9 * The Contents of this file are made available subject to the terms of
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************}
45 Windows
, Messages
, SysUtils
, Classes
, Graphics
, Controls
, Forms
, Dialogs
,
46 StdCtrls
, ComObj
, Variants
;
51 function Connect() : boolean;
52 procedure Disconnect();
54 function CreateDocument(bReadOnly
: boolean) : boolean;
56 procedure InsertTable(sTableName
: String; dbPointer
: String);
58 procedure InsertDatabaseTable(
65 function CreateTextTable(
70 iColumn
: Integer) : Variant
;
71 function getCellContent(
72 sBookmarkName
: String ) : Variant
;
73 function getDatabasePointer(
75 sCellname
: String ) : String;
76 procedure InsertBookmark(
78 oTextCursor
: Variant
;
79 sBookmarkName
: String );
80 function CreateBookmarkName(
83 sDatabasepointer
: String ) : String;
84 procedure ChangeCellContent(
89 function GetBookmarkFromDBPointer(
91 sBookmarkName
: String) : Variant
;
92 function GetBookmarkFromAdress(
95 sCellAdress
: String) : Variant
;
96 function JumpToBookmark(
97 oBookmark
: Variant
) : Variant
;
98 function CreateUniqueTablename(oDoc
: Variant
) : String;
101 StarOffice
: Variant
;
104 { Private-Deklarationen }
106 { Public-Deklarationen }
111 { Insert a table texttable and insert in each cell a Bookmark with the address
112 of the cell and database pointer
115 function TSampleCode
.Connect() : boolean;
117 if VarIsEmpty(StarOffice
) then
118 StarOffice
:= CreateOleObject('com.sun.star.ServiceManager');
120 Connect
:= not (VarIsEmpty(StarOffice
) or VarIsNull(StarOffice
));
123 procedure TSampleCode
.Disconnect();
125 StarOffice
:= Unassigned
;
128 function TSampleCode
.CreateDocument(bReadOnly
: boolean) : boolean;
130 StarDesktop
: Variant
;
131 LoadParams
: Variant
;
132 CoreReflection
: Variant
;
133 PropertyValue
: Variant
;
135 StarDesktop
:= StarOffice
.createInstance('com.sun.star.frame.Desktop');
137 if (bReadOnly
) then begin
138 LoadParams
:= VarArrayCreate([0, 0], varVariant
);
139 CoreReflection
:= StarOffice
.createInstance('com.sun.star.reflection.CoreReflection');
142 .forName('com.sun.star.beans.PropertyValue')
143 .createObject(PropertyValue
);
145 PropertyValue
.Name
:= 'ReadOnly';
146 PropertyValue
.Value
:= true;
148 LoadParams
[0] := PropertyValue
;
151 LoadParams
:= VarArrayCreate([0, -1], varVariant
);
153 Document
:= StarDesktop
.LoadComponentFromURL( 'private:factory/swriter', '_blank', 0, LoadParams
);
155 CreateDocument
:= not (VarIsEmpty(Document
) or VarIsNull(Document
));
159 function TSampleCode
.getCellContent(
160 sBookmarkName
: String ) : Variant
;
163 oTextCursor
: Variant
;
165 oBookmark
:= GetBookmarkFromDBPointer( Document
, sBookmarkName
);
166 oTextCursor
:= JumpToBookmark( oBookmark
);
168 getCellContent
:= oTextCursor
.Cell
.Value
;
173 function TSampleCode
.getDatabasePointer(
175 sCellname
: String ) : String;
178 sBookmarkName
: String;
181 oBookmark
:= GetBookmarkFromAdress( Document
, sTableName
, sCellName
);
183 sBookmarkName
:= oBookmark
.getName();
185 iPos
:= Pos('/%', sBookmarkName
);
186 while Pos('/%', sBookmarkName
) > 0 do
188 iPos
:= Pos('/%', sBookmarkName
);
189 sBookmarkName
[iPos
] := '%';
192 Delete( sBookmarkName
, 1, iPos
+1);
193 getDatabasePointer
:= sBookmarkName
;
197 procedure TSampleCode
.InsertTable(sTableName
: String; dbPointer
: String);
201 { create a cursor object on the current position in the document }
202 oCursor
:= Document
.Text.CreateTextCursor();
204 { Create for each table a unique database name }
205 if (sTableName
= '') then
206 sTableName
:= createUniqueTablename(Document
);
208 InsertDatabaseTable( Document
, sTableName
, oCursor
, 4, 2, dbPointer
);
210 ChangeCellContent( Document
, sTableName
, 'B2', 1.12 );
213 procedure TSampleCode
.InsertDatabaseTable(
222 sCellnames
: Variant
;
223 iCellcounter
: Integer;
224 oCellCursor
: Variant
;
225 oTextCursor
: Variant
;
228 oTable
:= CreateTextTable( oDoc
, oCursor
, sTableName
, iRows
, iColumns
);
229 sCellnames
:= oTable
.getCellNames();
231 For iCellcounter
:= VarArrayLowBound( sCellnames
, 1) to VarArrayHighBound(sCellnames
, 1) do
233 sCellName
:= sCellnames
[iCellcounter
];
235 oCellCursor
:= oTable
.getCellByName(sCellName
);
236 oCellCursor
.Value
:= iCellcounter
;
237 oTextCursor
:= oCellCursor
.getEnd();
241 createBookmarkName(sTableName
, sCellName
, dbPointer
));
247 ' Change the content of a cell
250 procedure TSampleCode
.ChangeCellContent(
257 oTextCursor
: Variant
;
258 sBookmarkName
: String;
260 oBookmark
:= GetBookmarkFromAdress( oDoc
, sTableName
, sCellName
);
261 oTextCursor
:= JumpToBookmark( oBookmark
);
262 oTextCursor
.Cell
.Value
:= dValue
;
264 { create a new bookmark for the new number }
265 sBookmarkName
:= oBookmark
.getName();
267 InsertBookmark( oDoc
, oTextCursor
, sBookmarkName
);
271 { ' Jump to Bookmark and return for this position the cursor }
273 function TSampleCode
.JumpToBookmark(
274 oBookmark
: Variant
) : Variant
;
277 JumpToBookmark
:= oBookmark
.Anchor
.Text.createTextCursorByRange(
282 { ' Create a Texttable on a Textdocument }
283 function TSampleCode
.CreateTextTable(
288 iColumn
: Integer) : Variant
;
292 ret
:= oDoc
.createInstance( 'com.sun.star.text.TextTable' );
294 ret
.setName( sName
);
295 ret
.initialize( iRow
, iColumn
);
296 oDoc
.Text.InsertTextContent( oCursor
, ret
, False );
298 CreateTextTable
:= ret
;
302 { 'create a unique name for the Texttables }
303 function TSampleCode
.CreateUniqueTablename(oDoc
: Variant
) : String;
305 iHighestNumber
: Integer;
306 sTableNames
: Variant
;
307 iTableCounter
: Integer;
309 iTableNumber
: Integer;
312 sTableNames
:= oDoc
.getTextTables
.getElementNames();
314 For iTableCounter
:= VarArrayLowBound(sTableNames
, 1) to VarArrayHighBound(sTableNames
, 1) do
316 sTableName
:= sTableNames
[iTableCounter
];
317 i
:= Pos( '$$', sTableName
);
318 iTableNumber
:= strtoint( Copy(sTableName
, i
+ 2, Length( sTableName
) - i
- 1 ) );
320 If iTableNumber
> iHighestNumber
then
321 iHighestNumber
:= iTableNumber
;
323 createUniqueTablename
:= 'DBTable$$' + inttostr(iHighestNumber
+ 1);
327 {' Insert a Bookmark on the cursor }
328 procedure TSampleCode
.InsertBookmark(
330 oTextCursor
: Variant
;
331 sBookmarkName
: String);
333 oBookmarkInst
: Variant
;
335 oBookmarkInst
:= oDoc
.createInstance('com.sun.star.text.Bookmark');
337 oBookmarkInst
.Name
:= sBookmarkName
;
338 oTextCursor
.gotoStart( true );
339 oTextCursor
.text.InsertTextContent( oTextCursor
, oBookmarkInst
, true );
343 function TSampleCode
.CreateBookmarkName(
346 sDatabasepointer
: String ) : String;
348 createBookmarkName
:= '//' + sTableName
+ '/%' + sCellName
+ '/%' + sDatabasePointer
+ ':' + sCellName
;
351 { ' Returns the Bookmark the Tablename and Cellname }
352 function TSampleCode
.GetBookmarkFromAdress(
355 sCellAdress
: String) : Variant
;
357 sTableAddress
: String;
358 iTableNameLength
: Integer;
359 sBookNames
: Variant
;
360 iBookCounter
: Integer;
362 sTableAddress
:= '//' + sTableName
+ '/%' + sCellAdress
;
363 iTableNameLength
:= Length( sTableAddress
);
365 sBookNames
:= oDoc
.Bookmarks
.getElementNames
;
367 for iBookCounter
:= VarArrayLowBound(sBookNames
, 1) to VarArrayHighBound(sBookNames
, 1) do
369 If sTableAddress
= Copy( sBookNames
[iBookCounter
], 1, iTableNameLength
) then
371 GetBookmarkFromAdress
:= oDoc
.Bookmarks
.getByName(sBookNames
[iBookCounter
]);
377 { ' Returns the Bookmark the Tablename and Cellname }
378 function TSampleCode
.GetBookmarkFromDBPointer(
380 sBookmarkName
: String) : Variant
;
382 sBookNames
: Variant
;
383 iBookCounter
: Integer;
385 sBookNames
:= oDoc
.Bookmarks
.getElementNames
;
387 for iBookCounter
:= VarArrayLowBound(sBookNames
, 1) to VarArrayHighBound(sBookNames
, 1) do
389 If Pos(sBookmarkName
, sBookNames
[iBookCounter
]) = (1 + Length(sBookNames
[iBookCounter
]) - Length(sBookmarkName
)) then
391 GetBookmarkFromDBPointer
:= oDoc
.Bookmarks
.getByName(sBookNames
[iBookCounter
]);