1 {***********************************************************************
2  *
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.
8  *
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.
20  *
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.
32  *
33  *************************************************************************}
34 unit SampleCode;
35 
36 interface
37 
38 uses
39   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
40   StdCtrls, ComObj, Variants;
41 
42   type
43     TSampleCode = class
44 
45     function Connect() : boolean;
46     procedure Disconnect();
47 
48     function CreateDocument(bReadOnly : boolean) : boolean;
49 
50     procedure InsertTable(sTableName : String; dbPointer : String);
51 
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;
93 
94   private
95    StarOffice : Variant;
96    Document : Variant;
97 
98     { Private-Deklarationen }
99   public
100     { Public-Deklarationen }
101   end;
102 
103 implementation
104 
105 { Insert a table texttable and insert in each cell a Bookmark with the address
106   of the cell and database pointer
107 }
108 
109 function TSampleCode.Connect() : boolean;
110 begin
111     if  VarIsEmpty(StarOffice) then
112         StarOffice := CreateOleObject('com.sun.star.ServiceManager');
113 
114     Connect := not (VarIsEmpty(StarOffice) or VarIsNull(StarOffice));
115 end;
116 
117 procedure TSampleCode.Disconnect();
118 begin
119     StarOffice := Unassigned;
120 end;
121 
122 function TSampleCode.CreateDocument(bReadOnly : boolean) : boolean;
123 var
124     StarDesktop : Variant;
125     LoadParams : Variant;
126     CoreReflection : Variant;
127     PropertyValue : Variant;
128 begin
129    StarDesktop := StarOffice.createInstance('com.sun.star.frame.Desktop');
130 
131    if (bReadOnly) then begin
132         LoadParams := VarArrayCreate([0, 0], varVariant);
133         CoreReflection := StarOffice.createInstance('com.sun.star.reflection.CoreReflection');
134 
135         CoreReflection
136                 .forName('com.sun.star.beans.PropertyValue')
137                 .createObject(PropertyValue);
138 
139         PropertyValue.Name := 'ReadOnly';
140         PropertyValue.Value := true;
141 
142         LoadParams[0] := PropertyValue;
143    end
144    else
145         LoadParams := VarArrayCreate([0, -1], varVariant);
146 
147    Document := StarDesktop.LoadComponentFromURL( 'private:factory/swriter', '_blank', 0,  LoadParams);
148 
149    CreateDocument := not (VarIsEmpty(Document) or VarIsNull(Document));
150 end;
151 
152 
153 function TSampleCode.getCellContent(
154     sBookmarkName : String ) : Variant;
155 var
156     oBookmark : Variant;
157     oTextCursor : Variant;
158 begin
159     oBookmark := GetBookmarkFromDBPointer( Document, sBookmarkName );
160     oTextCursor := JumpToBookmark( oBookmark );
161 
162     getCellContent := oTextCursor.Cell.Value;
163 
164 end;
165 
166 
167 function TSampleCode.getDatabasePointer(
168     sTableName : String;
169     sCellname : String ) : String;
170 var
171     oBookmark : Variant;
172     sBookmarkName : String;
173     iPos : Integer;
174 begin
175     oBookmark := GetBookmarkFromAdress( Document, sTableName, sCellName );
176 
177     sBookmarkName := oBookmark.getName();
178 
179     iPos := Pos('/%', sBookmarkName);
180     while Pos('/%', sBookmarkName) > 0 do
181     begin
182         iPos := Pos('/%', sBookmarkName);
183         sBookmarkName[iPos] := '%';
184     end;
185 
186     Delete( sBookmarkName, 1, iPos+1);
187     getDatabasePointer := sBookmarkName;
188 end;
189 
190 
191 procedure TSampleCode.InsertTable(sTableName : String; dbPointer : String);
192 var
193    oCursor : Variant;
194 begin
195    { create a cursor object on the current position in the document }
196    oCursor := Document.Text.CreateTextCursor();
197 
198    { Create for each table a unique database name }
199    if (sTableName = '') then
200         sTableName := createUniqueTablename(Document);
201 
202    InsertDatabaseTable( Document, sTableName, oCursor, 4, 2, dbPointer );
203 
204    ChangeCellContent( Document, sTableName, 'B2', 1.12 );
205 end;
206 
207 procedure TSampleCode.InsertDatabaseTable(
208     oDoc : Variant;
209     sTableName : String;
210     oCursor : Variant;
211     iRows : Integer;
212     iColumns : Integer;
213     dbPointer : String);
214 var
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();
224 
225     For iCellcounter := VarArrayLowBound( sCellnames, 1) to VarArrayHighBound(sCellnames, 1) do
226     begin
227         sCellName := sCellnames[iCellcounter];
228 
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;
238 
239 {
240 
241 ' Change the content of a cell
242 }
243 
244 procedure TSampleCode.ChangeCellContent(
245         oDoc : Variant;
246         sTableName : String;
247         sCellName : String;
248         dValue : Double );
249 var
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;
257 
258     { create a new bookmark for the new number }
259     sBookmarkName := oBookmark.getName();
260     oBookmark.dispose();
261     InsertBookmark( oDoc, oTextCursor, sBookmarkName );
262 end;
263 
264 
265 { ' Jump to Bookmark and return for this position the cursor }
266 
267 function TSampleCode.JumpToBookmark(
268         oBookmark : Variant) : Variant;
269 
270 begin
271 	JumpToBookmark := oBookmark.Anchor.Text.createTextCursorByRange(
272                 oBookmark.Anchor );
273 end;
274 
275 
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;
283 var
284     ret : Variant;
285 begin
286     ret := oDoc.createInstance( 'com.sun.star.text.TextTable' );
287 
288     ret.setName( sName );
289     ret.initialize( iRow, iColumn );
290     oDoc.Text.InsertTextContent( oCursor, ret, False );
291 
292     CreateTextTable := ret;
293 end;
294 
295 
296 { 'create a unique name for the Texttables }
297 function TSampleCode.CreateUniqueTablename(oDoc : Variant) : String;
298 var
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 ) );
313 
314     	If iTableNumber > iHighestNumber then
315     		iHighestNumber := iTableNumber;
316     end;
317     createUniqueTablename := 'DBTable$$' + inttostr(iHighestNumber + 1);
318 end;
319 
320 
321 {' Insert a Bookmark on the cursor }
322 procedure TSampleCode.InsertBookmark(
323         oDoc : Variant;
324         oTextCursor : Variant;
325         sBookmarkName : String);
326 var
327     oBookmarkInst : Variant;
328 begin
329     oBookmarkInst := oDoc.createInstance('com.sun.star.text.Bookmark');
330 
331     oBookmarkInst.Name := sBookmarkName;
332     oTextCursor.gotoStart( true );
333     oTextCursor.text.InsertTextContent( oTextCursor, oBookmarkInst, true );
334 end;
335 
336 
337 function TSampleCode.CreateBookmarkName(
338         sTableName : String;
339         sCellName : String;
340         sDatabasepointer : String ) : String;
341 begin
342     createBookmarkName := '//' + sTableName + '/%' + sCellName + '/%' + sDatabasePointer + ':' + sCellName;
343 end;
344 
345 { ' Returns the Bookmark the Tablename and Cellname }
346 function TSampleCode.GetBookmarkFromAdress(
347         oDoc : Variant;
348         sTableName : String;
349         sCellAdress : String) : Variant;
350 var
351     sTableAddress : String;
352     iTableNameLength : Integer;
353     sBookNames : Variant;
354     iBookCounter : Integer;
355 begin
356     sTableAddress := '//' + sTableName + '/%' + sCellAdress;
357     iTableNameLength := Length( sTableAddress );
358 
359     sBookNames := oDoc.Bookmarks.getElementNames;
360 
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;
370 
371 { ' Returns the Bookmark the Tablename and Cellname }
372 function TSampleCode.GetBookmarkFromDBPointer(
373         oDoc : Variant;
374         sBookmarkName : String) : Variant;
375 var
376     sBookNames : Variant;
377     iBookCounter : Integer;
378 begin
379     sBookNames := oDoc.Bookmarks.getElementNames;
380 
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;
390 
391 end.
392 
393 
394