1 /*
2  * This file is part of the LibreOffice project.
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
10  *   Licensed to the Apache Software Foundation (ASF) under one or more
11  *   contributor license agreements. See the NOTICE file distributed
12  *   with this work for additional information regarding copyright
13  *   ownership. The ASF licenses this file to you under the Apache
14  *   License, Version 2.0 (the "License"); you may not use this file
15  *   except in compliance with the License. You may obtain a copy of
16  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
17  */
18 
19 using System;
20 using unoidl.com.sun.star.lang;
21 using unoidl.com.sun.star.uno;
22 using unoidl.com.sun.star.frame;
23 using unoidl.com.sun.star.util;
24 
25 namespace cliversion
26 {
27 public class Version
28 {
Version()29     public Version()
30     {
31 		try
32 		{
33 //			System.Diagnostics.Debugger.Launch();
34 
35 			//link with cli_ure.dll
36 			uno.util.WeakBase wb = new uno.util.WeakBase();
37 			using ( SpreadsheetSample aSample = new SpreadsheetSample() )
38 			{
39 				aSample.doCellRangeSamples();
40 				aSample.terminate();
41 			}
42 		}
43 		catch (System.Exception )
44 		{
45 			//This exception is thrown if we link with a library which is not
46 			//available
47 			throw;
48 		}
49     }
50 }
51 
52 class SpreadsheetSample: SpreadsheetDocHelper
53 {
SpreadsheetSample()54     public SpreadsheetSample()
55     {
56     }
57         /** All samples regarding the service com.sun.star.sheet.SheetCellRange. */
doCellRangeSamples()58     public void doCellRangeSamples()
59     {
60         unoidl.com.sun.star.sheet.XSpreadsheet xSheet = getSpreadsheet( 0 );
61         unoidl.com.sun.star.table.XCellRange xCellRange = null;
62         unoidl.com.sun.star.beans.XPropertySet xPropSet = null;
63         unoidl.com.sun.star.table.CellRangeAddress aRangeAddress = null;
64 
65         // Preparation
66         setFormula( xSheet, "B5", "First cell" );
67         setFormula( xSheet, "B6", "Second cell" );
68         // Get cell range B5:B6 by position - (column, row, column, row)
69         xCellRange = xSheet.getCellRangeByPosition( 1, 4, 1, 5 );
70 
71 
72         // --- Change cell range properties. ---
73         xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCellRange;
74         // from com.sun.star.styles.CharacterProperties
75         xPropSet.setPropertyValue(
76             "CharColor", new uno.Any( (Int32) 0x003399 ) );
77         xPropSet.setPropertyValue(
78             "CharHeight", new uno.Any( (Single) 20.0 ) );
79         // from com.sun.star.styles.ParagraphProperties
80         xPropSet.setPropertyValue(
81             "ParaLeftMargin", new uno.Any( (Int32) 500 ) );
82         // from com.sun.star.table.CellProperties
83         xPropSet.setPropertyValue(
84             "IsCellBackgroundTransparent", new uno.Any( false ) );
85         xPropSet.setPropertyValue(
86             "CellBackColor", new uno.Any( (Int32) 0x99CCFF ) );
87 
88 
89         // --- Replace text in all cells. ---
90         unoidl.com.sun.star.util.XReplaceable xReplace =
91             (unoidl.com.sun.star.util.XReplaceable) xCellRange;
92         unoidl.com.sun.star.util.XReplaceDescriptor xReplaceDesc =
93             xReplace.createReplaceDescriptor();
94         xReplaceDesc.setSearchString( "cell" );
95         xReplaceDesc.setReplaceString( "text" );
96         // property SearchWords searches for whole cells!
97         xReplaceDesc.setPropertyValue( "SearchWords", new uno.Any( false ) );
98         int nCount = xReplace.replaceAll( xReplaceDesc );
99 
100         // --- Merge cells. ---
101         xCellRange = xSheet.getCellRangeByName( "F3:G6" );
102         prepareRange( xSheet, "E1:H7", "XMergeable" );
103         unoidl.com.sun.star.util.XMergeable xMerge =
104             (unoidl.com.sun.star.util.XMergeable) xCellRange;
105         xMerge.merge( true );
106 
107 
108         // --- Column properties. ---
109         xCellRange = xSheet.getCellRangeByName( "B1" );
110         unoidl.com.sun.star.table.XColumnRowRange xColRowRange =
111             (unoidl.com.sun.star.table.XColumnRowRange) xCellRange;
112         unoidl.com.sun.star.table.XTableColumns xColumns =
113             xColRowRange.getColumns();
114 
115         uno.Any aColumnObj = xColumns.getByIndex( 0 );
116         xPropSet = (unoidl.com.sun.star.beans.XPropertySet) aColumnObj.Value;
117         xPropSet.setPropertyValue( "Width", new uno.Any( (Int32) 6000 ) );
118 
119         unoidl.com.sun.star.container.XNamed xNamed =
120             (unoidl.com.sun.star.container.XNamed) aColumnObj.Value;
121 
122         // --- Cell range data ---
123         prepareRange( xSheet, "A9:C30", "XCellRangeData" );
124 
125         xCellRange = xSheet.getCellRangeByName( "A10:C30" );
126         unoidl.com.sun.star.sheet.XCellRangeData xData =
127             (unoidl.com.sun.star.sheet.XCellRangeData) xCellRange;
128         uno.Any [][] aValues =
129         {
130             new uno.Any [] { new uno.Any( "Name" ),
131                              new uno.Any( "Fruit" ),
132                              new uno.Any( "Quantity" ) },
133             new uno.Any [] { new uno.Any( "Alice" ),
134                              new uno.Any( "Apples" ),
135                              new uno.Any( (Double) 3.0 ) },
136             new uno.Any [] { new uno.Any( "Alice" ),
137                              new uno.Any( "Oranges" ),
138                              new uno.Any( (Double) 7.0 ) },
139             new uno.Any [] { new uno.Any( "Bob" ),
140                              new uno.Any( "Apples" ),
141                              new uno.Any( (Double) 3.0 ) },
142             new uno.Any [] { new uno.Any( "Alice" ),
143                              new uno.Any( "Apples" ),
144                              new uno.Any( (Double) 9.0 ) },
145             new uno.Any [] { new uno.Any( "Bob" ),
146                              new uno.Any( "Apples" ),
147                              new uno.Any( (Double) 5.0 ) },
148             new uno.Any [] { new uno.Any( "Bob" ),
149                              new uno.Any( "Oranges" ),
150                              new uno.Any( (Double) 6.0 ) },
151             new uno.Any [] { new uno.Any( "Alice" ),
152                              new uno.Any( "Oranges" ),
153                              new uno.Any( (Double) 3.0 ) },
154             new uno.Any [] { new uno.Any( "Alice" ),
155                              new uno.Any( "Apples" ),
156                              new uno.Any( (Double) 8.0 ) },
157             new uno.Any [] { new uno.Any( "Alice" ),
158                              new uno.Any( "Oranges" ),
159                              new uno.Any( (Double) 1.0 ) },
160             new uno.Any [] { new uno.Any( "Bob" ),
161                              new uno.Any( "Oranges" ),
162                              new uno.Any( (Double) 2.0 ) },
163             new uno.Any [] { new uno.Any( "Bob" ),
164                              new uno.Any( "Oranges" ),
165                              new uno.Any( (Double) 7.0 ) },
166             new uno.Any [] { new uno.Any( "Bob" ),
167                              new uno.Any( "Apples" ),
168                              new uno.Any( (Double) 1.0 ) },
169             new uno.Any [] { new uno.Any( "Alice" ),
170                              new uno.Any( "Apples" ),
171                              new uno.Any( (Double) 8.0 ) },
172             new uno.Any [] { new uno.Any( "Alice" ),
173                              new uno.Any( "Oranges" ),
174                              new uno.Any( (Double) 8.0 ) },
175             new uno.Any [] { new uno.Any( "Alice" ),
176                              new uno.Any( "Apples" ),
177                              new uno.Any( (Double) 7.0 ) },
178             new uno.Any [] { new uno.Any( "Bob" ),
179                              new uno.Any( "Apples" ),
180                              new uno.Any( (Double) 1.0 ) },
181             new uno.Any [] { new uno.Any( "Bob" ),
182                              new uno.Any( "Oranges" ),
183                              new uno.Any( (Double) 9.0 ) },
184             new uno.Any [] { new uno.Any( "Bob" ),
185                              new uno.Any( "Oranges" ),
186                              new uno.Any( (Double) 3.0 ) },
187             new uno.Any [] { new uno.Any( "Alice" ),
188                              new uno.Any( "Oranges" ),
189                              new uno.Any( (Double) 4.0 ) },
190             new uno.Any [] { new uno.Any( "Alice" ),
191                              new uno.Any( "Apples" ),
192                              new uno.Any( (Double) 9.0 ) }
193         };
194         xData.setDataArray( aValues );
195 
196 
197         // --- Get cell range address. ---
198         unoidl.com.sun.star.sheet.XCellRangeAddressable xRangeAddr =
199             (unoidl.com.sun.star.sheet.XCellRangeAddressable) xCellRange;
200         aRangeAddress = xRangeAddr.getRangeAddress();
201 
202         // --- Sheet operation. ---
203         // uses the range filled with XCellRangeData
204         unoidl.com.sun.star.sheet.XSheetOperation xSheetOp =
205             (unoidl.com.sun.star.sheet.XSheetOperation) xData;
206         double fResult = xSheetOp.computeFunction(
207             unoidl.com.sun.star.sheet.GeneralFunction.AVERAGE );
208 
209         // --- Fill series ---
210         // Prepare the example
211         setValue( xSheet, "E10", 1 );
212         setValue( xSheet, "E11", 4 );
213         setDate( xSheet, "E12", 30, 1, 2002 );
214         setFormula( xSheet, "I13", "Text 10" );
215         setFormula( xSheet, "E14", "Jan" );
216         setValue( xSheet, "K14", 10 );
217         setValue( xSheet, "E16", 1 );
218         setValue( xSheet, "F16", 2 );
219         setDate( xSheet, "E17", 28, 2, 2002 );
220         setDate( xSheet, "F17", 28, 1, 2002 );
221         setValue( xSheet, "E18", 6 );
222         setValue( xSheet, "F18", 4 );
223 
224     }
225 
226     /** Returns the XCellSeries interface of a cell range.
227         @param xSheet  The spreadsheet containing the cell range.
228         @param aRange  The address of the cell range.
229         @return  The XCellSeries interface. */
getCellSeries( unoidl.com.sun.star.sheet.XSpreadsheet xSheet, String aRange )230     private unoidl.com.sun.star.sheet.XCellSeries getCellSeries(
231             unoidl.com.sun.star.sheet.XSpreadsheet xSheet, String aRange )
232     {
233         return (unoidl.com.sun.star.sheet.XCellSeries)
234             xSheet.getCellRangeByName( aRange );
235     }
236 
237 }
238 
239 /** This is a helper class for the spreadsheet and table samples.
240     It connects to a running office and creates a spreadsheet document.
241     Additionally it contains various helper functions.
242  */
243 class SpreadsheetDocHelper : System.IDisposable
244 {
245 
246 // __  private members  ___________________________________________
247 
248     private const String  msDataSheetName  = "Data";
249 
250     private unoidl.com.sun.star.uno.XComponentContext m_xContext;
251     private unoidl.com.sun.star.lang.XMultiServiceFactory  mxMSFactory;
252     private unoidl.com.sun.star.sheet.XSpreadsheetDocument mxDocument;
253 
254 
255 
SpreadsheetDocHelper()256     public SpreadsheetDocHelper()
257     {
258 //        System.Diagnostics.Debugger.Launch();
259         // Connect to a running office and get the service manager
260         mxMSFactory = connect();
261         // Create a new spreadsheet document
262         mxDocument = initDocument();
263     }
264 
265 // __  helper methods  ____________________________________________
266 
267     /** Returns the service manager.
268         @return  XMultiServiceFactory interface of the service manager. */
getServiceManager()269     public unoidl.com.sun.star.lang.XMultiServiceFactory getServiceManager()
270     {
271         return mxMSFactory;
272     }
273 
274     /** Returns the whole spreadsheet document.
275         @return  XSpreadsheetDocument interface of the document. */
getDocument()276     public unoidl.com.sun.star.sheet.XSpreadsheetDocument getDocument()
277     {
278         return mxDocument;
279     }
280 
281     /** Returns the spreadsheet with the specified index (0-based).
282         @param nIndex  The index of the sheet.
283         @return  XSpreadsheet interface of the sheet. */
getSpreadsheet( int nIndex )284     public unoidl.com.sun.star.sheet.XSpreadsheet getSpreadsheet( int nIndex )
285     {
286         // Collection of sheets
287         unoidl.com.sun.star.sheet.XSpreadsheets xSheets =
288             mxDocument.getSheets();
289 
290         unoidl.com.sun.star.container.XIndexAccess xSheetsIA =
291             (unoidl.com.sun.star.container.XIndexAccess) xSheets;
292 
293         unoidl.com.sun.star.sheet.XSpreadsheet xSheet =
294             (unoidl.com.sun.star.sheet.XSpreadsheet)
295               xSheetsIA.getByIndex( nIndex ).Value;
296 
297         return xSheet;
298     }
299 
300     /** Inserts a new empty spreadsheet with the specified name.
301         @param aName  The name of the new sheet.
302         @param nIndex  The insertion index.
303         @return  The XSpreadsheet interface of the new sheet. */
insertSpreadsheet( String aName, short nIndex )304     public unoidl.com.sun.star.sheet.XSpreadsheet insertSpreadsheet(
305         String aName, short nIndex )
306     {
307         // Collection of sheets
308         unoidl.com.sun.star.sheet.XSpreadsheets xSheets =
309             mxDocument.getSheets();
310 
311         xSheets.insertNewByName( aName, nIndex );
312         unoidl.com.sun.star.sheet.XSpreadsheet xSheet =
313             (unoidl.com.sun.star.sheet.XSpreadsheet)
314               xSheets.getByName( aName ).Value;
315 
316         return xSheet;
317     }
318 
319 
320 // Methods to fill values into cells.
321 
322     /** Writes a double value into a spreadsheet.
323         @param xSheet  The XSpreadsheet interface of the spreadsheet.
324         @param aCellName  The address of the cell (or a named range).
325         @param fValue  The value to write into the cell. */
setValue( unoidl.com.sun.star.sheet.XSpreadsheet xSheet, String aCellName, double fValue )326     public void setValue(
327         unoidl.com.sun.star.sheet.XSpreadsheet xSheet,
328         String aCellName,
329         double fValue )
330     {
331         xSheet.getCellRangeByName( aCellName ).getCellByPosition(
332             0, 0 ).setValue( fValue );
333     }
334 
335     /** Writes a formula into a spreadsheet.
336         @param xSheet  The XSpreadsheet interface of the spreadsheet.
337         @param aCellName  The address of the cell (or a named range).
338         @param aFormula  The formula to write into the cell. */
setFormula( unoidl.com.sun.star.sheet.XSpreadsheet xSheet, String aCellName, String aFormula )339     public void setFormula(
340         unoidl.com.sun.star.sheet.XSpreadsheet xSheet,
341         String aCellName,
342         String aFormula )
343     {
344         xSheet.getCellRangeByName( aCellName ).getCellByPosition(
345             0, 0 ).setFormula( aFormula );
346     }
347 
348     /** Writes a date with standard date format into a spreadsheet.
349         @param xSheet  The XSpreadsheet interface of the spreadsheet.
350         @param aCellName  The address of the cell (or a named range).
351         @param nDay  The day of the date.
352         @param nMonth  The month of the date.
353         @param nYear  The year of the date. */
setDate( unoidl.com.sun.star.sheet.XSpreadsheet xSheet, String aCellName, int nDay, int nMonth, int nYear )354     public void setDate(
355         unoidl.com.sun.star.sheet.XSpreadsheet xSheet,
356         String aCellName,
357         int nDay, int nMonth, int nYear )
358     {
359         // Set the date value.
360         unoidl.com.sun.star.table.XCell xCell =
361             xSheet.getCellRangeByName( aCellName ).getCellByPosition( 0, 0 );
362         String aDateStr = nMonth + "/" + nDay + "/" + nYear;
363         xCell.setFormula( aDateStr );
364 
365         // Set standard date format.
366         unoidl.com.sun.star.util.XNumberFormatsSupplier xFormatsSupplier =
367             (unoidl.com.sun.star.util.XNumberFormatsSupplier) getDocument();
368         unoidl.com.sun.star.util.XNumberFormatTypes xFormatTypes =
369             (unoidl.com.sun.star.util.XNumberFormatTypes)
370               xFormatsSupplier.getNumberFormats();
371         int nFormat = xFormatTypes.getStandardFormat(
372             unoidl.com.sun.star.util.NumberFormat.DATE,
373             new unoidl.com.sun.star.lang.Locale() );
374 
375         unoidl.com.sun.star.beans.XPropertySet xPropSet =
376             (unoidl.com.sun.star.beans.XPropertySet) xCell;
377         xPropSet.setPropertyValue(
378             "NumberFormat",
379             new uno.Any( (Int32) nFormat ) );
380     }
381 
382     /** Draws a colored border around the range and writes the headline
383         in the first cell.
384 
385         @param xSheet  The XSpreadsheet interface of the spreadsheet.
386         @param aRange  The address of the cell range (or a named range).
387         @param aHeadline  The headline text. */
prepareRange( unoidl.com.sun.star.sheet.XSpreadsheet xSheet, String aRange, String aHeadline )388     public void prepareRange(
389         unoidl.com.sun.star.sheet.XSpreadsheet xSheet,
390         String aRange, String aHeadline )
391     {
392         unoidl.com.sun.star.beans.XPropertySet xPropSet = null;
393         unoidl.com.sun.star.table.XCellRange xCellRange = null;
394 
395         // draw border
396         xCellRange = xSheet.getCellRangeByName( aRange );
397         xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCellRange;
398         unoidl.com.sun.star.table.BorderLine aLine =
399             new unoidl.com.sun.star.table.BorderLine();
400         aLine.Color = 0x99CCFF;
401         aLine.InnerLineWidth = aLine.LineDistance = 0;
402         aLine.OuterLineWidth = 100;
403         unoidl.com.sun.star.table.TableBorder aBorder =
404             new unoidl.com.sun.star.table.TableBorder();
405         aBorder.TopLine = aBorder.BottomLine = aBorder.LeftLine =
406             aBorder.RightLine = aLine;
407         aBorder.IsTopLineValid = aBorder.IsBottomLineValid = true;
408         aBorder.IsLeftLineValid = aBorder.IsRightLineValid = true;
409         xPropSet.setPropertyValue(
410             "TableBorder",
411             new uno.Any(
412                 typeof (unoidl.com.sun.star.table.TableBorder), aBorder ) );
413 
414         // draw headline
415         unoidl.com.sun.star.sheet.XCellRangeAddressable xAddr =
416             (unoidl.com.sun.star.sheet.XCellRangeAddressable) xCellRange;
417         unoidl.com.sun.star.table.CellRangeAddress aAddr =
418             xAddr.getRangeAddress();
419 
420         xCellRange = xSheet.getCellRangeByPosition(
421             aAddr.StartColumn,
422             aAddr.StartRow, aAddr.EndColumn, aAddr.StartRow );
423 
424         xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCellRange;
425         xPropSet.setPropertyValue(
426             "CellBackColor", new uno.Any( (Int32) 0x99CCFF ) );
427         // write headline
428         unoidl.com.sun.star.table.XCell xCell =
429             xCellRange.getCellByPosition( 0, 0 );
430         xCell.setFormula( aHeadline );
431         xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCell;
432         xPropSet.setPropertyValue(
433             "CharColor", new uno.Any( (Int32) 0x003399 ) );
434         xPropSet.setPropertyValue(
435             "CharWeight",
436             new uno.Any( (Single) unoidl.com.sun.star.awt.FontWeight.BOLD ) );
437     }
438 
439 
440 // Methods to create cell addresses and range addresses.
441 
442     /** Creates a unoidl.com.sun.star.table.CellAddress and initializes it
443         with the given range.
444         @param xSheet  The XSpreadsheet interface of the spreadsheet.
445         @param aCell  The address of the cell (or a named cell). */
createCellAddress( unoidl.com.sun.star.sheet.XSpreadsheet xSheet, String aCell )446     public unoidl.com.sun.star.table.CellAddress createCellAddress(
447         unoidl.com.sun.star.sheet.XSpreadsheet xSheet,
448         String aCell )
449     {
450         unoidl.com.sun.star.sheet.XCellAddressable xAddr =
451         (unoidl.com.sun.star.sheet.XCellAddressable)
452             xSheet.getCellRangeByName( aCell ).getCellByPosition( 0, 0 );
453         return xAddr.getCellAddress();
454     }
455 
456     /** Creates a unoidl.com.sun.star.table.CellRangeAddress and initializes
457         it with the given range.
458         @param xSheet  The XSpreadsheet interface of the spreadsheet.
459         @param aRange  The address of the cell range (or a named range). */
createCellRangeAddress( unoidl.com.sun.star.sheet.XSpreadsheet xSheet, String aRange )460     public unoidl.com.sun.star.table.CellRangeAddress createCellRangeAddress(
461         unoidl.com.sun.star.sheet.XSpreadsheet xSheet, String aRange )
462     {
463         unoidl.com.sun.star.sheet.XCellRangeAddressable xAddr =
464             (unoidl.com.sun.star.sheet.XCellRangeAddressable)
465             xSheet.getCellRangeByName( aRange );
466         return xAddr.getRangeAddress();
467     }
468 
469 
470 // Methods to convert cell addresses and range addresses to strings.
471 
472     /** Returns the text address of the cell.
473         @param nColumn  The column index.
474         @param nRow  The row index.
475         @return  A string containing the cell address. */
getCellAddressString( int nColumn, int nRow )476     public String getCellAddressString( int nColumn, int nRow )
477     {
478         String aStr = "";
479         if (nColumn > 25)
480             aStr += (char) ('A' + nColumn / 26 - 1);
481         aStr += (char) ('A' + nColumn % 26);
482         aStr += (nRow + 1);
483         return aStr;
484     }
485 
486     /** Returns the text address of the cell range.
487         @param aCellRange  The cell range address.
488         @return  A string containing the cell range address. */
getCellRangeAddressString( unoidl.com.sun.star.table.CellRangeAddress aCellRange )489     public String getCellRangeAddressString(
490         unoidl.com.sun.star.table.CellRangeAddress aCellRange )
491     {
492         return
493             getCellAddressString( aCellRange.StartColumn, aCellRange.StartRow )
494             + ":"
495             + getCellAddressString( aCellRange.EndColumn, aCellRange.EndRow );
496     }
497 
498     /** Returns the text address of the cell range.
499         @param xCellRange  The XSheetCellRange interface of the cell range.
500         @param bWithSheet  true = Include sheet name.
501         @return  A string containing the cell range address. */
getCellRangeAddressString( unoidl.com.sun.star.sheet.XSheetCellRange xCellRange, bool bWithSheet )502     public String getCellRangeAddressString(
503         unoidl.com.sun.star.sheet.XSheetCellRange xCellRange, bool bWithSheet )
504     {
505         String aStr = "";
506         if (bWithSheet)
507         {
508             unoidl.com.sun.star.sheet.XSpreadsheet xSheet =
509                 xCellRange.getSpreadsheet();
510             unoidl.com.sun.star.container.XNamed xNamed =
511                 (unoidl.com.sun.star.container.XNamed) xSheet;
512             aStr += xNamed.getName() + ".";
513         }
514         unoidl.com.sun.star.sheet.XCellRangeAddressable xAddr =
515             (unoidl.com.sun.star.sheet.XCellRangeAddressable) xCellRange;
516         aStr += getCellRangeAddressString( xAddr.getRangeAddress() );
517         return aStr;
518     }
519 
520     /** Returns a list of addresses of all cell ranges contained in the
521         collection.
522 
523         @param xRangesIA  The XIndexAccess interface of the collection.
524         @return  A string containing the cell range address list. */
getCellRangeListString( unoidl.com.sun.star.container.XIndexAccess xRangesIA )525     public String getCellRangeListString(
526         unoidl.com.sun.star.container.XIndexAccess xRangesIA )
527     {
528         String aStr = "";
529         int nCount = xRangesIA.getCount();
530         for (int nIndex = 0; nIndex < nCount; ++nIndex)
531         {
532             if (nIndex > 0)
533                 aStr += " ";
534             uno.Any aRangeObj = xRangesIA.getByIndex( nIndex );
535             unoidl.com.sun.star.sheet.XSheetCellRange xCellRange =
536                 (unoidl.com.sun.star.sheet.XSheetCellRange) aRangeObj.Value;
537             aStr += getCellRangeAddressString( xCellRange, false );
538         }
539         return aStr;
540     }
541 
542 
543 
544     /** Connect to a running office that is accepting connections.
545         @return  The ServiceManager to instantiate office components. */
connect()546     private XMultiServiceFactory connect()
547     {
548 
549         m_xContext = uno.util.Bootstrap.bootstrap();
550 
551         return (XMultiServiceFactory) m_xContext.getServiceManager();
552     }
553 
Dispose()554     public void Dispose()
555     {
556 
557     }
558 
559     /** Creates an empty spreadsheet document.
560         @return  The XSpreadsheetDocument interface of the document. */
initDocument()561     private unoidl.com.sun.star.sheet.XSpreadsheetDocument initDocument()
562     {
563         XComponentLoader aLoader = (XComponentLoader)
564             mxMSFactory.createInstance( "com.sun.star.frame.Desktop" );
565 
566         XComponent xComponent = aLoader.loadComponentFromURL(
567             "private:factory/scalc", "_blank", 0,
568             new unoidl.com.sun.star.beans.PropertyValue[0] );
569 
570         return (unoidl.com.sun.star.sheet.XSpreadsheetDocument) xComponent;
571     }
572 
573 
terminate()574     public void terminate()
575     {
576         XModifiable xMod = (XModifiable) mxDocument;
577         if (xMod != null)
578             xMod.setModified(false);
579         XDesktop aDesktop = (XDesktop)
580             mxMSFactory.createInstance( "com.sun.star.frame.Desktop" );
581         if (aDesktop != null)
582         {
583             try
584             {
585                 aDesktop.terminate();
586             }
587             catch (DisposedException d)
588             {
589                 //This exception may be thrown because shutting down OOo using
590                 //XDesktop terminate does not really work. In the case of the
591                 //Exception OOo will still terminate.
592             }
593         }
594     }
595 
596 
597 }
598 
599 }
600