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