Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / odk / examples / OLE / delphi / InsertTables / SampleCode.pas
blobf314f7ea6d29afd96de8ca54562521f5c5ad503d
1 {***********************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * the BSD license.
5 *
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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 *************************************************************************}
34 unit SampleCode;
36 interface
38 uses
39 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
40 StdCtrls, ComObj, Variants;
42 type
43 TSampleCode = class
45 function Connect() : boolean;
46 procedure Disconnect();
48 function CreateDocument(bReadOnly : boolean) : boolean;
50 procedure InsertTable(sTableName : String; dbPointer : String);
52 procedure InsertDatabaseTable(
53 oDoc : Variant;
54 sTableName : String;
55 oCursor : Variant;
56 iRows : Integer;
57 iColumns : Integer;
58 dbPointer : String );
59 function CreateTextTable(
60 oDoc : Variant;
61 oCursor : Variant;
62 sName : String;
63 iRow : Integer;
64 iColumn : Integer) : Variant;
65 function getCellContent(
66 sBookmarkName : String ) : Variant;
67 function getDatabasePointer(
68 sTableName : String;
69 sCellname : String ) : String;
70 procedure InsertBookmark(
71 oDoc : Variant;
72 oTextCursor : Variant;
73 sBookmarkName : String );
74 function CreateBookmarkName(
75 sTableName : String;
76 sCellName : String;
77 sDatabasepointer : String ) : String;
78 procedure ChangeCellContent(
79 oDoc : Variant;
80 sTableName : String;
81 sCellName : String;
82 dValue : Double );
83 function GetBookmarkFromDBPointer(
84 oDoc : Variant;
85 sBookmarkName : String) : Variant;
86 function GetBookmarkFromAdress(
87 oDoc : Variant;
88 sTableName : String;
89 sCellAdress : String) : Variant;
90 function JumpToBookmark(
91 oBookmark : Variant) : Variant;
92 function CreateUniqueTablename(oDoc : Variant) : String;
94 private
95 StarOffice : Variant;
96 Document : Variant;
98 { Private-Deklarationen }
99 public
100 { Public-Deklarationen }
101 end;
103 implementation
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;
110 begin
111 if VarIsEmpty(StarOffice) then
112 StarOffice := CreateOleObject('com.sun.star.ServiceManager');
114 Connect := not (VarIsEmpty(StarOffice) or VarIsNull(StarOffice));
115 end;
117 procedure TSampleCode.Disconnect();
118 begin
119 StarOffice := Unassigned;
120 end;
122 function TSampleCode.CreateDocument(bReadOnly : boolean) : boolean;
124 StarDesktop : Variant;
125 LoadParams : Variant;
126 CoreReflection : Variant;
127 PropertyValue : Variant;
128 begin
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');
135 CoreReflection
136 .forName('com.sun.star.beans.PropertyValue')
137 .createObject(PropertyValue);
139 PropertyValue.Name := 'ReadOnly';
140 PropertyValue.Value := true;
142 LoadParams[0] := PropertyValue;
144 else
145 LoadParams := VarArrayCreate([0, -1], varVariant);
147 Document := StarDesktop.LoadComponentFromURL( 'private:factory/swriter', '_blank', 0, LoadParams);
149 CreateDocument := not (VarIsEmpty(Document) or VarIsNull(Document));
150 end;
153 function TSampleCode.getCellContent(
154 sBookmarkName : String ) : Variant;
156 oBookmark : Variant;
157 oTextCursor : Variant;
158 begin
159 oBookmark := GetBookmarkFromDBPointer( Document, sBookmarkName );
160 oTextCursor := JumpToBookmark( oBookmark );
162 getCellContent := oTextCursor.Cell.Value;
164 end;
167 function TSampleCode.getDatabasePointer(
168 sTableName : String;
169 sCellname : String ) : String;
171 oBookmark : Variant;
172 sBookmarkName : String;
173 iPos : Integer;
174 begin
175 oBookmark := GetBookmarkFromAdress( Document, sTableName, sCellName );
177 sBookmarkName := oBookmark.getName();
179 iPos := Pos('/%', sBookmarkName);
180 while Pos('/%', sBookmarkName) > 0 do
181 begin
182 iPos := Pos('/%', sBookmarkName);
183 sBookmarkName[iPos] := '%';
184 end;
186 Delete( sBookmarkName, 1, iPos+1);
187 getDatabasePointer := sBookmarkName;
188 end;
191 procedure TSampleCode.InsertTable(sTableName : String; dbPointer : String);
193 oCursor : Variant;
194 begin
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 );
205 end;
207 procedure TSampleCode.InsertDatabaseTable(
208 oDoc : Variant;
209 sTableName : String;
210 oCursor : Variant;
211 iRows : Integer;
212 iColumns : Integer;
213 dbPointer : String);
215 oTable : Variant;
216 sCellnames : Variant;
217 iCellcounter : Integer;
218 oCellCursor : Variant;
219 oTextCursor : Variant;
220 sCellName : String;
221 begin
222 oTable := CreateTextTable( oDoc, oCursor, sTableName, iRows, iColumns );
223 sCellnames := oTable.getCellNames();
225 For iCellcounter := VarArrayLowBound( sCellnames, 1) to VarArrayHighBound(sCellnames, 1) do
226 begin
227 sCellName := sCellnames[iCellcounter];
229 oCellCursor := oTable.getCellByName(sCellName);
230 oCellCursor.Value := iCellcounter;
231 oTextCursor := oCellCursor.getEnd();
232 InsertBookmark(
233 oDoc,
234 oTextCursor,
235 createBookmarkName(sTableName, sCellName, dbPointer));
236 end;
237 end;
241 ' Change the content of a cell
244 procedure TSampleCode.ChangeCellContent(
245 oDoc : Variant;
246 sTableName : String;
247 sCellName : String;
248 dValue : Double );
250 oBookmark : Variant;
251 oTextCursor : Variant;
252 sBookmarkName : String;
253 begin
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();
260 oBookmark.dispose();
261 InsertBookmark( oDoc, oTextCursor, sBookmarkName );
262 end;
265 { ' Jump to Bookmark and return for this position the cursor }
267 function TSampleCode.JumpToBookmark(
268 oBookmark : Variant) : Variant;
270 begin
271 JumpToBookmark := oBookmark.Anchor.Text.createTextCursorByRange(
272 oBookmark.Anchor );
273 end;
276 { ' Create a Texttable on a Textdocument }
277 function TSampleCode.CreateTextTable(
278 oDoc : Variant;
279 oCursor : Variant;
280 sName : String;
281 iRow : Integer;
282 iColumn : Integer) : Variant;
284 ret : Variant;
285 begin
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;
293 end;
296 { 'create a unique name for the Texttables }
297 function TSampleCode.CreateUniqueTablename(oDoc : Variant) : String;
299 iHighestNumber : Integer;
300 sTableNames : Variant;
301 iTableCounter : Integer;
302 sTableName : String;
303 iTableNumber : Integer;
304 i : Integer;
305 begin
306 sTableNames := oDoc.getTextTables.getElementNames();
307 iHighestNumber := 0;
308 For iTableCounter := VarArrayLowBound(sTableNames, 1) to VarArrayHighBound(sTableNames, 1) do
309 begin
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;
316 end;
317 createUniqueTablename := 'DBTable$$' + inttostr(iHighestNumber + 1);
318 end;
321 {' Insert a Bookmark on the cursor }
322 procedure TSampleCode.InsertBookmark(
323 oDoc : Variant;
324 oTextCursor : Variant;
325 sBookmarkName : String);
327 oBookmarkInst : Variant;
328 begin
329 oBookmarkInst := oDoc.createInstance('com.sun.star.text.Bookmark');
331 oBookmarkInst.Name := sBookmarkName;
332 oTextCursor.gotoStart( true );
333 oTextCursor.text.InsertTextContent( oTextCursor, oBookmarkInst, true );
334 end;
337 function TSampleCode.CreateBookmarkName(
338 sTableName : String;
339 sCellName : String;
340 sDatabasepointer : String ) : String;
341 begin
342 createBookmarkName := '//' + sTableName + '/%' + sCellName + '/%' + sDatabasePointer + ':' + sCellName;
343 end;
345 { ' Returns the Bookmark the Tablename and Cellname }
346 function TSampleCode.GetBookmarkFromAdress(
347 oDoc : Variant;
348 sTableName : String;
349 sCellAdress : String) : Variant;
351 sTableAddress : String;
352 iTableNameLength : Integer;
353 sBookNames : Variant;
354 iBookCounter : Integer;
355 begin
356 sTableAddress := '//' + sTableName + '/%' + sCellAdress;
357 iTableNameLength := Length( sTableAddress );
359 sBookNames := oDoc.Bookmarks.getElementNames;
361 for iBookCounter := VarArrayLowBound(sBookNames, 1) to VarArrayHighBound(sBookNames, 1) do
362 begin
363 If sTableAddress = Copy( sBookNames[iBookCounter], 1, iTableNameLength) then
364 begin
365 GetBookmarkFromAdress := oDoc.Bookmarks.getByName(sBookNames[iBookCounter]);
366 exit;
367 end;
368 end;
369 end;
371 { ' Returns the Bookmark the Tablename and Cellname }
372 function TSampleCode.GetBookmarkFromDBPointer(
373 oDoc : Variant;
374 sBookmarkName : String) : Variant;
376 sBookNames : Variant;
377 iBookCounter : Integer;
378 begin
379 sBookNames := oDoc.Bookmarks.getElementNames;
381 for iBookCounter := VarArrayLowBound(sBookNames, 1) to VarArrayHighBound(sBookNames, 1) do
382 begin
383 If Pos(sBookmarkName, sBookNames[iBookCounter]) = (1 + Length(sBookNames[iBookCounter]) - Length(sBookmarkName)) then
384 begin
385 GetBookmarkFromDBPointer := oDoc.Bookmarks.getByName(sBookNames[iBookCounter]);
386 exit;
387 end;
388 end;
389 end;
391 end.