OOHelper.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. {***********************************************************************
  2. *
  3. * $RCSfile: SampleCode.pas,v $
  4. *
  5. * $Revision: 1.2 $
  6. *
  7. * last change: $Author: hr $ $Date: 2003/06/30 15:51:30 $
  8. *
  9. * The Contents of this file are made available subject to the terms of
  10. * the BSD license.
  11. *
  12. * Copyright (c) 2003 by Sun Microsystems, Inc.
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions
  17. * are met:
  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.
  26. *
  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.
  38. *
  39. *************************************************************************}
  40. {$mode delphi}
  41. unit OOHelper;
  42. interface
  43. uses
  44. Windows, Messages, SysUtils, Classes, Variants, ComObj;
  45. type
  46. TSampleCode = class
  47. function Connect() : boolean;
  48. procedure Disconnect();
  49. function CreateDocument(bReadOnly : boolean) : boolean;
  50. procedure InsertTable(sTableName : String; dbPointer : String);
  51. procedure InsertDatabaseTable(
  52. oDoc : Variant;
  53. sTableName : String;
  54. oCursor : Variant;
  55. iRows : Integer;
  56. iColumns : Integer;
  57. dbPointer : String );
  58. function CreateTextTable(
  59. oDoc : Variant;
  60. oCursor : Variant;
  61. sName : String;
  62. iRow : Integer;
  63. iColumn : Integer) : Variant;
  64. function getCellContent(
  65. sBookmarkName : String ) : Variant;
  66. function getDatabasePointer(
  67. sTableName : String;
  68. sCellname : String ) : String;
  69. procedure InsertBookmark(
  70. oDoc : Variant;
  71. oTextCursor : Variant;
  72. sBookmarkName : String );
  73. function CreateBookmarkName(
  74. sTableName : String;
  75. sCellName : String;
  76. sDatabasepointer : String ) : String;
  77. procedure ChangeCellContent(
  78. oDoc : Variant;
  79. sTableName : String;
  80. sCellName : String;
  81. dValue : Double );
  82. function GetBookmarkFromDBPointer(
  83. oDoc : Variant;
  84. sBookmarkName : String) : Variant;
  85. function GetBookmarkFromAdress(
  86. oDoc : Variant;
  87. sTableName : String;
  88. sCellAdress : String) : Variant;
  89. function JumpToBookmark(
  90. oBookmark : Variant) : Variant;
  91. function CreateUniqueTablename(oDoc : Variant) : String;
  92. private
  93. StarOffice : Variant;
  94. Document : Variant;
  95. { Private-Deklarationen }
  96. public
  97. { Public-Deklarationen }
  98. end;
  99. implementation
  100. { Insert a table texttable and insert in each cell a Bookmark with the address
  101. of the cell and database pointer
  102. }
  103. function TSampleCode.Connect() : boolean;
  104. begin
  105. if VarIsEmpty(StarOffice) then
  106. StarOffice := CreateOleObject('com.sun.star.ServiceManager');
  107. Connect := not (VarIsEmpty(StarOffice) or VarIsNull(StarOffice));
  108. end;
  109. procedure TSampleCode.Disconnect();
  110. begin
  111. StarOffice := Unassigned;
  112. end;
  113. function TSampleCode.CreateDocument(bReadOnly : boolean) : boolean;
  114. var
  115. StarDesktop : Variant;
  116. LoadParams : Variant;
  117. CoreReflection : Variant;
  118. PropertyValue : Variant;
  119. begin
  120. StarDesktop := StarOffice.createInstance('com.sun.star.frame.Desktop');
  121. if (bReadOnly) then begin
  122. LoadParams := VarArrayCreate([0, 0], varVariant);
  123. CoreReflection := StarOffice.createInstance('com.sun.star.reflection.CoreReflection');
  124. CoreReflection
  125. .forName('com.sun.star.beans.PropertyValue')
  126. .createObject(PropertyValue);
  127. PropertyValue.Name := 'ReadOnly';
  128. PropertyValue.Value := true;
  129. LoadParams[0] := PropertyValue;
  130. end
  131. else
  132. LoadParams := VarArrayCreate([0, -1], varVariant);
  133. Document := StarDesktop.LoadComponentFromURL( 'private:factory/swriter', '_blank', 0, LoadParams);
  134. CreateDocument := not (VarIsEmpty(Document) or VarIsNull(Document));
  135. end;
  136. function TSampleCode.getCellContent(
  137. sBookmarkName : String ) : Variant;
  138. var
  139. oBookmark : Variant;
  140. oTextCursor : Variant;
  141. begin
  142. oBookmark := GetBookmarkFromDBPointer( Document, sBookmarkName );
  143. oTextCursor := JumpToBookmark( oBookmark );
  144. getCellContent := oTextCursor.Cell.Value;
  145. end;
  146. function TSampleCode.getDatabasePointer(
  147. sTableName : String;
  148. sCellname : String ) : String;
  149. var
  150. oBookmark : Variant;
  151. sBookmarkName : String;
  152. iPos : Integer;
  153. begin
  154. oBookmark := GetBookmarkFromAdress( Document, sTableName, sCellName );
  155. sBookmarkName := oBookmark.getName();
  156. iPos := Pos('/%', sBookmarkName);
  157. while Pos('/%', sBookmarkName) > 0 do
  158. begin
  159. iPos := Pos('/%', sBookmarkName);
  160. sBookmarkName[iPos] := '%';
  161. end;
  162. Delete( sBookmarkName, 1, iPos+1);
  163. getDatabasePointer := sBookmarkName;
  164. end;
  165. procedure TSampleCode.InsertTable(sTableName : String; dbPointer : String);
  166. var
  167. oCursor : Variant;
  168. begin
  169. { create a cursor object on the current position in the document }
  170. oCursor := Document.Text.CreateTextCursor();
  171. { Create for each table a unique database name }
  172. if (sTableName = '') then
  173. sTableName := createUniqueTablename(Document);
  174. InsertDatabaseTable( Document, sTableName, oCursor, 4, 2, dbPointer );
  175. ChangeCellContent( Document, sTableName, 'B2', 1.12 );
  176. end;
  177. procedure TSampleCode.InsertDatabaseTable(
  178. oDoc : Variant;
  179. sTableName : String;
  180. oCursor : Variant;
  181. iRows : Integer;
  182. iColumns : Integer;
  183. dbPointer : String);
  184. var
  185. oTable : Variant;
  186. sCellnames : Variant;
  187. iCellcounter : Integer;
  188. oCellCursor : Variant;
  189. oTextCursor : Variant;
  190. sCellName : String;
  191. begin
  192. oTable := CreateTextTable( oDoc, oCursor, sTableName, iRows, iColumns );
  193. sCellnames := oTable.getCellNames();
  194. For iCellcounter := VarArrayLowBound( sCellnames, 1) to VarArrayHighBound(sCellnames, 1) do
  195. begin
  196. sCellName := sCellnames[iCellcounter];
  197. oCellCursor := oTable.getCellByName(sCellName);
  198. oCellCursor.Value := iCellcounter;
  199. oTextCursor := oCellCursor.getEnd();
  200. InsertBookmark(
  201. oDoc,
  202. oTextCursor,
  203. createBookmarkName(sTableName, sCellName, dbPointer));
  204. end;
  205. end;
  206. {
  207. ' Change the content of a cell
  208. }
  209. procedure TSampleCode.ChangeCellContent(
  210. oDoc : Variant;
  211. sTableName : String;
  212. sCellName : String;
  213. dValue : Double );
  214. var
  215. oBookmark : Variant;
  216. oTextCursor : Variant;
  217. sBookmarkName : String;
  218. begin
  219. oBookmark := GetBookmarkFromAdress( oDoc, sTableName, sCellName );
  220. oTextCursor := JumpToBookmark( oBookmark );
  221. oTextCursor.Cell.Value := dValue;
  222. { create a new bookmark for the new number }
  223. sBookmarkName := oBookmark.getName();
  224. oBookmark.dispose();
  225. InsertBookmark( oDoc, oTextCursor, sBookmarkName );
  226. end;
  227. { ' Jump to Bookmark and return for this position the cursor }
  228. function TSampleCode.JumpToBookmark(
  229. oBookmark : Variant) : Variant;
  230. begin
  231. JumpToBookmark := oBookmark.Anchor.Text.createTextCursorByRange(
  232. oBookmark.Anchor );
  233. end;
  234. { ' Create a Texttable on a Textdocument }
  235. function TSampleCode.CreateTextTable(
  236. oDoc : Variant;
  237. oCursor : Variant;
  238. sName : String;
  239. iRow : Integer;
  240. iColumn : Integer) : Variant;
  241. var
  242. ret : Variant;
  243. begin
  244. ret := oDoc.createInstance( 'com.sun.star.text.TextTable' );
  245. ret.setName( sName );
  246. ret.initialize( iRow, iColumn );
  247. oDoc.Text.InsertTextContent( oCursor, ret, False );
  248. CreateTextTable := ret;
  249. end;
  250. { 'create a unique name for the Texttables }
  251. function TSampleCode.CreateUniqueTablename(oDoc : Variant) : String;
  252. var
  253. iHighestNumber : Integer;
  254. sTableNames : Variant;
  255. iTableCounter : Integer;
  256. sTableName : String;
  257. iTableNumber : Integer;
  258. i : Integer;
  259. begin
  260. sTableNames := oDoc.getTextTables.getElementNames();
  261. iHighestNumber := 0;
  262. For iTableCounter := VarArrayLowBound(sTableNames, 1) to VarArrayHighBound(sTableNames, 1) do
  263. begin
  264. sTableName := sTableNames[iTableCounter];
  265. i := Pos( '$$', sTableName );
  266. iTableNumber := strtoint( Copy(sTableName, i + 2, Length( sTableName ) - i - 1 ) );
  267. If iTableNumber > iHighestNumber then
  268. iHighestNumber := iTableNumber;
  269. end;
  270. createUniqueTablename := 'DBTable$$' + inttostr(iHighestNumber + 1);
  271. end;
  272. {' Insert a Bookmark on the cursor }
  273. procedure TSampleCode.InsertBookmark(
  274. oDoc : Variant;
  275. oTextCursor : Variant;
  276. sBookmarkName : String);
  277. var
  278. oBookmarkInst : Variant;
  279. begin
  280. oBookmarkInst := oDoc.createInstance('com.sun.star.text.Bookmark');
  281. oBookmarkInst.Name := sBookmarkName;
  282. oTextCursor.gotoStart( true );
  283. oTextCursor.text.InsertTextContent( oTextCursor, oBookmarkInst, true );
  284. end;
  285. function TSampleCode.CreateBookmarkName(
  286. sTableName : String;
  287. sCellName : String;
  288. sDatabasepointer : String ) : String;
  289. begin
  290. createBookmarkName := '//' + sTableName + '/%' + sCellName + '/%' + sDatabasePointer + ':' + sCellName;
  291. end;
  292. { ' Returns the Bookmark the Tablename and Cellname }
  293. function TSampleCode.GetBookmarkFromAdress(
  294. oDoc : Variant;
  295. sTableName : String;
  296. sCellAdress : String) : Variant;
  297. var
  298. sTableAddress : String;
  299. iTableNameLength : Integer;
  300. sBookNames : Variant;
  301. iBookCounter : Integer;
  302. begin
  303. sTableAddress := '//' + sTableName + '/%' + sCellAdress;
  304. iTableNameLength := Length( sTableAddress );
  305. sBookNames := oDoc.Bookmarks.getElementNames;
  306. for iBookCounter := VarArrayLowBound(sBookNames, 1) to VarArrayHighBound(sBookNames, 1) do
  307. begin
  308. If sTableAddress = Copy( sBookNames[iBookCounter], 1, iTableNameLength) then
  309. begin
  310. GetBookmarkFromAdress := oDoc.Bookmarks.getByName(sBookNames[iBookCounter]);
  311. exit;
  312. end;
  313. end;
  314. end;
  315. { ' Returns the Bookmark the Tablename and Cellname }
  316. function TSampleCode.GetBookmarkFromDBPointer(
  317. oDoc : Variant;
  318. sBookmarkName : String) : Variant;
  319. var
  320. sBookNames : Variant;
  321. iBookCounter : Integer;
  322. begin
  323. sBookNames := oDoc.Bookmarks.getElementNames;
  324. for iBookCounter := VarArrayLowBound(sBookNames, 1) to VarArrayHighBound(sBookNames, 1) do
  325. begin
  326. If Pos(sBookmarkName, sBookNames[iBookCounter]) = (1 + Length(sBookNames[iBookCounter]) - Length(sBookmarkName)) then
  327. begin
  328. GetBookmarkFromDBPointer := oDoc.Bookmarks.getByName(sBookNames[iBookCounter]);
  329. exit;
  330. end;
  331. end;
  332. end;
  333. end.