1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 {***********************************************************************
23  *
24  *  The Contents of this file are made available subject to the terms of
25  *  the BSD license.
26  *
27  *  Copyright 2000, 2010 Oracle and/or its affiliates.
28  *  All rights reserved.
29  *
30  *  Redistribution and use in source and binary forms, with or without
31  *  modification, are permitted provided that the following conditions
32  *  are met:
33  *  1. Redistributions of source code must retain the above copyright
34  *     notice, this list of conditions and the following disclaimer.
35  *  2. Redistributions in binary form must reproduce the above copyright
36  *     notice, this list of conditions and the following disclaimer in the
37  *     documentation and/or other materials provided with the distribution.
38  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
39  *     contributors may be used to endorse or promote products derived
40  *     from this software without specific prior written permission.
41  *
42  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
43  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
44  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
45  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
46  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
47  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
48  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
49  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
50  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
51  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
52  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53  *
54  *************************************************************************}
55 unit SampleCode;
56 
57 interface
58 
59 uses
60   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
61   StdCtrls, ComObj, Variants;
62 
63   type
64     TSampleCode = class
65 
Connectnull66     function Connect() : boolean;
67     procedure Disconnect();
68 
CreateDocumentnull69     function CreateDocument(bReadOnly : boolean) : boolean;
70 
71     procedure InsertTable(sTableName : String; dbPointer : String);
72 
73     procedure InsertDatabaseTable(
74         oDoc : Variant;
75         sTableName : String;
76         oCursor : Variant;
77         iRows : Integer;
78         iColumns : Integer;
79         dbPointer : String );
CreateTextTablenull80     function CreateTextTable(
81         oDoc : Variant;
82         oCursor : Variant;
83         sName : String;
84         iRow : Integer;
85         iColumn : Integer) : Variant;
getCellContentnull86     function getCellContent(
87         sBookmarkName : String ) : Variant;
getDatabasePointernull88     function getDatabasePointer(
89         sTableName : String;
90         sCellname : String ) : String;
91     procedure InsertBookmark(
92         oDoc : Variant;
93         oTextCursor : Variant;
94         sBookmarkName : String );
CreateBookmarkNamenull95     function CreateBookmarkName(
96         sTableName : String;
97         sCellName : String;
98         sDatabasepointer : String ) : String;
99     procedure ChangeCellContent(
100         oDoc : Variant;
101         sTableName : String;
102         sCellName : String;
103         dValue : Double );
GetBookmarkFromDBPointernull104     function GetBookmarkFromDBPointer(
105         oDoc : Variant;
106         sBookmarkName : String) : Variant;
GetBookmarkFromAdressnull107     function GetBookmarkFromAdress(
108         oDoc : Variant;
109         sTableName : String;
110         sCellAdress : String) : Variant;
JumpToBookmarknull111     function JumpToBookmark(
112         oBookmark : Variant) : Variant;
CreateUniqueTablenamenull113     function CreateUniqueTablename(oDoc : Variant) : String;
114 
115   private
116    StarOffice : Variant;
117    Document : Variant;
118 
119     { Private-Deklarationen }
120   public
121     { Public-Deklarationen }
122   end;
123 
124 implementation
125 
126 { Insert a table texttable and insert in each cell a Bookmark with the address
127   of the cell and database pointer
128 }
129 
Connectnull130 function TSampleCode.Connect() : boolean;
131 begin
132     if  VarIsEmpty(StarOffice) then
133         StarOffice := CreateOleObject('com.sun.star.ServiceManager');
134 
135     Connect := not (VarIsEmpty(StarOffice) or VarIsNull(StarOffice));
136 end;
137 
138 procedure TSampleCode.Disconnect();
139 begin
140     StarOffice := Unassigned;
141 end;
142 
TSampleCode.CreateDocumentnull143 function TSampleCode.CreateDocument(bReadOnly : boolean) : boolean;
144 var
145     StarDesktop : Variant;
146     LoadParams : Variant;
147     CoreReflection : Variant;
148     PropertyValue : Variant;
149 begin
150    StarDesktop := StarOffice.createInstance('com.sun.star.frame.Desktop');
151 
152    if (bReadOnly) then begin
153         LoadParams := VarArrayCreate([0, 0], varVariant);
154         CoreReflection := StarOffice.createInstance('com.sun.star.reflection.CoreReflection');
155 
156         CoreReflection
157                 .forName('com.sun.star.beans.PropertyValue')
158                 .createObject(PropertyValue);
159 
160         PropertyValue.Name := 'ReadOnly';
161         PropertyValue.Value := true;
162 
163         LoadParams[0] := PropertyValue;
164    end
165    else
166         LoadParams := VarArrayCreate([0, -1], varVariant);
167 
168    Document := StarDesktop.LoadComponentFromURL( 'private:factory/swriter', '_blank', 0,  LoadParams);
169 
170    CreateDocument := not (VarIsEmpty(Document) or VarIsNull(Document));
171 end;
172 
173 
getCellContentnull174 function TSampleCode.getCellContent(
175     sBookmarkName : String ) : Variant;
176 var
177     oBookmark : Variant;
178     oTextCursor : Variant;
179 begin
180     oBookmark := GetBookmarkFromDBPointer( Document, sBookmarkName );
181     oTextCursor := JumpToBookmark( oBookmark );
182 
183     getCellContent := oTextCursor.Cell.Value;
184 
185 end;
186 
187 
getDatabasePointernull188 function TSampleCode.getDatabasePointer(
189     sTableName : String;
190     sCellname : String ) : String;
191 var
192     oBookmark : Variant;
193     sBookmarkName : String;
194     iPos : Integer;
195 begin
196     oBookmark := GetBookmarkFromAdress( Document, sTableName, sCellName );
197 
198     sBookmarkName := oBookmark.getName();
199 
200     iPos := Pos('/%', sBookmarkName);
201     while Pos('/%', sBookmarkName) > 0 do
202     begin
203         iPos := Pos('/%', sBookmarkName);
204         sBookmarkName[iPos] := '%';
205     end;
206 
207     Delete( sBookmarkName, 1, iPos+1);
208     getDatabasePointer := sBookmarkName;
209 end;
210 
211 
212 procedure TSampleCode.InsertTable(sTableName : String; dbPointer : String);
213 var
214    oCursor : Variant;
215 begin
216    { create a cursor object on the current position in the document }
217    oCursor := Document.Text.CreateTextCursor();
218 
219    { Create for each table a unique database name }
220    if (sTableName = '') then
221         sTableName := createUniqueTablename(Document);
222 
223    InsertDatabaseTable( Document, sTableName, oCursor, 4, 2, dbPointer );
224 
225    ChangeCellContent( Document, sTableName, 'B2', 1.12 );
226 end;
227 
228 procedure TSampleCode.InsertDatabaseTable(
229     oDoc : Variant;
230     sTableName : String;
231     oCursor : Variant;
232     iRows : Integer;
233     iColumns : Integer;
234     dbPointer : String);
235 var
236     oTable : Variant;
237     sCellnames : Variant;
238     iCellcounter : Integer;
239     oCellCursor : Variant;
240     oTextCursor : Variant;
241     sCellName : String;
242 begin
243     oTable := CreateTextTable( oDoc, oCursor, sTableName, iRows, iColumns );
244     sCellnames := oTable.getCellNames();
245 
246     For iCellcounter := VarArrayLowBound( sCellnames, 1) to VarArrayHighBound(sCellnames, 1) do
247     begin
248         sCellName := sCellnames[iCellcounter];
249 
250     	oCellCursor := oTable.getCellByName(sCellName);
251     	oCellCursor.Value := iCellcounter;
252     	oTextCursor := oCellCursor.getEnd();
253         InsertBookmark(
254                 oDoc,
255                 oTextCursor,
256                 createBookmarkName(sTableName, sCellName, dbPointer));
257     end;
258 end;
259 
260 {
261 
262 ' Change the content of a cell
263 }
264 
265 procedure TSampleCode.ChangeCellContent(
266         oDoc : Variant;
267         sTableName : String;
268         sCellName : String;
269         dValue : Double );
270 var
271     oBookmark : Variant;
272     oTextCursor : Variant;
273     sBookmarkName : String;
274 begin
275     oBookmark := GetBookmarkFromAdress( oDoc, sTableName, sCellName );
276     oTextCursor := JumpToBookmark( oBookmark );
277     oTextCursor.Cell.Value := dValue;
278 
279     { create a new bookmark for the new number }
280     sBookmarkName := oBookmark.getName();
281     oBookmark.dispose();
282     InsertBookmark( oDoc, oTextCursor, sBookmarkName );
283 end;
284 
285 
286 { ' Jump to Bookmark and return for this position the cursor }
287 
TSampleCode.JumpToBookmarknull288 function TSampleCode.JumpToBookmark(
289         oBookmark : Variant) : Variant;
290 
291 begin
292 	JumpToBookmark := oBookmark.Anchor.Text.createTextCursorByRange(
293                 oBookmark.Anchor );
294 end;
295 
296 
297 { ' Create a Texttable on a Textdocument }
TSampleCode.CreateTextTablenull298 function TSampleCode.CreateTextTable(
299         oDoc : Variant;
300         oCursor : Variant;
301         sName : String;
302         iRow : Integer;
303         iColumn : Integer) : Variant;
304 var
305     ret : Variant;
306 begin
307     ret := oDoc.createInstance( 'com.sun.star.text.TextTable' );
308 
309     ret.setName( sName );
310     ret.initialize( iRow, iColumn );
311     oDoc.Text.InsertTextContent( oCursor, ret, False );
312 
313     CreateTextTable := ret;
314 end;
315 
316 
317 { 'create a unique name for the Texttables }
TSampleCode.CreateUniqueTablenamenull318 function TSampleCode.CreateUniqueTablename(oDoc : Variant) : String;
319 var
320     iHighestNumber : Integer;
321     sTableNames : Variant;
322     iTableCounter : Integer;
323     sTableName : String;
324     iTableNumber : Integer;
325     i : Integer;
326 begin
327     sTableNames := oDoc.getTextTables.getElementNames();
328     iHighestNumber := 0;
329     For iTableCounter := VarArrayLowBound(sTableNames, 1) to VarArrayHighBound(sTableNames, 1) do
330     begin
331     	sTableName := sTableNames[iTableCounter];
332         i := Pos( '$$', sTableName );
333     	iTableNumber := strtoint( Copy(sTableName, i + 2, Length( sTableName ) - i - 1 ) );
334 
335     	If iTableNumber > iHighestNumber then
336     		iHighestNumber := iTableNumber;
337     end;
338     createUniqueTablename := 'DBTable$$' + inttostr(iHighestNumber + 1);
339 end;
340 
341 
342 {' Insert a Bookmark on the cursor }
343 procedure TSampleCode.InsertBookmark(
344         oDoc : Variant;
345         oTextCursor : Variant;
346         sBookmarkName : String);
347 var
348     oBookmarkInst : Variant;
349 begin
350     oBookmarkInst := oDoc.createInstance('com.sun.star.text.Bookmark');
351 
352     oBookmarkInst.Name := sBookmarkName;
353     oTextCursor.gotoStart( true );
354     oTextCursor.text.InsertTextContent( oTextCursor, oBookmarkInst, true );
355 end;
356 
357 
TSampleCode.CreateBookmarkNamenull358 function TSampleCode.CreateBookmarkName(
359         sTableName : String;
360         sCellName : String;
361         sDatabasepointer : String ) : String;
362 begin
363     createBookmarkName := '//' + sTableName + '/%' + sCellName + '/%' + sDatabasePointer + ':' + sCellName;
364 end;
365 
366 { ' Returns the Bookmark the Tablename and Cellname }
TSampleCode.GetBookmarkFromAdressnull367 function TSampleCode.GetBookmarkFromAdress(
368         oDoc : Variant;
369         sTableName : String;
370         sCellAdress : String) : Variant;
371 var
372     sTableAddress : String;
373     iTableNameLength : Integer;
374     sBookNames : Variant;
375     iBookCounter : Integer;
376 begin
377     sTableAddress := '//' + sTableName + '/%' + sCellAdress;
378     iTableNameLength := Length( sTableAddress );
379 
380     sBookNames := oDoc.Bookmarks.getElementNames;
381 
382     for iBookCounter := VarArrayLowBound(sBookNames, 1) to VarArrayHighBound(sBookNames, 1) do
383     begin
384     	If sTableAddress = Copy( sBookNames[iBookCounter], 1, iTableNameLength) then
385         begin
386     		GetBookmarkFromAdress := oDoc.Bookmarks.getByName(sBookNames[iBookCounter]);
387     		exit;
388         end;
389     end;
390 end;
391 
392 { ' Returns the Bookmark the Tablename and Cellname }
GetBookmarkFromDBPointernull393 function TSampleCode.GetBookmarkFromDBPointer(
394         oDoc : Variant;
395         sBookmarkName : String) : Variant;
396 var
397     sBookNames : Variant;
398     iBookCounter : Integer;
399 begin
400     sBookNames := oDoc.Bookmarks.getElementNames;
401 
402     for iBookCounter := VarArrayLowBound(sBookNames, 1) to VarArrayHighBound(sBookNames, 1) do
403     begin
404     	If Pos(sBookmarkName, sBookNames[iBookCounter]) = (1 + Length(sBookNames[iBookCounter]) - Length(sBookmarkName)) then
405         begin
406     		GetBookmarkFromDBPointer := oDoc.Bookmarks.getByName(sBookNames[iBookCounter]);
407     		exit;
408         end;
409     end;
410 end;
411 
412 end.
413 
414 
415