1*cf279e26SAndrew Rist /**************************************************************
2*cf279e26SAndrew Rist  *
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 
22cdf0e10cSrcweir 
23cdf0e10cSrcweir using System;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir // __________  implementation  ____________________________________
26cdf0e10cSrcweir 
27cdf0e10cSrcweir /** Create and modify a spreadsheet document.
28cdf0e10cSrcweir  */
29cdf0e10cSrcweir public class SpreadsheetSample : SpreadsheetDocHelper
30cdf0e10cSrcweir {
31cdf0e10cSrcweir 
Main( String [] args )32cdf0e10cSrcweir     public static void Main( String [] args )
33cdf0e10cSrcweir     {
34cdf0e10cSrcweir         try
35cdf0e10cSrcweir         {
36cdf0e10cSrcweir             using ( SpreadsheetSample aSample = new SpreadsheetSample( args ) )
37cdf0e10cSrcweir             {
38cdf0e10cSrcweir                 aSample.doSampleFunctions();
39cdf0e10cSrcweir             }
40cdf0e10cSrcweir             Console.WriteLine( "\nSamples done." );
41cdf0e10cSrcweir         }
42cdf0e10cSrcweir         catch (Exception ex)
43cdf0e10cSrcweir         {
44cdf0e10cSrcweir             Console.WriteLine( "Sample caught exception! " + ex );
45cdf0e10cSrcweir         }
46cdf0e10cSrcweir     }
47cdf0e10cSrcweir 
SpreadsheetSample( String[] args )48cdf0e10cSrcweir     public SpreadsheetSample( String[] args )
49cdf0e10cSrcweir         : base( args )
50cdf0e10cSrcweir     {
51cdf0e10cSrcweir     }
52cdf0e10cSrcweir 
53cdf0e10cSrcweir     /** This sample function performs all changes on the document. */
doSampleFunctions()54cdf0e10cSrcweir     public void doSampleFunctions()
55cdf0e10cSrcweir     {
56cdf0e10cSrcweir         doCellSamples();
57cdf0e10cSrcweir         doCellRangeSamples();
58cdf0e10cSrcweir         doCellRangesSamples();
59cdf0e10cSrcweir         doCellCursorSamples();
60cdf0e10cSrcweir         doFormattingSamples();
61cdf0e10cSrcweir         doDocumentSamples();
62cdf0e10cSrcweir         doDatabaseSamples();
63cdf0e10cSrcweir         doDataPilotSamples();
64cdf0e10cSrcweir         doNamedRangesSamples();
65cdf0e10cSrcweir         doFunctionAccessSamples();
66cdf0e10cSrcweir         doApplicationSettingsSamples();
67cdf0e10cSrcweir     }
68cdf0e10cSrcweir 
69cdf0e10cSrcweir // ________________________________________________________________
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     /** All samples regarding the service com.sun.star.sheet.SheetCell. */
doCellSamples()72cdf0e10cSrcweir     private void doCellSamples()
73cdf0e10cSrcweir     {
74cdf0e10cSrcweir         Console.WriteLine( "\n*** Samples for service sheet.SheetCell ***\n" );
75cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XSpreadsheet xSheet = getSpreadsheet( 0 );
76cdf0e10cSrcweir         unoidl.com.sun.star.table.XCell xCell = null;
77cdf0e10cSrcweir         unoidl.com.sun.star.beans.XPropertySet xPropSet = null;
78cdf0e10cSrcweir         String aText;
79cdf0e10cSrcweir         prepareRange( xSheet, "A1:C7", "Cells and Cell Ranges" );
80cdf0e10cSrcweir 
81cdf0e10cSrcweir         // --- Get cell B3 by position - (column, row) ---
82cdf0e10cSrcweir         xCell = xSheet.getCellByPosition( 1, 2 );
83cdf0e10cSrcweir 
84cdf0e10cSrcweir         // --- Insert two text paragraphs into the cell. ---
85cdf0e10cSrcweir         unoidl.com.sun.star.text.XText xText =
86cdf0e10cSrcweir             (unoidl.com.sun.star.text.XText) xCell;
87cdf0e10cSrcweir         unoidl.com.sun.star.text.XTextCursor xTextCursor =
88cdf0e10cSrcweir             xText.createTextCursor();
89cdf0e10cSrcweir 
90cdf0e10cSrcweir         xText.insertString( xTextCursor, "Text in first line.", false );
91cdf0e10cSrcweir         xText.insertControlCharacter( xTextCursor,
92cdf0e10cSrcweir             unoidl.com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false );
93cdf0e10cSrcweir         xText.insertString( xTextCursor, "And a ", false );
94cdf0e10cSrcweir 
95cdf0e10cSrcweir         // create a hyperlink
96cdf0e10cSrcweir         unoidl.com.sun.star.lang.XMultiServiceFactory xServiceMan =
97cdf0e10cSrcweir             (unoidl.com.sun.star.lang.XMultiServiceFactory) getDocument();
98cdf0e10cSrcweir         Object aHyperlinkObj =
99cdf0e10cSrcweir             xServiceMan.createInstance( "com.sun.star.text.TextField.URL" );
100cdf0e10cSrcweir         xPropSet = (unoidl.com.sun.star.beans.XPropertySet) aHyperlinkObj;
101cdf0e10cSrcweir         xPropSet.setPropertyValue(
102cdf0e10cSrcweir             "URL", new uno.Any( "http://www.example.org" ) );
103cdf0e10cSrcweir         xPropSet.setPropertyValue(
104cdf0e10cSrcweir             "Representation", new uno.Any( "hyperlink" ) );
105cdf0e10cSrcweir         // ... and insert
106cdf0e10cSrcweir         unoidl.com.sun.star.text.XTextContent xContent =
107cdf0e10cSrcweir             (unoidl.com.sun.star.text.XTextContent) aHyperlinkObj;
108cdf0e10cSrcweir         xText.insertTextContent( xTextCursor, xContent, false );
109cdf0e10cSrcweir 
110cdf0e10cSrcweir         // --- Query the separate paragraphs. ---
111cdf0e10cSrcweir         unoidl.com.sun.star.container.XEnumerationAccess xParaEA =
112cdf0e10cSrcweir             (unoidl.com.sun.star.container.XEnumerationAccess) xCell;
113cdf0e10cSrcweir         unoidl.com.sun.star.container.XEnumeration xParaEnum =
114cdf0e10cSrcweir             xParaEA.createEnumeration();
115cdf0e10cSrcweir         // Go through the paragraphs
116cdf0e10cSrcweir         while( xParaEnum.hasMoreElements() )
117cdf0e10cSrcweir         {
118cdf0e10cSrcweir             uno.Any aPortionObj = xParaEnum.nextElement();
119cdf0e10cSrcweir             unoidl.com.sun.star.container.XEnumerationAccess xPortionEA =
120cdf0e10cSrcweir                 (unoidl.com.sun.star.container.XEnumerationAccess)
121cdf0e10cSrcweir                 aPortionObj.Value;
122cdf0e10cSrcweir             unoidl.com.sun.star.container.XEnumeration xPortionEnum =
123cdf0e10cSrcweir                 xPortionEA.createEnumeration();
124cdf0e10cSrcweir             aText = "";
125cdf0e10cSrcweir             // Go through all text portions of a paragraph and construct string.
126cdf0e10cSrcweir             while( xPortionEnum.hasMoreElements() )
127cdf0e10cSrcweir             {
128cdf0e10cSrcweir                 unoidl.com.sun.star.text.XTextRange xRange =
129cdf0e10cSrcweir                     (unoidl.com.sun.star.text.XTextRange)
130cdf0e10cSrcweir                     xPortionEnum.nextElement().Value;
131cdf0e10cSrcweir                 aText += xRange.getString();
132cdf0e10cSrcweir             }
133cdf0e10cSrcweir             Console.WriteLine( "Paragraph text: " + aText );
134cdf0e10cSrcweir         }
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 
137cdf0e10cSrcweir         // --- Change cell properties. ---
138cdf0e10cSrcweir         xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCell;
139cdf0e10cSrcweir         // from styles.CharacterProperties
140cdf0e10cSrcweir         xPropSet.setPropertyValue(
141cdf0e10cSrcweir             "CharColor", new uno.Any( (Int32) 0x003399 ) );
142cdf0e10cSrcweir         xPropSet.setPropertyValue(
143cdf0e10cSrcweir             "CharHeight", new uno.Any( (Single) 20.0 ) );
144cdf0e10cSrcweir         // from styles.ParagraphProperties
145cdf0e10cSrcweir         xPropSet.setPropertyValue(
146cdf0e10cSrcweir             "ParaLeftMargin", new uno.Any( (Int32) 500 ) );
147cdf0e10cSrcweir         // from table.CellProperties
148cdf0e10cSrcweir         xPropSet.setPropertyValue(
149cdf0e10cSrcweir             "IsCellBackgroundTransparent", new uno.Any( false ) );
150cdf0e10cSrcweir         xPropSet.setPropertyValue(
151cdf0e10cSrcweir             "CellBackColor", new uno.Any( (Int32) 0x99CCFF ) );
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 
154cdf0e10cSrcweir         // --- Get cell address. ---
155cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XCellAddressable xCellAddr =
156cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XCellAddressable) xCell;
157cdf0e10cSrcweir         unoidl.com.sun.star.table.CellAddress aAddress =
158cdf0e10cSrcweir             xCellAddr.getCellAddress();
159cdf0e10cSrcweir         aText = "Address of this cell:  Column=" + aAddress.Column;
160cdf0e10cSrcweir         aText += ";  Row=" + aAddress.Row;
161cdf0e10cSrcweir         aText += ";  Sheet=" + aAddress.Sheet;
162cdf0e10cSrcweir         Console.WriteLine( aText );
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 
165cdf0e10cSrcweir         // --- Insert an annotation ---
166cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XSheetAnnotationsSupplier xAnnotationsSupp =
167cdf0e10cSrcweir         (unoidl.com.sun.star.sheet.XSheetAnnotationsSupplier) xSheet;
168cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XSheetAnnotations xAnnotations =
169cdf0e10cSrcweir             xAnnotationsSupp.getAnnotations();
170cdf0e10cSrcweir         xAnnotations.insertNew( aAddress, "This is an annotation" );
171cdf0e10cSrcweir 
172cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XSheetAnnotationAnchor xAnnotAnchor =
173cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XSheetAnnotationAnchor) xCell;
174cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XSheetAnnotation xAnnotation =
175cdf0e10cSrcweir             xAnnotAnchor.getAnnotation();
176cdf0e10cSrcweir         xAnnotation.setIsVisible( true );
177cdf0e10cSrcweir     }
178cdf0e10cSrcweir 
179cdf0e10cSrcweir // ________________________________________________________________
180cdf0e10cSrcweir 
181cdf0e10cSrcweir     /** All samples regarding the service com.sun.star.sheet.SheetCellRange. */
doCellRangeSamples()182cdf0e10cSrcweir     private void doCellRangeSamples()
183cdf0e10cSrcweir     {
184cdf0e10cSrcweir         Console.WriteLine(
185cdf0e10cSrcweir             "\n*** Samples for service sheet.SheetCellRange ***\n" );
186cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XSpreadsheet xSheet = getSpreadsheet( 0 );
187cdf0e10cSrcweir         unoidl.com.sun.star.table.XCellRange xCellRange = null;
188cdf0e10cSrcweir         unoidl.com.sun.star.beans.XPropertySet xPropSet = null;
189cdf0e10cSrcweir         unoidl.com.sun.star.table.CellRangeAddress aRangeAddress = null;
190cdf0e10cSrcweir 
191cdf0e10cSrcweir         // Preparation
192cdf0e10cSrcweir         setFormula( xSheet, "B5", "First cell" );
193cdf0e10cSrcweir         setFormula( xSheet, "B6", "Second cell" );
194cdf0e10cSrcweir         // Get cell range B5:B6 by position - (column, row, column, row)
195cdf0e10cSrcweir         xCellRange = xSheet.getCellRangeByPosition( 1, 4, 1, 5 );
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 
198cdf0e10cSrcweir         // --- Change cell range properties. ---
199cdf0e10cSrcweir         xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCellRange;
200cdf0e10cSrcweir         // from com.sun.star.styles.CharacterProperties
201cdf0e10cSrcweir         xPropSet.setPropertyValue(
202cdf0e10cSrcweir             "CharColor", new uno.Any( (Int32) 0x003399 ) );
203cdf0e10cSrcweir         xPropSet.setPropertyValue(
204cdf0e10cSrcweir             "CharHeight", new uno.Any( (Single) 20.0 ) );
205cdf0e10cSrcweir         // from com.sun.star.styles.ParagraphProperties
206cdf0e10cSrcweir         xPropSet.setPropertyValue(
207cdf0e10cSrcweir             "ParaLeftMargin", new uno.Any( (Int32) 500 ) );
208cdf0e10cSrcweir         // from com.sun.star.table.CellProperties
209cdf0e10cSrcweir         xPropSet.setPropertyValue(
210cdf0e10cSrcweir             "IsCellBackgroundTransparent", new uno.Any( false ) );
211cdf0e10cSrcweir         xPropSet.setPropertyValue(
212cdf0e10cSrcweir             "CellBackColor", new uno.Any( (Int32) 0x99CCFF ) );
213cdf0e10cSrcweir 
214cdf0e10cSrcweir 
215cdf0e10cSrcweir         // --- Replace text in all cells. ---
216cdf0e10cSrcweir         unoidl.com.sun.star.util.XReplaceable xReplace =
217cdf0e10cSrcweir             (unoidl.com.sun.star.util.XReplaceable) xCellRange;
218cdf0e10cSrcweir         unoidl.com.sun.star.util.XReplaceDescriptor xReplaceDesc =
219cdf0e10cSrcweir             xReplace.createReplaceDescriptor();
220cdf0e10cSrcweir         xReplaceDesc.setSearchString( "cell" );
221cdf0e10cSrcweir         xReplaceDesc.setReplaceString( "text" );
222cdf0e10cSrcweir         // property SearchWords searches for whole cells!
223cdf0e10cSrcweir         xReplaceDesc.setPropertyValue( "SearchWords", new uno.Any( false ) );
224cdf0e10cSrcweir         int nCount = xReplace.replaceAll( xReplaceDesc );
225cdf0e10cSrcweir         Console.WriteLine( "Search text replaced " + nCount + " times." );
226cdf0e10cSrcweir 
227cdf0e10cSrcweir 
228cdf0e10cSrcweir         // --- Merge cells. ---
229cdf0e10cSrcweir         xCellRange = xSheet.getCellRangeByName( "F3:G6" );
230cdf0e10cSrcweir         prepareRange( xSheet, "E1:H7", "XMergeable" );
231cdf0e10cSrcweir         unoidl.com.sun.star.util.XMergeable xMerge =
232cdf0e10cSrcweir             (unoidl.com.sun.star.util.XMergeable) xCellRange;
233cdf0e10cSrcweir         xMerge.merge( true );
234cdf0e10cSrcweir 
235cdf0e10cSrcweir 
236cdf0e10cSrcweir         // --- Change indentation. ---
237cdf0e10cSrcweir /* does not work (bug in XIndent implementation)
238cdf0e10cSrcweir         prepareRange( xSheet, "I20:I23", "XIndent" );
239cdf0e10cSrcweir         setValue( xSheet, "I21", 1 );
240cdf0e10cSrcweir         setValue( xSheet, "I22", 1 );
241cdf0e10cSrcweir         setValue( xSheet, "I23", 1 );
242cdf0e10cSrcweir 
243cdf0e10cSrcweir         xCellRange = xSheet.getCellRangeByName( "I21:I22" );
244cdf0e10cSrcweir         unoidl.com.sun.star.util.XIndent xIndent =
245cdf0e10cSrcweir         (unoidl.com.sun.star.util.XIndent) xCellRange;
246cdf0e10cSrcweir         xIndent.incrementIndent();
247cdf0e10cSrcweir 
248cdf0e10cSrcweir         xCellRange = xSheet.getCellRangeByName( "I22:I23" );
249cdf0e10cSrcweir         xIndent = (unoidl.com.sun.star.util.XIndent) xCellRange;
250cdf0e10cSrcweir         xIndent.incrementIndent();
251cdf0e10cSrcweir */
252cdf0e10cSrcweir 
253cdf0e10cSrcweir 
254cdf0e10cSrcweir         // --- Column properties. ---
255cdf0e10cSrcweir         xCellRange = xSheet.getCellRangeByName( "B1" );
256cdf0e10cSrcweir         unoidl.com.sun.star.table.XColumnRowRange xColRowRange =
257cdf0e10cSrcweir             (unoidl.com.sun.star.table.XColumnRowRange) xCellRange;
258cdf0e10cSrcweir         unoidl.com.sun.star.table.XTableColumns xColumns =
259cdf0e10cSrcweir             xColRowRange.getColumns();
260cdf0e10cSrcweir 
261cdf0e10cSrcweir         uno.Any aColumnObj = xColumns.getByIndex( 0 );
262cdf0e10cSrcweir         xPropSet = (unoidl.com.sun.star.beans.XPropertySet) aColumnObj.Value;
263cdf0e10cSrcweir         xPropSet.setPropertyValue( "Width", new uno.Any( (Int32) 6000 ) );
264cdf0e10cSrcweir 
265cdf0e10cSrcweir         unoidl.com.sun.star.container.XNamed xNamed =
266cdf0e10cSrcweir             (unoidl.com.sun.star.container.XNamed) aColumnObj.Value;
267cdf0e10cSrcweir         Console.WriteLine(
268cdf0e10cSrcweir             "The name of the wide column is " + xNamed.getName() + "." );
269cdf0e10cSrcweir 
270cdf0e10cSrcweir 
271cdf0e10cSrcweir         // --- Cell range data ---
272cdf0e10cSrcweir         prepareRange( xSheet, "A9:C30", "XCellRangeData" );
273cdf0e10cSrcweir 
274cdf0e10cSrcweir         xCellRange = xSheet.getCellRangeByName( "A10:C30" );
275cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XCellRangeData xData =
276cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XCellRangeData) xCellRange;
277cdf0e10cSrcweir         uno.Any [][] aValues =
278cdf0e10cSrcweir         {
279cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Name" ),
280cdf0e10cSrcweir                              new uno.Any( "Fruit" ),
281cdf0e10cSrcweir                              new uno.Any( "Quantity" ) },
282cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Alice" ),
283cdf0e10cSrcweir                              new uno.Any( "Apples" ),
284cdf0e10cSrcweir                              new uno.Any( (Double) 3.0 ) },
285cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Alice" ),
286cdf0e10cSrcweir                              new uno.Any( "Oranges" ),
287cdf0e10cSrcweir                              new uno.Any( (Double) 7.0 ) },
288cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Bob" ),
289cdf0e10cSrcweir                              new uno.Any( "Apples" ),
290cdf0e10cSrcweir                              new uno.Any( (Double) 3.0 ) },
291cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Alice" ),
292cdf0e10cSrcweir                              new uno.Any( "Apples" ),
293cdf0e10cSrcweir                              new uno.Any( (Double) 9.0 ) },
294cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Bob" ),
295cdf0e10cSrcweir                              new uno.Any( "Apples" ),
296cdf0e10cSrcweir                              new uno.Any( (Double) 5.0 ) },
297cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Bob" ),
298cdf0e10cSrcweir                              new uno.Any( "Oranges" ),
299cdf0e10cSrcweir                              new uno.Any( (Double) 6.0 ) },
300cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Alice" ),
301cdf0e10cSrcweir                              new uno.Any( "Oranges" ),
302cdf0e10cSrcweir                              new uno.Any( (Double) 3.0 ) },
303cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Alice" ),
304cdf0e10cSrcweir                              new uno.Any( "Apples" ),
305cdf0e10cSrcweir                              new uno.Any( (Double) 8.0 ) },
306cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Alice" ),
307cdf0e10cSrcweir                              new uno.Any( "Oranges" ),
308cdf0e10cSrcweir                              new uno.Any( (Double) 1.0 ) },
309cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Bob" ),
310cdf0e10cSrcweir                              new uno.Any( "Oranges" ),
311cdf0e10cSrcweir                              new uno.Any( (Double) 2.0 ) },
312cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Bob" ),
313cdf0e10cSrcweir                              new uno.Any( "Oranges" ),
314cdf0e10cSrcweir                              new uno.Any( (Double) 7.0 ) },
315cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Bob" ),
316cdf0e10cSrcweir                              new uno.Any( "Apples" ),
317cdf0e10cSrcweir                              new uno.Any( (Double) 1.0 ) },
318cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Alice" ),
319cdf0e10cSrcweir                              new uno.Any( "Apples" ),
320cdf0e10cSrcweir                              new uno.Any( (Double) 8.0 ) },
321cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Alice" ),
322cdf0e10cSrcweir                              new uno.Any( "Oranges" ),
323cdf0e10cSrcweir                              new uno.Any( (Double) 8.0 ) },
324cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Alice" ),
325cdf0e10cSrcweir                              new uno.Any( "Apples" ),
326cdf0e10cSrcweir                              new uno.Any( (Double) 7.0 ) },
327cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Bob" ),
328cdf0e10cSrcweir                              new uno.Any( "Apples" ),
329cdf0e10cSrcweir                              new uno.Any( (Double) 1.0 ) },
330cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Bob" ),
331cdf0e10cSrcweir                              new uno.Any( "Oranges" ),
332cdf0e10cSrcweir                              new uno.Any( (Double) 9.0 ) },
333cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Bob" ),
334cdf0e10cSrcweir                              new uno.Any( "Oranges" ),
335cdf0e10cSrcweir                              new uno.Any( (Double) 3.0 ) },
336cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Alice" ),
337cdf0e10cSrcweir                              new uno.Any( "Oranges" ),
338cdf0e10cSrcweir                              new uno.Any( (Double) 4.0 ) },
339cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Alice" ),
340cdf0e10cSrcweir                              new uno.Any( "Apples" ),
341cdf0e10cSrcweir                              new uno.Any( (Double) 9.0 ) }
342cdf0e10cSrcweir         };
343cdf0e10cSrcweir         xData.setDataArray( aValues );
344cdf0e10cSrcweir 
345cdf0e10cSrcweir 
346cdf0e10cSrcweir         // --- Get cell range address. ---
347cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XCellRangeAddressable xRangeAddr =
348cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XCellRangeAddressable) xCellRange;
349cdf0e10cSrcweir         aRangeAddress = xRangeAddr.getRangeAddress();
350cdf0e10cSrcweir         Console.WriteLine(
351cdf0e10cSrcweir             "Address of this range:  Sheet=" + aRangeAddress.Sheet );
352cdf0e10cSrcweir         Console.WriteLine(
353cdf0e10cSrcweir             "Start column=" + aRangeAddress.StartColumn + ";  Start row=" +
354cdf0e10cSrcweir             aRangeAddress.StartRow );
355cdf0e10cSrcweir         Console.WriteLine(
356cdf0e10cSrcweir             "End column  =" + aRangeAddress.EndColumn   + ";  End row  =" +
357cdf0e10cSrcweir             aRangeAddress.EndRow );
358cdf0e10cSrcweir 
359cdf0e10cSrcweir 
360cdf0e10cSrcweir         // --- Sheet operation. ---
361cdf0e10cSrcweir         // uses the range filled with XCellRangeData
362cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XSheetOperation xSheetOp =
363cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XSheetOperation) xData;
364cdf0e10cSrcweir         double fResult = xSheetOp.computeFunction(
365cdf0e10cSrcweir             unoidl.com.sun.star.sheet.GeneralFunction.AVERAGE );
366cdf0e10cSrcweir         Console.WriteLine(
367cdf0e10cSrcweir             "Average value of the data table A10:C30: " + fResult );
368cdf0e10cSrcweir 
369cdf0e10cSrcweir 
370cdf0e10cSrcweir         // --- Fill series ---
371cdf0e10cSrcweir         // Prepare the example
372cdf0e10cSrcweir         setValue( xSheet, "E10", 1 );
373cdf0e10cSrcweir         setValue( xSheet, "E11", 4 );
374cdf0e10cSrcweir         setDate( xSheet, "E12", 30, 1, 2002 );
375cdf0e10cSrcweir         setFormula( xSheet, "I13", "Text 10" );
376cdf0e10cSrcweir         setFormula( xSheet, "E14", "Jan" );
377cdf0e10cSrcweir         setValue( xSheet, "K14", 10 );
378cdf0e10cSrcweir         setValue( xSheet, "E16", 1 );
379cdf0e10cSrcweir         setValue( xSheet, "F16", 2 );
380cdf0e10cSrcweir         setDate( xSheet, "E17", 28, 2, 2002 );
381cdf0e10cSrcweir         setDate( xSheet, "F17", 28, 1, 2002 );
382cdf0e10cSrcweir         setValue( xSheet, "E18", 6 );
383cdf0e10cSrcweir         setValue( xSheet, "F18", 4 );
384cdf0e10cSrcweir 
385cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XCellSeries xSeries = null;
386cdf0e10cSrcweir         // Fill 2 rows linear with end value
387cdf0e10cSrcweir         // -> 2nd series is not filled completely
388cdf0e10cSrcweir         xSeries = getCellSeries( xSheet, "E10:I11" );
389cdf0e10cSrcweir         xSeries.fillSeries(
390cdf0e10cSrcweir             unoidl.com.sun.star.sheet.FillDirection.TO_RIGHT,
391cdf0e10cSrcweir             unoidl.com.sun.star.sheet.FillMode.LINEAR,
392cdf0e10cSrcweir             unoidl.com.sun.star.sheet.FillDateMode.FILL_DATE_DAY, 2, 9 );
393cdf0e10cSrcweir         // Add months to a date
394cdf0e10cSrcweir         xSeries = getCellSeries( xSheet, "E12:I12" );
395cdf0e10cSrcweir         xSeries.fillSeries(
396cdf0e10cSrcweir             unoidl.com.sun.star.sheet.FillDirection.TO_RIGHT,
397cdf0e10cSrcweir             unoidl.com.sun.star.sheet.FillMode.DATE,
398cdf0e10cSrcweir             unoidl.com.sun.star.sheet.FillDateMode.FILL_DATE_MONTH,
399cdf0e10cSrcweir             1, 0x7FFFFFFF );
400cdf0e10cSrcweir         // Fill right to left with a text containing a value
401cdf0e10cSrcweir         xSeries = getCellSeries( xSheet, "E13:I13" );
402cdf0e10cSrcweir         xSeries.fillSeries(
403cdf0e10cSrcweir             unoidl.com.sun.star.sheet.FillDirection.TO_LEFT,
404cdf0e10cSrcweir             unoidl.com.sun.star.sheet.FillMode.LINEAR,
405cdf0e10cSrcweir             unoidl.com.sun.star.sheet.FillDateMode.FILL_DATE_DAY,
406cdf0e10cSrcweir             10, 0x7FFFFFFF );
407cdf0e10cSrcweir         // Fill with an user defined list
408cdf0e10cSrcweir         xSeries = getCellSeries( xSheet, "E14:I14" );
409cdf0e10cSrcweir         xSeries.fillSeries(
410cdf0e10cSrcweir             unoidl.com.sun.star.sheet.FillDirection.TO_RIGHT,
411cdf0e10cSrcweir             unoidl.com.sun.star.sheet.FillMode.AUTO,
412cdf0e10cSrcweir             unoidl.com.sun.star.sheet.FillDateMode.FILL_DATE_DAY,
413cdf0e10cSrcweir             1, 0x7FFFFFFF );
414cdf0e10cSrcweir         // Fill bottom to top with a geometric series
415cdf0e10cSrcweir         xSeries = getCellSeries( xSheet, "K10:K14" );
416cdf0e10cSrcweir         xSeries.fillSeries(
417cdf0e10cSrcweir             unoidl.com.sun.star.sheet.FillDirection.TO_TOP,
418cdf0e10cSrcweir             unoidl.com.sun.star.sheet.FillMode.GROWTH,
419cdf0e10cSrcweir             unoidl.com.sun.star.sheet.FillDateMode.FILL_DATE_DAY,
420cdf0e10cSrcweir             2, 0x7FFFFFFF );
421cdf0e10cSrcweir         // Auto fill
422cdf0e10cSrcweir         xSeries = getCellSeries( xSheet, "E16:K18" );
423cdf0e10cSrcweir         xSeries.fillAuto(
424cdf0e10cSrcweir             unoidl.com.sun.star.sheet.FillDirection.TO_RIGHT, 2 );
425cdf0e10cSrcweir         // Fill series copies cell formats -> draw border here
426cdf0e10cSrcweir         prepareRange( xSheet, "E9:K18", "XCellSeries" );
427cdf0e10cSrcweir 
428cdf0e10cSrcweir 
429cdf0e10cSrcweir         // --- Array formulas ---
430cdf0e10cSrcweir         xCellRange = xSheet.getCellRangeByName( "E21:G23" );
431cdf0e10cSrcweir         prepareRange( xSheet, "E20:G23", "XArrayFormulaRange" );
432cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XArrayFormulaRange xArrayFormula =
433cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XArrayFormulaRange) xCellRange;
434cdf0e10cSrcweir         // Insert a 3x3 unit matrix.
435cdf0e10cSrcweir         xArrayFormula.setArrayFormula( "=A10:C12" );
436cdf0e10cSrcweir         Console.WriteLine(
437cdf0e10cSrcweir             "Array formula is: " + xArrayFormula.getArrayFormula() );
438cdf0e10cSrcweir 
439cdf0e10cSrcweir 
440cdf0e10cSrcweir         // --- Multiple operations ---
441cdf0e10cSrcweir         setFormula( xSheet, "E26", "=E27^F26" );
442cdf0e10cSrcweir         setValue( xSheet, "E27", 1 );
443cdf0e10cSrcweir         setValue( xSheet, "F26", 1 );
444cdf0e10cSrcweir         getCellSeries( xSheet, "E27:E31" ).fillAuto(
445cdf0e10cSrcweir             unoidl.com.sun.star.sheet.FillDirection.TO_BOTTOM, 1 );
446cdf0e10cSrcweir         getCellSeries( xSheet, "F26:J26" ).fillAuto(
447cdf0e10cSrcweir             unoidl.com.sun.star.sheet.FillDirection.TO_RIGHT, 1 );
448cdf0e10cSrcweir         setFormula( xSheet, "F33", "=SIN(E33)" );
449cdf0e10cSrcweir         setFormula( xSheet, "G33", "=COS(E33)" );
450cdf0e10cSrcweir         setFormula( xSheet, "H33", "=TAN(E33)" );
451cdf0e10cSrcweir         setValue( xSheet, "E34", 0 );
452cdf0e10cSrcweir         setValue( xSheet, "E35", 0.2 );
453cdf0e10cSrcweir         getCellSeries( xSheet, "E34:E38" ).fillAuto(
454cdf0e10cSrcweir             unoidl.com.sun.star.sheet.FillDirection.TO_BOTTOM, 2 );
455cdf0e10cSrcweir         prepareRange( xSheet, "E25:J38", "XMultipleOperation" );
456cdf0e10cSrcweir 
457cdf0e10cSrcweir         unoidl.com.sun.star.table.CellRangeAddress aFormulaRange =
458cdf0e10cSrcweir             createCellRangeAddress( xSheet, "E26" );
459cdf0e10cSrcweir         unoidl.com.sun.star.table.CellAddress aColCell =
460cdf0e10cSrcweir             createCellAddress( xSheet, "E27" );
461cdf0e10cSrcweir         unoidl.com.sun.star.table.CellAddress aRowCell =
462cdf0e10cSrcweir             createCellAddress( xSheet, "F26" );
463cdf0e10cSrcweir 
464cdf0e10cSrcweir         xCellRange = xSheet.getCellRangeByName( "E26:J31" );
465cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XMultipleOperation xMultOp =
466cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XMultipleOperation) xCellRange;
467cdf0e10cSrcweir         xMultOp.setTableOperation(
468cdf0e10cSrcweir             aFormulaRange, unoidl.com.sun.star.sheet.TableOperationMode.BOTH,
469cdf0e10cSrcweir             aColCell, aRowCell );
470cdf0e10cSrcweir 
471cdf0e10cSrcweir         aFormulaRange = createCellRangeAddress( xSheet, "F33:H33" );
472cdf0e10cSrcweir         aColCell = createCellAddress( xSheet, "E33" );
473cdf0e10cSrcweir         // Row cell not needed
474cdf0e10cSrcweir 
475cdf0e10cSrcweir         xCellRange = xSheet.getCellRangeByName( "E34:H38" );
476cdf0e10cSrcweir         xMultOp = (unoidl.com.sun.star.sheet.XMultipleOperation) xCellRange;
477cdf0e10cSrcweir         xMultOp.setTableOperation(
478cdf0e10cSrcweir             aFormulaRange, unoidl.com.sun.star.sheet.TableOperationMode.COLUMN,
479cdf0e10cSrcweir             aColCell, aRowCell );
480cdf0e10cSrcweir 
481cdf0e10cSrcweir 
482cdf0e10cSrcweir         // --- Cell Ranges Query ---
483cdf0e10cSrcweir         xCellRange = xSheet.getCellRangeByName( "A10:C30" );
484cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XCellRangesQuery xRangesQuery =
485cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XCellRangesQuery) xCellRange;
486cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XSheetCellRanges xCellRanges =
487cdf0e10cSrcweir             xRangesQuery.queryContentCells(
488cdf0e10cSrcweir                 (short) unoidl.com.sun.star.sheet.CellFlags.STRING );
489cdf0e10cSrcweir         Console.WriteLine(
490cdf0e10cSrcweir             "Cells in A10:C30 containing text: "
491cdf0e10cSrcweir             + xCellRanges.getRangeAddressesAsString() );
492cdf0e10cSrcweir     }
493cdf0e10cSrcweir 
494cdf0e10cSrcweir     /** Returns the XCellSeries interface of a cell range.
495cdf0e10cSrcweir         @param xSheet  The spreadsheet containing the cell range.
496cdf0e10cSrcweir         @param aRange  The address of the cell range.
497cdf0e10cSrcweir         @return  The XCellSeries interface. */
getCellSeries( unoidl.com.sun.star.sheet.XSpreadsheet xSheet, String aRange )498cdf0e10cSrcweir     private unoidl.com.sun.star.sheet.XCellSeries getCellSeries(
499cdf0e10cSrcweir             unoidl.com.sun.star.sheet.XSpreadsheet xSheet, String aRange )
500cdf0e10cSrcweir     {
501cdf0e10cSrcweir         return (unoidl.com.sun.star.sheet.XCellSeries)
502cdf0e10cSrcweir             xSheet.getCellRangeByName( aRange );
503cdf0e10cSrcweir     }
504cdf0e10cSrcweir 
505cdf0e10cSrcweir // ________________________________________________________________
506cdf0e10cSrcweir 
507cdf0e10cSrcweir     /** All samples regarding cell range collections. */
doCellRangesSamples()508cdf0e10cSrcweir     private void doCellRangesSamples()
509cdf0e10cSrcweir     {
510cdf0e10cSrcweir         Console.WriteLine( "\n*** Samples for cell range collections ***\n" );
511cdf0e10cSrcweir 
512cdf0e10cSrcweir         // Create a new cell range container
513cdf0e10cSrcweir         unoidl.com.sun.star.lang.XMultiServiceFactory xDocFactory =
514cdf0e10cSrcweir             (unoidl.com.sun.star.lang.XMultiServiceFactory) getDocument();
515cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XSheetCellRangeContainer xRangeCont =
516cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XSheetCellRangeContainer)
517cdf0e10cSrcweir             xDocFactory.createInstance(
518cdf0e10cSrcweir                 "com.sun.star.sheet.SheetCellRanges" );
519cdf0e10cSrcweir 
520cdf0e10cSrcweir 
521cdf0e10cSrcweir         // --- Insert ranges ---
522cdf0e10cSrcweir         insertRange( xRangeCont, 0, 0, 0, 0, 0, false );    // A1:A1
523cdf0e10cSrcweir         insertRange( xRangeCont, 0, 0, 1, 0, 2, true );     // A2:A3
524cdf0e10cSrcweir         insertRange( xRangeCont, 0, 1, 0, 1, 2, false );    // B1:B3
525cdf0e10cSrcweir 
526cdf0e10cSrcweir 
527cdf0e10cSrcweir         // --- Query the list of filled cells ---
528cdf0e10cSrcweir         Console.WriteLine( "All filled cells: " );
529cdf0e10cSrcweir         unoidl.com.sun.star.container.XEnumerationAccess xCellsEA =
530cdf0e10cSrcweir             xRangeCont.getCells();
531cdf0e10cSrcweir         unoidl.com.sun.star.container.XEnumeration xEnum =
532cdf0e10cSrcweir             xCellsEA.createEnumeration();
533cdf0e10cSrcweir         while( xEnum.hasMoreElements() )
534cdf0e10cSrcweir         {
535cdf0e10cSrcweir             uno.Any aCellObj = xEnum.nextElement();
536cdf0e10cSrcweir             unoidl.com.sun.star.sheet.XCellAddressable xAddr =
537cdf0e10cSrcweir                 (unoidl.com.sun.star.sheet.XCellAddressable) aCellObj.Value;
538cdf0e10cSrcweir             unoidl.com.sun.star.table.CellAddress aAddr =
539cdf0e10cSrcweir                 xAddr.getCellAddress();
540cdf0e10cSrcweir             Console.WriteLine(
541cdf0e10cSrcweir                 getCellAddressString( aAddr.Column, aAddr.Row ) + " " );
542cdf0e10cSrcweir         }
543cdf0e10cSrcweir         Console.WriteLine();
544cdf0e10cSrcweir     }
545cdf0e10cSrcweir 
546cdf0e10cSrcweir     /** Inserts a cell range address into a cell range container and prints
547cdf0e10cSrcweir         a message.
548cdf0e10cSrcweir         @param xContainer  unoidl.com.sun.star.sheet.XSheetCellRangeContainer
549cdf0e10cSrcweir                            interface of the container.
550cdf0e10cSrcweir         @param nSheet  Index of sheet of the range.
551cdf0e10cSrcweir         @param nStartCol  Index of first column of the range.
552cdf0e10cSrcweir         @param nStartRow  Index of first row of the range.
553cdf0e10cSrcweir         @param nEndCol  Index of last column of the range.
554cdf0e10cSrcweir         @param nEndRow  Index of last row of the range.
555cdf0e10cSrcweir         @param bMerge  Determines whether the new range should be merged
556cdf0e10cSrcweir                        with the existing ranges.
557cdf0e10cSrcweir     */
insertRange( unoidl.com.sun.star.sheet.XSheetCellRangeContainer xContainer, int nSheet, int nStartCol, int nStartRow, int nEndCol, int nEndRow, bool bMerge )558cdf0e10cSrcweir     private void insertRange(
559cdf0e10cSrcweir             unoidl.com.sun.star.sheet.XSheetCellRangeContainer xContainer,
560cdf0e10cSrcweir             int nSheet, int nStartCol, int nStartRow, int nEndCol, int nEndRow,
561cdf0e10cSrcweir             bool bMerge )
562cdf0e10cSrcweir     {
563cdf0e10cSrcweir         unoidl.com.sun.star.table.CellRangeAddress aAddress =
564cdf0e10cSrcweir             new unoidl.com.sun.star.table.CellRangeAddress();
565cdf0e10cSrcweir         aAddress.Sheet = (short)nSheet;
566cdf0e10cSrcweir         aAddress.StartColumn = nStartCol;
567cdf0e10cSrcweir         aAddress.StartRow = nStartRow;
568cdf0e10cSrcweir         aAddress.EndColumn = nEndCol;
569cdf0e10cSrcweir         aAddress.EndRow = nEndRow;
570cdf0e10cSrcweir         xContainer.addRangeAddress( aAddress, bMerge );
571cdf0e10cSrcweir         Console.WriteLine(
572cdf0e10cSrcweir             "Inserting " + getCellRangeAddressString( aAddress )
573cdf0e10cSrcweir             + " " + (bMerge ? "   with" : "without") + " merge,"
574cdf0e10cSrcweir             + " resulting list: " + xContainer.getRangeAddressesAsString() );
575cdf0e10cSrcweir     }
576cdf0e10cSrcweir 
577cdf0e10cSrcweir // ________________________________________________________________
578cdf0e10cSrcweir 
579cdf0e10cSrcweir     /** All samples regarding cell cursors. */
doCellCursorSamples()580cdf0e10cSrcweir     private void doCellCursorSamples()
581cdf0e10cSrcweir     {
582cdf0e10cSrcweir         Console.WriteLine( "\n*** Samples for cell cursor ***\n" );
583cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XSpreadsheet xSheet = getSpreadsheet( 0 );
584cdf0e10cSrcweir 
585cdf0e10cSrcweir 
586cdf0e10cSrcweir         // --- Find the array formula using a cell cursor ---
587cdf0e10cSrcweir         unoidl.com.sun.star.table.XCellRange xRange =
588cdf0e10cSrcweir             xSheet.getCellRangeByName( "F22" );
589cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XSheetCellRange xCellRange =
590cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XSheetCellRange) xRange;
591cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XSheetCellCursor xCursor =
592cdf0e10cSrcweir             xSheet.createCursorByRange( xCellRange );
593cdf0e10cSrcweir 
594cdf0e10cSrcweir         xCursor.collapseToCurrentArray();
595cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XArrayFormulaRange xArray =
596cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XArrayFormulaRange) xCursor;
597cdf0e10cSrcweir         Console.WriteLine(
598cdf0e10cSrcweir             "Array formula in " + getCellRangeAddressString( xCursor, false )
599cdf0e10cSrcweir             + " contains formula " + xArray.getArrayFormula() );
600cdf0e10cSrcweir 
601cdf0e10cSrcweir 
602cdf0e10cSrcweir         // --- Find the used area ---
603cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XUsedAreaCursor xUsedCursor =
604cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XUsedAreaCursor) xCursor;
605cdf0e10cSrcweir         xUsedCursor.gotoStartOfUsedArea( false );
606cdf0e10cSrcweir         xUsedCursor.gotoEndOfUsedArea( true );
607cdf0e10cSrcweir         // xUsedCursor and xCursor are interfaces of the same object -
608cdf0e10cSrcweir         // so modifying xUsedCursor takes effect on xCursor:
609cdf0e10cSrcweir         Console.WriteLine(
610cdf0e10cSrcweir             "The used area is: " + getCellRangeAddressString( xCursor, true ) );
611cdf0e10cSrcweir     }
612cdf0e10cSrcweir 
613cdf0e10cSrcweir // ________________________________________________________________
614cdf0e10cSrcweir 
615cdf0e10cSrcweir     /** All samples regarding the formatting of cells and ranges. */
doFormattingSamples()616cdf0e10cSrcweir     private void doFormattingSamples()
617cdf0e10cSrcweir     {
618cdf0e10cSrcweir         Console.WriteLine( "\n*** Formatting samples ***\n" );
619cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XSpreadsheet xSheet = getSpreadsheet( 1 );
620cdf0e10cSrcweir         unoidl.com.sun.star.table.XCellRange xCellRange;
621cdf0e10cSrcweir         unoidl.com.sun.star.beans.XPropertySet xPropSet = null;
622cdf0e10cSrcweir         unoidl.com.sun.star.container.XIndexAccess xRangeIA = null;
623cdf0e10cSrcweir         unoidl.com.sun.star.lang.XMultiServiceFactory xServiceManager;
624cdf0e10cSrcweir 
625cdf0e10cSrcweir 
626cdf0e10cSrcweir         // --- Cell styles ---
627cdf0e10cSrcweir         // get the cell style container
628cdf0e10cSrcweir         unoidl.com.sun.star.style.XStyleFamiliesSupplier xFamiliesSupplier =
629cdf0e10cSrcweir             (unoidl.com.sun.star.style.XStyleFamiliesSupplier) getDocument();
630cdf0e10cSrcweir         unoidl.com.sun.star.container.XNameAccess xFamiliesNA =
631cdf0e10cSrcweir             xFamiliesSupplier.getStyleFamilies();
632cdf0e10cSrcweir         uno.Any aCellStylesObj = xFamiliesNA.getByName( "CellStyles" );
633cdf0e10cSrcweir         unoidl.com.sun.star.container.XNameContainer xCellStylesNA =
634cdf0e10cSrcweir             (unoidl.com.sun.star.container.XNameContainer) aCellStylesObj.Value;
635cdf0e10cSrcweir 
636cdf0e10cSrcweir         // create a new cell style
637cdf0e10cSrcweir         xServiceManager =
638cdf0e10cSrcweir             (unoidl.com.sun.star.lang.XMultiServiceFactory) getDocument();
639cdf0e10cSrcweir         Object aCellStyle = xServiceManager.createInstance(
640cdf0e10cSrcweir             "com.sun.star.style.CellStyle" );
641cdf0e10cSrcweir         String aStyleName = "MyNewCellStyle";
642cdf0e10cSrcweir         xCellStylesNA.insertByName(
643cdf0e10cSrcweir             aStyleName, new uno.Any( typeof (Object), aCellStyle ) );
644cdf0e10cSrcweir 
645cdf0e10cSrcweir         // modify properties of the new style
646cdf0e10cSrcweir         xPropSet = (unoidl.com.sun.star.beans.XPropertySet) aCellStyle;
647cdf0e10cSrcweir         xPropSet.setPropertyValue(
648cdf0e10cSrcweir             "CellBackColor", new uno.Any( (Int32) 0x888888 ) );
649cdf0e10cSrcweir         xPropSet.setPropertyValue(
650cdf0e10cSrcweir             "IsCellBackgroundTransparent", new uno.Any( false ) );
651cdf0e10cSrcweir 
652cdf0e10cSrcweir 
653cdf0e10cSrcweir 
654cdf0e10cSrcweir         // --- Query equal-formatted cell ranges ---
655cdf0e10cSrcweir         // prepare example, use the new cell style
656cdf0e10cSrcweir         xCellRange = xSheet.getCellRangeByName( "D2:F2" );
657cdf0e10cSrcweir         xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCellRange;
658cdf0e10cSrcweir         xPropSet.setPropertyValue( "CellStyle", new uno.Any( aStyleName ) );
659cdf0e10cSrcweir 
660cdf0e10cSrcweir         xCellRange = xSheet.getCellRangeByName( "A3:G3" );
661cdf0e10cSrcweir         xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCellRange;
662cdf0e10cSrcweir         xPropSet.setPropertyValue( "CellStyle", new uno.Any( aStyleName ) );
663cdf0e10cSrcweir 
664cdf0e10cSrcweir         // All ranges in one container
665cdf0e10cSrcweir         xCellRange = xSheet.getCellRangeByName( "A1:G3" );
666cdf0e10cSrcweir         Console.WriteLine( "Service CellFormatRanges:" );
667cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XCellFormatRangesSupplier xFormatSupp =
668cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XCellFormatRangesSupplier) xCellRange;
669cdf0e10cSrcweir         xRangeIA = xFormatSupp.getCellFormatRanges();
670cdf0e10cSrcweir         Console.WriteLine( getCellRangeListString( xRangeIA ) );
671cdf0e10cSrcweir 
672cdf0e10cSrcweir         // Ranges sorted in SheetCellRanges containers
673cdf0e10cSrcweir         Console.WriteLine( "\nService UniqueCellFormatRanges:" );
674cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XUniqueCellFormatRangesSupplier
675cdf0e10cSrcweir             xUniqueFormatSupp =
676cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XUniqueCellFormatRangesSupplier)
677cdf0e10cSrcweir               xCellRange;
678cdf0e10cSrcweir         unoidl.com.sun.star.container.XIndexAccess xRangesIA =
679cdf0e10cSrcweir             xUniqueFormatSupp.getUniqueCellFormatRanges();
680cdf0e10cSrcweir         int nCount = xRangesIA.getCount();
681cdf0e10cSrcweir         for (int nIndex = 0; nIndex < nCount; ++nIndex)
682cdf0e10cSrcweir         {
683cdf0e10cSrcweir             uno.Any aRangesObj = xRangesIA.getByIndex( nIndex );
684cdf0e10cSrcweir             xRangeIA =
685cdf0e10cSrcweir                 (unoidl.com.sun.star.container.XIndexAccess) aRangesObj.Value;
686cdf0e10cSrcweir             Console.WriteLine(
687cdf0e10cSrcweir                 "Container " + (nIndex + 1) + ": " +
688cdf0e10cSrcweir                 getCellRangeListString( xRangeIA ) );
689cdf0e10cSrcweir         }
690cdf0e10cSrcweir 
691cdf0e10cSrcweir 
692cdf0e10cSrcweir         // --- Table auto formats ---
693cdf0e10cSrcweir         // get the global collection of table auto formats,
694cdf0e10cSrcweir         // use global service manager
695cdf0e10cSrcweir         xServiceManager = getServiceManager();
696cdf0e10cSrcweir         Object aAutoFormatsObj = xServiceManager.createInstance(
697cdf0e10cSrcweir             "com.sun.star.sheet.TableAutoFormats" );
698cdf0e10cSrcweir         unoidl.com.sun.star.container.XNameContainer xAutoFormatsNA =
699cdf0e10cSrcweir             (unoidl.com.sun.star.container.XNameContainer) aAutoFormatsObj;
700cdf0e10cSrcweir 
701cdf0e10cSrcweir         // create a new table auto format and insert into the container
702cdf0e10cSrcweir         String aAutoFormatName =  "Temp_Example";
703cdf0e10cSrcweir         bool bExistsAlready = xAutoFormatsNA.hasByName( aAutoFormatName );
704cdf0e10cSrcweir         uno.Any aAutoFormatObj;
705cdf0e10cSrcweir         if (bExistsAlready)
706cdf0e10cSrcweir             // auto format already exists -> use it
707cdf0e10cSrcweir             aAutoFormatObj = xAutoFormatsNA.getByName( aAutoFormatName );
708cdf0e10cSrcweir         else
709cdf0e10cSrcweir         {
710cdf0e10cSrcweir             // create a new auto format (with document service manager!)
711cdf0e10cSrcweir             xServiceManager =
712cdf0e10cSrcweir                 (unoidl.com.sun.star.lang.XMultiServiceFactory) getDocument();
713cdf0e10cSrcweir             aAutoFormatObj = new uno.Any(
714cdf0e10cSrcweir                 typeof (Object),
715cdf0e10cSrcweir                 xServiceManager.createInstance(
716cdf0e10cSrcweir                     "com.sun.star.sheet.TableAutoFormat" ) );
717cdf0e10cSrcweir             xAutoFormatsNA.insertByName( aAutoFormatName, aAutoFormatObj );
718cdf0e10cSrcweir         }
719cdf0e10cSrcweir         // index access to the auto format fields
720cdf0e10cSrcweir         unoidl.com.sun.star.container.XIndexAccess xAutoFormatIA =
721cdf0e10cSrcweir             (unoidl.com.sun.star.container.XIndexAccess) aAutoFormatObj.Value;
722cdf0e10cSrcweir 
723cdf0e10cSrcweir         // set properties of all auto format fields
724cdf0e10cSrcweir         for (int nRow = 0; nRow < 4; ++nRow)
725cdf0e10cSrcweir         {
726cdf0e10cSrcweir             int nRowColor = 0;
727cdf0e10cSrcweir             switch (nRow)
728cdf0e10cSrcweir             {
729cdf0e10cSrcweir                 case 0:     nRowColor = 0x999999;   break;
730cdf0e10cSrcweir                 case 1:     nRowColor = 0xFFFFCC;   break;
731cdf0e10cSrcweir                 case 2:     nRowColor = 0xEEEEEE;   break;
732cdf0e10cSrcweir                 case 3:     nRowColor = 0x999999;   break;
733cdf0e10cSrcweir             }
734cdf0e10cSrcweir 
735cdf0e10cSrcweir             for (int nColumn = 0; nColumn < 4; ++nColumn)
736cdf0e10cSrcweir             {
737cdf0e10cSrcweir                 int nColor = nRowColor;
738cdf0e10cSrcweir                 if ((nColumn == 0) || (nColumn == 3))
739cdf0e10cSrcweir                     nColor -= 0x333300;
740cdf0e10cSrcweir 
741cdf0e10cSrcweir                 // get the auto format field and apply properties
742cdf0e10cSrcweir                 uno.Any aFieldObj = xAutoFormatIA.getByIndex(
743cdf0e10cSrcweir                     4 * nRow + nColumn );
744cdf0e10cSrcweir                 xPropSet =
745cdf0e10cSrcweir                     (unoidl.com.sun.star.beans.XPropertySet) aFieldObj.Value;
746cdf0e10cSrcweir                 xPropSet.setPropertyValue(
747cdf0e10cSrcweir                     "CellBackColor", new uno.Any( (Int32) nColor ) );
748cdf0e10cSrcweir             }
749cdf0e10cSrcweir         }
750cdf0e10cSrcweir 
751cdf0e10cSrcweir         // set the auto format to the spreadsheet
752cdf0e10cSrcweir         xCellRange = xSheet.getCellRangeByName( "A5:H25" );
753cdf0e10cSrcweir         unoidl.com.sun.star.table.XAutoFormattable xAutoForm =
754cdf0e10cSrcweir             (unoidl.com.sun.star.table.XAutoFormattable) xCellRange;
755cdf0e10cSrcweir         xAutoForm.autoFormat( aAutoFormatName );
756cdf0e10cSrcweir 
757cdf0e10cSrcweir         // remove the auto format
758cdf0e10cSrcweir         if (!bExistsAlready)
759cdf0e10cSrcweir             xAutoFormatsNA.removeByName( aAutoFormatName );
760cdf0e10cSrcweir 
761cdf0e10cSrcweir 
762cdf0e10cSrcweir         // --- Conditional formats ---
763cdf0e10cSrcweir         xSheet = getSpreadsheet( 0 );
764cdf0e10cSrcweir         prepareRange( xSheet, "K20:K23", "Cond. Format" );
765cdf0e10cSrcweir         setValue( xSheet, "K21", 1 );
766cdf0e10cSrcweir         setValue( xSheet, "K22", 2 );
767cdf0e10cSrcweir         setValue( xSheet, "K23", 3 );
768cdf0e10cSrcweir 
769cdf0e10cSrcweir         // get the conditional format object of the cell range
770cdf0e10cSrcweir         xCellRange = xSheet.getCellRangeByName( "K21:K23" );
771cdf0e10cSrcweir         xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCellRange;
772cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XSheetConditionalEntries xEntries =
773cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XSheetConditionalEntries)
774cdf0e10cSrcweir               xPropSet.getPropertyValue( "ConditionalFormat" ).Value;
775cdf0e10cSrcweir 
776cdf0e10cSrcweir         // create a condition and apply it to the range
777cdf0e10cSrcweir         unoidl.com.sun.star.beans.PropertyValue[] aCondition =
778cdf0e10cSrcweir             new unoidl.com.sun.star.beans.PropertyValue[3];
779cdf0e10cSrcweir         aCondition[0] = new unoidl.com.sun.star.beans.PropertyValue();
780cdf0e10cSrcweir         aCondition[0].Name  = "Operator";
781cdf0e10cSrcweir         aCondition[0].Value =
782cdf0e10cSrcweir             new uno.Any(
783cdf0e10cSrcweir                 typeof (unoidl.com.sun.star.sheet.ConditionOperator),
784cdf0e10cSrcweir                 unoidl.com.sun.star.sheet.ConditionOperator.GREATER );
785cdf0e10cSrcweir         aCondition[1] = new unoidl.com.sun.star.beans.PropertyValue();
786cdf0e10cSrcweir         aCondition[1].Name  = "Formula1";
787cdf0e10cSrcweir         aCondition[1].Value = new uno.Any( "1" );
788cdf0e10cSrcweir         aCondition[2] = new unoidl.com.sun.star.beans.PropertyValue();
789cdf0e10cSrcweir         aCondition[2].Name  = "StyleName";
790cdf0e10cSrcweir         aCondition[2].Value = new uno.Any( aStyleName );
791cdf0e10cSrcweir         xEntries.addNew( aCondition );
792cdf0e10cSrcweir         xPropSet.setPropertyValue(
793cdf0e10cSrcweir             "ConditionalFormat",
794cdf0e10cSrcweir             new uno.Any(
795cdf0e10cSrcweir                 typeof (unoidl.com.sun.star.sheet.XSheetConditionalEntries),
796cdf0e10cSrcweir                 xEntries ) );
797cdf0e10cSrcweir     }
798cdf0e10cSrcweir 
799cdf0e10cSrcweir // ________________________________________________________________
800cdf0e10cSrcweir 
801cdf0e10cSrcweir     /** All samples regarding the spreadsheet document. */
doDocumentSamples()802cdf0e10cSrcweir     private void doDocumentSamples()
803cdf0e10cSrcweir     {
804cdf0e10cSrcweir         Console.WriteLine( "\n*** Samples for spreadsheet document ***\n" );
805cdf0e10cSrcweir 
806cdf0e10cSrcweir 
807cdf0e10cSrcweir         // --- Insert a new spreadsheet ---
808cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XSpreadsheet xSheet =
809cdf0e10cSrcweir             insertSpreadsheet( "A new sheet", (short) 0x7FFF );
810cdf0e10cSrcweir 
811cdf0e10cSrcweir 
812cdf0e10cSrcweir         // --- Copy a cell range ---
813cdf0e10cSrcweir         prepareRange( xSheet, "A1:B3", "Copy from" );
814cdf0e10cSrcweir         prepareRange( xSheet, "D1:E3", "To" );
815cdf0e10cSrcweir         setValue( xSheet, "A2", 123 );
816cdf0e10cSrcweir         setValue( xSheet, "B2", 345 );
817cdf0e10cSrcweir         setFormula( xSheet, "A3", "=SUM(A2:B2)" );
818cdf0e10cSrcweir         setFormula( xSheet, "B3", "=FORMULA(A3)" );
819cdf0e10cSrcweir 
820cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XCellRangeMovement xMovement =
821cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XCellRangeMovement) xSheet;
822cdf0e10cSrcweir         unoidl.com.sun.star.table.CellRangeAddress aSourceRange =
823cdf0e10cSrcweir             createCellRangeAddress( xSheet, "A2:B3" );
824cdf0e10cSrcweir         unoidl.com.sun.star.table.CellAddress aDestCell =
825cdf0e10cSrcweir             createCellAddress( xSheet, "D2" );
826cdf0e10cSrcweir         xMovement.copyRange( aDestCell, aSourceRange );
827cdf0e10cSrcweir 
828cdf0e10cSrcweir 
829cdf0e10cSrcweir         // --- Print automatic column page breaks ---
830cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XSheetPageBreak xPageBreak =
831cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XSheetPageBreak) xSheet;
832cdf0e10cSrcweir         unoidl.com.sun.star.sheet.TablePageBreakData[] aPageBreakArray =
833cdf0e10cSrcweir             xPageBreak.getColumnPageBreaks();
834cdf0e10cSrcweir 
835cdf0e10cSrcweir         Console.Write( "Automatic column page breaks:" );
836cdf0e10cSrcweir         for (int nIndex = 0; nIndex < aPageBreakArray.Length; ++nIndex)
837cdf0e10cSrcweir             if (!aPageBreakArray[nIndex].ManualBreak)
838cdf0e10cSrcweir                 Console.Write( " " + aPageBreakArray[nIndex].Position );
839cdf0e10cSrcweir         Console.WriteLine();
840cdf0e10cSrcweir 
841cdf0e10cSrcweir 
842cdf0e10cSrcweir         // --- Document properties ---
843cdf0e10cSrcweir         unoidl.com.sun.star.beans.XPropertySet xPropSet =
844cdf0e10cSrcweir             (unoidl.com.sun.star.beans.XPropertySet) getDocument();
845cdf0e10cSrcweir 
846cdf0e10cSrcweir         String aText = "Value of property IsIterationEnabled: ";
847cdf0e10cSrcweir         aText +=
848cdf0e10cSrcweir             (Boolean) xPropSet.getPropertyValue( "IsIterationEnabled" ).Value;
849cdf0e10cSrcweir         Console.WriteLine( aText );
850cdf0e10cSrcweir         aText = "Value of property IterationCount: ";
851cdf0e10cSrcweir         aText += (Int32) xPropSet.getPropertyValue( "IterationCount" ).Value;
852cdf0e10cSrcweir         Console.WriteLine( aText );
853cdf0e10cSrcweir         aText = "Value of property NullDate: ";
854cdf0e10cSrcweir         unoidl.com.sun.star.util.Date aDate = (unoidl.com.sun.star.util.Date)
855cdf0e10cSrcweir             xPropSet.getPropertyValue( "NullDate" ).Value;
856cdf0e10cSrcweir         aText += aDate.Year + "-" + aDate.Month + "-" + aDate.Day;
857cdf0e10cSrcweir         Console.WriteLine( aText );
858cdf0e10cSrcweir 
859cdf0e10cSrcweir 
860cdf0e10cSrcweir         // --- Data validation ---
861cdf0e10cSrcweir         prepareRange( xSheet, "A5:C7", "Validation" );
862cdf0e10cSrcweir         setFormula( xSheet, "A6", "Insert values between 0.0 and 5.0 below:" );
863cdf0e10cSrcweir 
864cdf0e10cSrcweir         unoidl.com.sun.star.table.XCellRange xCellRange =
865cdf0e10cSrcweir             xSheet.getCellRangeByName( "A7:C7" );
866cdf0e10cSrcweir         unoidl.com.sun.star.beans.XPropertySet xCellPropSet =
867cdf0e10cSrcweir             (unoidl.com.sun.star.beans.XPropertySet) xCellRange;
868cdf0e10cSrcweir         // validation properties
869cdf0e10cSrcweir         unoidl.com.sun.star.beans.XPropertySet xValidPropSet =
870cdf0e10cSrcweir             (unoidl.com.sun.star.beans.XPropertySet)
871cdf0e10cSrcweir               xCellPropSet.getPropertyValue( "Validation" ).Value;
872cdf0e10cSrcweir         xValidPropSet.setPropertyValue(
873cdf0e10cSrcweir             "Type",
874cdf0e10cSrcweir             new uno.Any(
875cdf0e10cSrcweir                 typeof (unoidl.com.sun.star.sheet.ValidationType),
876cdf0e10cSrcweir                 unoidl.com.sun.star.sheet.ValidationType.DECIMAL ) );
877cdf0e10cSrcweir         xValidPropSet.setPropertyValue(
878cdf0e10cSrcweir             "ShowErrorMessage", new uno.Any( true ) );
879cdf0e10cSrcweir         xValidPropSet.setPropertyValue(
880cdf0e10cSrcweir             "ErrorMessage", new uno.Any( "This is an invalid value!" ) );
881cdf0e10cSrcweir         xValidPropSet.setPropertyValue(
882cdf0e10cSrcweir             "ErrorAlertStyle",
883cdf0e10cSrcweir             new uno.Any(
884cdf0e10cSrcweir                 typeof (unoidl.com.sun.star.sheet.ValidationAlertStyle),
885cdf0e10cSrcweir                 unoidl.com.sun.star.sheet.ValidationAlertStyle.STOP ) );
886cdf0e10cSrcweir         // condition
887cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XSheetCondition xCondition =
888cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XSheetCondition) xValidPropSet;
889cdf0e10cSrcweir         xCondition.setOperator(
890cdf0e10cSrcweir             unoidl.com.sun.star.sheet.ConditionOperator.BETWEEN );
891cdf0e10cSrcweir         xCondition.setFormula1( "0.0" );
892cdf0e10cSrcweir         xCondition.setFormula2( "5.0" );
893cdf0e10cSrcweir         // apply on cell range
894cdf0e10cSrcweir         xCellPropSet.setPropertyValue(
895cdf0e10cSrcweir             "Validation",
896cdf0e10cSrcweir             new uno.Any(
897cdf0e10cSrcweir                 typeof (unoidl.com.sun.star.beans.XPropertySet),
898cdf0e10cSrcweir                 xValidPropSet ) );
899cdf0e10cSrcweir 
900cdf0e10cSrcweir 
901cdf0e10cSrcweir         // --- Scenarios ---
902cdf0e10cSrcweir         uno.Any [][] aValues = {
903cdf0e10cSrcweir             new uno.Any [] { uno.Any.VOID, uno.Any.VOID },
904cdf0e10cSrcweir             new uno.Any [] { uno.Any.VOID, uno.Any.VOID }
905cdf0e10cSrcweir         };
906cdf0e10cSrcweir 
907cdf0e10cSrcweir         aValues[ 0 ][ 0 ] = new uno.Any( (Double) 11 );
908cdf0e10cSrcweir         aValues[ 0 ][ 1 ] = new uno.Any( (Double) 12 );
909cdf0e10cSrcweir         aValues[ 1 ][ 0 ] = new uno.Any( "Test13" );
910cdf0e10cSrcweir         aValues[ 1 ][ 1 ] = new uno.Any( "Test14" );
911cdf0e10cSrcweir         insertScenario(
912cdf0e10cSrcweir             xSheet, "B10:C11", aValues,
913cdf0e10cSrcweir             "First Scenario", "The first scenario." );
914cdf0e10cSrcweir 
915cdf0e10cSrcweir         aValues[ 0 ][ 0 ] = new uno.Any( "Test21" );
916cdf0e10cSrcweir         aValues[ 0 ][ 1 ] = new uno.Any( "Test22" );
917cdf0e10cSrcweir         aValues[ 1 ][ 0 ] = new uno.Any( (Double) 23 );
918cdf0e10cSrcweir         aValues[ 1 ][ 1 ] = new uno.Any( (Double) 24 );
919cdf0e10cSrcweir         insertScenario(
920cdf0e10cSrcweir             xSheet, "B10:C11", aValues,
921cdf0e10cSrcweir             "Second Scenario", "The visible scenario." );
922cdf0e10cSrcweir 
923cdf0e10cSrcweir         aValues[ 0 ][ 0 ] = new uno.Any( (Double) 31 );
924cdf0e10cSrcweir         aValues[ 0 ][ 1 ] = new uno.Any( (Double) 32 );
925cdf0e10cSrcweir         aValues[ 1 ][ 0 ] = new uno.Any( "Test33" );
926cdf0e10cSrcweir         aValues[ 1 ][ 1 ] = new uno.Any( "Test34" );
927cdf0e10cSrcweir         insertScenario(
928cdf0e10cSrcweir             xSheet, "B10:C11", aValues,
929cdf0e10cSrcweir             "Third Scenario", "The last scenario." );
930cdf0e10cSrcweir 
931cdf0e10cSrcweir         // show second scenario
932cdf0e10cSrcweir         showScenario( xSheet, "Second Scenario" );
933cdf0e10cSrcweir     }
934cdf0e10cSrcweir 
935cdf0e10cSrcweir     /** Inserts a scenario containing one cell range into a sheet and
936cdf0e10cSrcweir         applies the value array.
937cdf0e10cSrcweir         @param xSheet           The XSpreadsheet interface of the spreadsheet.
938cdf0e10cSrcweir         @param aRange           The range address for the scenario.
939cdf0e10cSrcweir         @param aValueArray      The array of cell contents.
940cdf0e10cSrcweir         @param aScenarioName    The name of the new scenario.
941cdf0e10cSrcweir         @param aScenarioComment The user comment for the scenario. */
insertScenario( unoidl.com.sun.star.sheet.XSpreadsheet xSheet, String aRange, uno.Any [][] aValueArray, String aScenarioName, String aScenarioComment )942cdf0e10cSrcweir     private void insertScenario(
943cdf0e10cSrcweir             unoidl.com.sun.star.sheet.XSpreadsheet xSheet,
944cdf0e10cSrcweir             String aRange,
945cdf0e10cSrcweir             uno.Any [][] aValueArray,
946cdf0e10cSrcweir             String aScenarioName,
947cdf0e10cSrcweir             String aScenarioComment )
948cdf0e10cSrcweir     {
949cdf0e10cSrcweir         // get the cell range with the given address
950cdf0e10cSrcweir         unoidl.com.sun.star.table.XCellRange xCellRange =
951cdf0e10cSrcweir             xSheet.getCellRangeByName( aRange );
952cdf0e10cSrcweir 
953cdf0e10cSrcweir         // create the range address sequence
954cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XCellRangeAddressable xAddr =
955cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XCellRangeAddressable) xCellRange;
956cdf0e10cSrcweir         unoidl.com.sun.star.table.CellRangeAddress[] aRangesSeq =
957cdf0e10cSrcweir             new unoidl.com.sun.star.table.CellRangeAddress[1];
958cdf0e10cSrcweir         aRangesSeq[0] = xAddr.getRangeAddress();
959cdf0e10cSrcweir 
960cdf0e10cSrcweir         // create the scenario
961cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XScenariosSupplier xScenSupp =
962cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XScenariosSupplier) xSheet;
963cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XScenarios xScenarios =
964cdf0e10cSrcweir             xScenSupp.getScenarios();
965cdf0e10cSrcweir         xScenarios.addNewByName( aScenarioName, aRangesSeq, aScenarioComment );
966cdf0e10cSrcweir 
967cdf0e10cSrcweir         // insert the values into the range
968cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XCellRangeData xData =
969cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XCellRangeData) xCellRange;
970cdf0e10cSrcweir         xData.setDataArray( aValueArray );
971cdf0e10cSrcweir     }
972cdf0e10cSrcweir 
973cdf0e10cSrcweir     /** Activates a scenario.
974cdf0e10cSrcweir         @param xSheet           The XSpreadsheet interface of the spreadsheet.
975cdf0e10cSrcweir         @param aScenarioName    The name of the scenario. */
showScenario( unoidl.com.sun.star.sheet.XSpreadsheet xSheet, String aScenarioName )976cdf0e10cSrcweir     private void showScenario(
977cdf0e10cSrcweir             unoidl.com.sun.star.sheet.XSpreadsheet xSheet,
978cdf0e10cSrcweir             String aScenarioName )
979cdf0e10cSrcweir     {
980cdf0e10cSrcweir         // get the scenario set
981cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XScenariosSupplier xScenSupp =
982cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XScenariosSupplier) xSheet;
983cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XScenarios xScenarios =
984cdf0e10cSrcweir             xScenSupp.getScenarios();
985cdf0e10cSrcweir 
986cdf0e10cSrcweir         // get the scenario and activate it
987cdf0e10cSrcweir         uno.Any aScenarioObj = xScenarios.getByName( aScenarioName );
988cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XScenario xScenario =
989cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XScenario) aScenarioObj.Value;
990cdf0e10cSrcweir         xScenario.apply();
991cdf0e10cSrcweir     }
992cdf0e10cSrcweir 
993cdf0e10cSrcweir // ________________________________________________________________
994cdf0e10cSrcweir 
doNamedRangesSamples()995cdf0e10cSrcweir     private void doNamedRangesSamples()
996cdf0e10cSrcweir     {
997cdf0e10cSrcweir         Console.WriteLine( "\n*** Samples for named ranges ***\n" );
998cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XSpreadsheetDocument xDocument =
999cdf0e10cSrcweir             getDocument();
1000cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XSpreadsheet xSheet =
1001cdf0e10cSrcweir             getSpreadsheet( 0 );
1002cdf0e10cSrcweir 
1003cdf0e10cSrcweir 
1004cdf0e10cSrcweir         // --- Named ranges ---
1005cdf0e10cSrcweir         prepareRange( xSheet, "G42:H45", "Named ranges" );
1006cdf0e10cSrcweir         xSheet.getCellByPosition( 6, 42 ).setValue( 1 );
1007cdf0e10cSrcweir         xSheet.getCellByPosition( 6, 43 ).setValue( 2 );
1008cdf0e10cSrcweir         xSheet.getCellByPosition( 7, 42 ).setValue( 3 );
1009cdf0e10cSrcweir         xSheet.getCellByPosition( 7, 43 ).setValue( 4 );
1010cdf0e10cSrcweir 
1011cdf0e10cSrcweir         // insert a named range
1012cdf0e10cSrcweir         unoidl.com.sun.star.beans.XPropertySet xDocProp =
1013cdf0e10cSrcweir             (unoidl.com.sun.star.beans.XPropertySet) xDocument;
1014cdf0e10cSrcweir         uno.Any aRangesObj = xDocProp.getPropertyValue( "NamedRanges" );
1015cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XNamedRanges xNamedRanges =
1016cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XNamedRanges) aRangesObj.Value;
1017cdf0e10cSrcweir         unoidl.com.sun.star.table.CellAddress aRefPos =
1018cdf0e10cSrcweir             new unoidl.com.sun.star.table.CellAddress();
1019cdf0e10cSrcweir         aRefPos.Sheet  = 0;
1020cdf0e10cSrcweir         aRefPos.Column = 6;
1021cdf0e10cSrcweir         aRefPos.Row    = 44;
1022cdf0e10cSrcweir         xNamedRanges.addNewByName( "ExampleName", "SUM(G43:G44)", aRefPos, 0 );
1023cdf0e10cSrcweir 
1024cdf0e10cSrcweir         // use the named range in formulas
1025cdf0e10cSrcweir         xSheet.getCellByPosition( 6, 44 ).setFormula( "=ExampleName" );
1026cdf0e10cSrcweir         xSheet.getCellByPosition( 7, 44 ).setFormula( "=ExampleName" );
1027cdf0e10cSrcweir 
1028cdf0e10cSrcweir 
1029cdf0e10cSrcweir         // --- Label ranges ---
1030cdf0e10cSrcweir         prepareRange( xSheet, "G47:I50", "Label ranges" );
1031cdf0e10cSrcweir         unoidl.com.sun.star.table.XCellRange xRange =
1032cdf0e10cSrcweir             xSheet.getCellRangeByPosition( 6, 47, 7, 49 );
1033cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XCellRangeData xData =
1034cdf0e10cSrcweir             ( unoidl.com.sun.star.sheet.XCellRangeData ) xRange;
1035cdf0e10cSrcweir         uno.Any [][] aValues =
1036cdf0e10cSrcweir         {
1037cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Apples" ),
1038cdf0e10cSrcweir                              new uno.Any( "Oranges" ) },
1039cdf0e10cSrcweir             new uno.Any [] { new uno.Any( (Double) 5 ),
1040cdf0e10cSrcweir                              new uno.Any( (Double) 7 ) },
1041cdf0e10cSrcweir             new uno.Any [] { new uno.Any( (Double) 6 ),
1042cdf0e10cSrcweir                              new uno.Any( (Double) 8 ) }
1043cdf0e10cSrcweir         };
1044cdf0e10cSrcweir         xData.setDataArray( aValues );
1045cdf0e10cSrcweir 
1046cdf0e10cSrcweir         // insert a column label range
1047cdf0e10cSrcweir         uno.Any aLabelsObj = xDocProp.getPropertyValue( "ColumnLabelRanges" );
1048cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XLabelRanges xLabelRanges =
1049cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XLabelRanges) aLabelsObj.Value;
1050cdf0e10cSrcweir         unoidl.com.sun.star.table.CellRangeAddress aLabelArea =
1051cdf0e10cSrcweir             new unoidl.com.sun.star.table.CellRangeAddress();
1052cdf0e10cSrcweir         aLabelArea.Sheet       = 0;
1053cdf0e10cSrcweir         aLabelArea.StartColumn = 6;
1054cdf0e10cSrcweir         aLabelArea.StartRow    = 47;
1055cdf0e10cSrcweir         aLabelArea.EndColumn   = 7;
1056cdf0e10cSrcweir         aLabelArea.EndRow      = 47;
1057cdf0e10cSrcweir         unoidl.com.sun.star.table.CellRangeAddress aDataArea =
1058cdf0e10cSrcweir             new unoidl.com.sun.star.table.CellRangeAddress();
1059cdf0e10cSrcweir         aDataArea.Sheet       = 0;
1060cdf0e10cSrcweir         aDataArea.StartColumn = 6;
1061cdf0e10cSrcweir         aDataArea.StartRow    = 48;
1062cdf0e10cSrcweir         aDataArea.EndColumn   = 7;
1063cdf0e10cSrcweir         aDataArea.EndRow      = 49;
1064cdf0e10cSrcweir         xLabelRanges.addNew( aLabelArea, aDataArea );
1065cdf0e10cSrcweir 
1066cdf0e10cSrcweir         // use the label range in formulas
1067cdf0e10cSrcweir         xSheet.getCellByPosition( 8, 48 ).setFormula( "=Apples+Oranges" );
1068cdf0e10cSrcweir         xSheet.getCellByPosition( 8, 49 ).setFormula( "=Apples+Oranges" );
1069cdf0e10cSrcweir     }
1070cdf0e10cSrcweir 
1071cdf0e10cSrcweir // ________________________________________________________________
1072cdf0e10cSrcweir 
1073cdf0e10cSrcweir     /** Helper for doDatabaseSamples: get name of first database. */
getFirstDatabaseName()1074cdf0e10cSrcweir     private String getFirstDatabaseName()
1075cdf0e10cSrcweir     {
1076cdf0e10cSrcweir         String aDatabase = null;
1077cdf0e10cSrcweir         unoidl.com.sun.star.lang.XMultiServiceFactory xServiceManager =
1078cdf0e10cSrcweir             getServiceManager();
1079cdf0e10cSrcweir         unoidl.com.sun.star.container.XNameAccess xContext =
1080cdf0e10cSrcweir             (unoidl.com.sun.star.container.XNameAccess)
1081cdf0e10cSrcweir             xServiceManager.createInstance(
1082cdf0e10cSrcweir                 "com.sun.star.sdb.DatabaseContext" );
1083cdf0e10cSrcweir         String[] aNames = xContext.getElementNames();
1084cdf0e10cSrcweir         if ( aNames.Length > 0 )
1085cdf0e10cSrcweir             aDatabase = aNames[0];
1086cdf0e10cSrcweir         return aDatabase;
1087cdf0e10cSrcweir     }
1088cdf0e10cSrcweir 
1089cdf0e10cSrcweir     /** Helper for doDatabaseSamples: get name of first table in a database. */
getFirstTableName( String aDatabase )1090cdf0e10cSrcweir     private String getFirstTableName( String aDatabase )
1091cdf0e10cSrcweir     {
1092cdf0e10cSrcweir         if ( aDatabase == null )
1093cdf0e10cSrcweir             return null;
1094cdf0e10cSrcweir 
1095cdf0e10cSrcweir         String aTable = null;
1096cdf0e10cSrcweir         unoidl.com.sun.star.lang.XMultiServiceFactory xServiceManager =
1097cdf0e10cSrcweir             getServiceManager();
1098cdf0e10cSrcweir         unoidl.com.sun.star.container.XNameAccess xContext =
1099cdf0e10cSrcweir             (unoidl.com.sun.star.container.XNameAccess)
1100cdf0e10cSrcweir             xServiceManager.createInstance(
1101cdf0e10cSrcweir                 "com.sun.star.sdb.DatabaseContext" );
1102cdf0e10cSrcweir         unoidl.com.sun.star.sdb.XCompletedConnection xSource =
1103cdf0e10cSrcweir             (unoidl.com.sun.star.sdb.XCompletedConnection)
1104cdf0e10cSrcweir               xContext.getByName( aDatabase ).Value;
1105cdf0e10cSrcweir         unoidl.com.sun.star.task.XInteractionHandler xHandler =
1106cdf0e10cSrcweir             (unoidl.com.sun.star.task.XInteractionHandler)
1107cdf0e10cSrcweir               xServiceManager.createInstance(
1108cdf0e10cSrcweir                   "com.sun.star.task.InteractionHandler" );
1109cdf0e10cSrcweir          unoidl.com.sun.star.sdbcx.XTablesSupplier xSupplier =
1110cdf0e10cSrcweir             (unoidl.com.sun.star.sdbcx.XTablesSupplier)
1111cdf0e10cSrcweir               xSource.connectWithCompletion( xHandler );
1112cdf0e10cSrcweir         unoidl.com.sun.star.container.XNameAccess xTables =
1113cdf0e10cSrcweir             xSupplier.getTables();
1114cdf0e10cSrcweir         String[] aNames = xTables.getElementNames();
1115cdf0e10cSrcweir         if ( aNames.Length > 0 )
1116cdf0e10cSrcweir             aTable = aNames[0];
1117cdf0e10cSrcweir         return aTable;
1118cdf0e10cSrcweir     }
1119cdf0e10cSrcweir 
doDatabaseSamples()1120cdf0e10cSrcweir     private void doDatabaseSamples()
1121cdf0e10cSrcweir     {
1122cdf0e10cSrcweir         Console.WriteLine( "\n*** Samples for database operations ***\n" );
1123cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XSpreadsheet xSheet = getSpreadsheet( 2 );
1124cdf0e10cSrcweir 
1125cdf0e10cSrcweir 
1126cdf0e10cSrcweir         // --- put some example data into the sheet ---
1127cdf0e10cSrcweir         unoidl.com.sun.star.table.XCellRange xRange =
1128cdf0e10cSrcweir             xSheet.getCellRangeByName( "B3:D24" );
1129cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XCellRangeData xData =
1130cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XCellRangeData) xRange;
1131cdf0e10cSrcweir         uno.Any [][] aValues =
1132cdf0e10cSrcweir         {
1133cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Name" ),
1134cdf0e10cSrcweir                              new uno.Any( "Year" ),
1135cdf0e10cSrcweir                              new uno.Any( "Sales" ) },
1136cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Alice" ),
1137cdf0e10cSrcweir                              new uno.Any( (Double) 2001 ),
1138cdf0e10cSrcweir                              new uno.Any( (Double) 4.0 ) },
1139cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Carol" ),
1140cdf0e10cSrcweir                              new uno.Any( (Double) 1997 ),
1141cdf0e10cSrcweir                              new uno.Any( (Double) 3.0 ) },
1142cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Carol" ),
1143cdf0e10cSrcweir                              new uno.Any( (Double) 1998 ),
1144cdf0e10cSrcweir                              new uno.Any( (Double) 8.0 ) },
1145cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Bob" ),
1146cdf0e10cSrcweir                              new uno.Any( (Double) 1997 ),
1147cdf0e10cSrcweir                              new uno.Any( (Double) 8.0 ) },
1148cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Alice" ),
1149cdf0e10cSrcweir                              new uno.Any( (Double) 2002 ),
1150cdf0e10cSrcweir                              new uno.Any( (Double) 9.0 ) },
1151cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Alice" ),
1152cdf0e10cSrcweir                              new uno.Any( (Double) 1999 ),
1153cdf0e10cSrcweir                              new uno.Any( (Double) 7.0 ) },
1154cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Alice" ),
1155cdf0e10cSrcweir                              new uno.Any( (Double) 1996 ),
1156cdf0e10cSrcweir                              new uno.Any( (Double) 3.0 ) },
1157cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Bob" ),
1158cdf0e10cSrcweir                              new uno.Any( (Double) 2000 ),
1159cdf0e10cSrcweir                              new uno.Any( (Double) 1.0 ) },
1160cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Carol" ),
1161cdf0e10cSrcweir                              new uno.Any( (Double) 1999 ),
1162cdf0e10cSrcweir                              new uno.Any( (Double) 5.0 ) },
1163cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Bob" ),
1164cdf0e10cSrcweir                              new uno.Any( (Double) 2002 ),
1165cdf0e10cSrcweir                              new uno.Any( (Double) 1.0 ) },
1166cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Carol" ),
1167cdf0e10cSrcweir               new uno.Any( (Double) 2001 ),
1168cdf0e10cSrcweir               new uno.Any( (Double) 5.0 ) },
1169cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Carol" ),
1170cdf0e10cSrcweir               new uno.Any( (Double) 2000 ),
1171cdf0e10cSrcweir               new uno.Any( (Double) 1.0 ) },
1172cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Carol" ),
1173cdf0e10cSrcweir               new uno.Any( (Double) 1996 ),
1174cdf0e10cSrcweir               new uno.Any( (Double) 8.0 ) },
1175cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Bob" ),
1176cdf0e10cSrcweir               new uno.Any( (Double) 1996 ),
1177cdf0e10cSrcweir               new uno.Any( (Double) 7.0 ) },
1178cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Alice" ),
1179cdf0e10cSrcweir               new uno.Any( (Double) 1997 ),
1180cdf0e10cSrcweir               new uno.Any( (Double) 3.0 ) },
1181cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Alice" ),
1182cdf0e10cSrcweir               new uno.Any( (Double) 2000 ),
1183cdf0e10cSrcweir               new uno.Any( (Double) 9.0 ) },
1184cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Bob" ),
1185cdf0e10cSrcweir               new uno.Any( (Double) 1998 ),
1186cdf0e10cSrcweir               new uno.Any( (Double) 1.0 ) },
1187cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Bob" ),
1188cdf0e10cSrcweir               new uno.Any( (Double) 1999 ),
1189cdf0e10cSrcweir               new uno.Any( (Double) 6.0 ) },
1190cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Carol" ),
1191cdf0e10cSrcweir               new uno.Any( (Double) 2002 ),
1192cdf0e10cSrcweir               new uno.Any( (Double) 8.0 ) },
1193cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Alice" ),
1194cdf0e10cSrcweir               new uno.Any( (Double) 1998 ),
1195cdf0e10cSrcweir               new uno.Any( (Double) 5.0 ) },
1196cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Bob" ),
1197cdf0e10cSrcweir               new uno.Any( (Double) 2001 ),
1198cdf0e10cSrcweir               new uno.Any( (Double) 6.0 ) }
1199cdf0e10cSrcweir         };
1200cdf0e10cSrcweir         xData.setDataArray( aValues );
1201cdf0e10cSrcweir 
1202cdf0e10cSrcweir 
1203cdf0e10cSrcweir         // --- filter for second column >= 1998 ---
1204cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XSheetFilterable xFilter =
1205cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XSheetFilterable) xRange;
1206cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XSheetFilterDescriptor xFilterDesc =
1207cdf0e10cSrcweir             xFilter.createFilterDescriptor( true );
1208cdf0e10cSrcweir         unoidl.com.sun.star.sheet.TableFilterField[] aFilterFields =
1209cdf0e10cSrcweir             new unoidl.com.sun.star.sheet.TableFilterField[1];
1210cdf0e10cSrcweir         aFilterFields[0] = new unoidl.com.sun.star.sheet.TableFilterField();
1211cdf0e10cSrcweir         aFilterFields[0].Field        = 1;
1212cdf0e10cSrcweir         aFilterFields[0].IsNumeric    = true;
1213cdf0e10cSrcweir         aFilterFields[0].Operator =
1214cdf0e10cSrcweir             unoidl.com.sun.star.sheet.FilterOperator.GREATER_EQUAL;
1215cdf0e10cSrcweir         aFilterFields[0].NumericValue = 1998;
1216cdf0e10cSrcweir         xFilterDesc.setFilterFields( aFilterFields );
1217cdf0e10cSrcweir         unoidl.com.sun.star.beans.XPropertySet xFilterProp =
1218cdf0e10cSrcweir             (unoidl.com.sun.star.beans.XPropertySet) xFilterDesc;
1219cdf0e10cSrcweir         xFilterProp.setPropertyValue(
1220cdf0e10cSrcweir             "ContainsHeader", new uno.Any( true ) );
1221cdf0e10cSrcweir         xFilter.filter( xFilterDesc );
1222cdf0e10cSrcweir 
1223cdf0e10cSrcweir 
1224cdf0e10cSrcweir         // --- do the same filter as above, using criteria from a cell range ---
1225cdf0e10cSrcweir         unoidl.com.sun.star.table.XCellRange xCritRange =
1226cdf0e10cSrcweir             xSheet.getCellRangeByName( "B27:B28" );
1227cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XCellRangeData xCritData =
1228cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XCellRangeData) xCritRange;
1229cdf0e10cSrcweir         uno.Any [][] aCritValues =
1230cdf0e10cSrcweir         {
1231cdf0e10cSrcweir             new uno.Any [] { new uno.Any( "Year" ) },
1232cdf0e10cSrcweir             new uno.Any [] { new uno.Any( ">= 1998" ) }
1233cdf0e10cSrcweir         };
1234cdf0e10cSrcweir         xCritData.setDataArray( aCritValues );
1235cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XSheetFilterableEx xCriteria =
1236cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XSheetFilterableEx) xCritRange;
1237cdf0e10cSrcweir         xFilterDesc = xCriteria.createFilterDescriptorByObject( xFilter );
1238cdf0e10cSrcweir         if ( xFilterDesc != null )
1239cdf0e10cSrcweir             xFilter.filter( xFilterDesc );
1240cdf0e10cSrcweir 
1241cdf0e10cSrcweir 
1242cdf0e10cSrcweir         // --- sort by second column, ascending ---
1243cdf0e10cSrcweir         unoidl.com.sun.star.util.SortField[] aSortFields =
1244cdf0e10cSrcweir             new unoidl.com.sun.star.util.SortField[1];
1245cdf0e10cSrcweir         aSortFields[0] = new unoidl.com.sun.star.util.SortField();
1246cdf0e10cSrcweir         aSortFields[0].Field         = 1;
1247cdf0e10cSrcweir         aSortFields[0].SortAscending = true;
1248cdf0e10cSrcweir 
1249cdf0e10cSrcweir         unoidl.com.sun.star.beans.PropertyValue[] aSortDesc =
1250cdf0e10cSrcweir             new unoidl.com.sun.star.beans.PropertyValue[2];
1251cdf0e10cSrcweir         aSortDesc[0] = new unoidl.com.sun.star.beans.PropertyValue();
1252cdf0e10cSrcweir         aSortDesc[0].Name   = "SortFields";
1253cdf0e10cSrcweir         aSortDesc[0].Value  =
1254cdf0e10cSrcweir             new uno.Any(
1255cdf0e10cSrcweir                 typeof (unoidl.com.sun.star.util.SortField []),
1256cdf0e10cSrcweir                 aSortFields );
1257cdf0e10cSrcweir         aSortDesc[1] = new unoidl.com.sun.star.beans.PropertyValue();
1258cdf0e10cSrcweir         aSortDesc[1].Name   = "ContainsHeader";
1259cdf0e10cSrcweir         aSortDesc[1].Value  = new uno.Any( true );
1260cdf0e10cSrcweir 
1261cdf0e10cSrcweir         unoidl.com.sun.star.util.XSortable xSort =
1262cdf0e10cSrcweir             (unoidl.com.sun.star.util.XSortable) xRange;
1263cdf0e10cSrcweir         xSort.sort( aSortDesc );
1264cdf0e10cSrcweir 
1265cdf0e10cSrcweir 
1266cdf0e10cSrcweir         // --- insert subtotals ---
1267cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XSubTotalCalculatable xSub =
1268cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XSubTotalCalculatable) xRange;
1269cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XSubTotalDescriptor xSubDesc =
1270cdf0e10cSrcweir             xSub.createSubTotalDescriptor( true );
1271cdf0e10cSrcweir         unoidl.com.sun.star.sheet.SubTotalColumn[] aColumns =
1272cdf0e10cSrcweir             new unoidl.com.sun.star.sheet.SubTotalColumn[1];
1273cdf0e10cSrcweir         // calculate sum of third column
1274cdf0e10cSrcweir         aColumns[0] = new unoidl.com.sun.star.sheet.SubTotalColumn();
1275cdf0e10cSrcweir         aColumns[0].Column   = 2;
1276cdf0e10cSrcweir         aColumns[0].Function = unoidl.com.sun.star.sheet.GeneralFunction.SUM;
1277cdf0e10cSrcweir         // group by first column
1278cdf0e10cSrcweir         xSubDesc.addNew( aColumns, 0 );
1279cdf0e10cSrcweir         xSub.applySubTotals( xSubDesc, true );
1280cdf0e10cSrcweir 
1281cdf0e10cSrcweir         String aDatabase = getFirstDatabaseName();
1282cdf0e10cSrcweir         String aTableName = getFirstTableName( aDatabase );
1283cdf0e10cSrcweir         if ( aDatabase != null && aTableName != null )
1284cdf0e10cSrcweir         {
1285cdf0e10cSrcweir             // --- import from database ---
1286cdf0e10cSrcweir             unoidl.com.sun.star.beans.PropertyValue[] aImportDesc =
1287cdf0e10cSrcweir                 new unoidl.com.sun.star.beans.PropertyValue[3];
1288cdf0e10cSrcweir             aImportDesc[0] = new unoidl.com.sun.star.beans.PropertyValue();
1289cdf0e10cSrcweir             aImportDesc[0].Name     = "DatabaseName";
1290cdf0e10cSrcweir             aImportDesc[0].Value    = new uno.Any( aDatabase );
1291cdf0e10cSrcweir             aImportDesc[1] = new unoidl.com.sun.star.beans.PropertyValue();
1292cdf0e10cSrcweir             aImportDesc[1].Name     = "SourceType";
1293cdf0e10cSrcweir             aImportDesc[1].Value    =
1294cdf0e10cSrcweir                 new uno.Any(
1295cdf0e10cSrcweir                     typeof (unoidl.com.sun.star.sheet.DataImportMode),
1296cdf0e10cSrcweir                     unoidl.com.sun.star.sheet.DataImportMode.TABLE );
1297cdf0e10cSrcweir             aImportDesc[2] = new unoidl.com.sun.star.beans.PropertyValue();
1298cdf0e10cSrcweir             aImportDesc[2].Name     = "SourceObject";
1299cdf0e10cSrcweir             aImportDesc[2].Value    = new uno.Any( aTableName );
1300cdf0e10cSrcweir 
1301cdf0e10cSrcweir             unoidl.com.sun.star.table.XCellRange xImportRange =
1302cdf0e10cSrcweir                 xSheet.getCellRangeByName( "B35:B35" );
1303cdf0e10cSrcweir             unoidl.com.sun.star.util.XImportable xImport =
1304cdf0e10cSrcweir                 (unoidl.com.sun.star.util.XImportable) xImportRange;
1305cdf0e10cSrcweir             xImport.doImport( aImportDesc );
1306cdf0e10cSrcweir 
1307cdf0e10cSrcweir 
1308cdf0e10cSrcweir             // --- use the temporary database range to find the
1309cdf0e10cSrcweir             // imported data's size ---
1310cdf0e10cSrcweir             unoidl.com.sun.star.beans.XPropertySet xDocProp =
1311cdf0e10cSrcweir                 (unoidl.com.sun.star.beans.XPropertySet) getDocument();
1312cdf0e10cSrcweir             uno.Any aRangesObj = xDocProp.getPropertyValue( "DatabaseRanges" );
1313cdf0e10cSrcweir             unoidl.com.sun.star.container.XNameAccess xRanges =
1314cdf0e10cSrcweir                 (unoidl.com.sun.star.container.XNameAccess) aRangesObj.Value;
1315cdf0e10cSrcweir             String[] aNames = xRanges.getElementNames();
1316cdf0e10cSrcweir             for ( int i=0; i<aNames.Length; i++ )
1317cdf0e10cSrcweir             {
1318cdf0e10cSrcweir                 uno.Any aRangeObj = xRanges.getByName( aNames[i] );
1319cdf0e10cSrcweir                 unoidl.com.sun.star.beans.XPropertySet xRangeProp =
1320cdf0e10cSrcweir                     (unoidl.com.sun.star.beans.XPropertySet) aRangeObj.Value;
1321cdf0e10cSrcweir                 bool bUser = (Boolean)
1322cdf0e10cSrcweir                     xRangeProp.getPropertyValue( "IsUserDefined" ).Value;
1323cdf0e10cSrcweir                 if ( !bUser )
1324cdf0e10cSrcweir                 {
1325cdf0e10cSrcweir                     // this is the temporary database range -
1326cdf0e10cSrcweir                     // get the cell range and format it
1327cdf0e10cSrcweir                     unoidl.com.sun.star.sheet.XCellRangeReferrer xRef =
1328cdf0e10cSrcweir                         (unoidl.com.sun.star.sheet.XCellRangeReferrer)
1329cdf0e10cSrcweir                           aRangeObj.Value;
1330cdf0e10cSrcweir                     unoidl.com.sun.star.table.XCellRange xResultRange =
1331cdf0e10cSrcweir                         xRef.getReferredCells();
1332cdf0e10cSrcweir                     unoidl.com.sun.star.beans.XPropertySet xResultProp =
1333cdf0e10cSrcweir                         (unoidl.com.sun.star.beans.XPropertySet) xResultRange;
1334cdf0e10cSrcweir                     xResultProp.setPropertyValue(
1335cdf0e10cSrcweir                         "IsCellBackgroundTransparent", new uno.Any( false ) );
1336cdf0e10cSrcweir                     xResultProp.setPropertyValue(
1337cdf0e10cSrcweir                         "CellBackColor", new uno.Any( (Int32) 0xFFFFCC ) );
1338cdf0e10cSrcweir                 }
1339cdf0e10cSrcweir             }
1340cdf0e10cSrcweir         }
1341cdf0e10cSrcweir         else
1342cdf0e10cSrcweir             Console.WriteLine("can't get database");
1343cdf0e10cSrcweir     }
1344cdf0e10cSrcweir 
1345cdf0e10cSrcweir // ________________________________________________________________
1346cdf0e10cSrcweir 
doDataPilotSamples()1347cdf0e10cSrcweir     private void doDataPilotSamples()
1348cdf0e10cSrcweir     {
1349cdf0e10cSrcweir         Console.WriteLine( "\n*** Samples for Data Pilot ***\n" );
1350cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XSpreadsheet xSheet = getSpreadsheet( 0 );
1351cdf0e10cSrcweir 
1352cdf0e10cSrcweir 
1353cdf0e10cSrcweir         // --- Create a new DataPilot table ---
1354cdf0e10cSrcweir         prepareRange( xSheet, "A38:C38", "Data Pilot" );
1355cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XDataPilotTablesSupplier xDPSupp =
1356cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XDataPilotTablesSupplier) xSheet;
1357cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XDataPilotTables xDPTables =
1358cdf0e10cSrcweir             xDPSupp.getDataPilotTables();
1359cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XDataPilotDescriptor xDPDesc =
1360cdf0e10cSrcweir             xDPTables.createDataPilotDescriptor();
1361cdf0e10cSrcweir         // set source range (use data range from CellRange test)
1362cdf0e10cSrcweir         unoidl.com.sun.star.table.CellRangeAddress aSourceAddress =
1363cdf0e10cSrcweir             createCellRangeAddress( xSheet, "A10:C30" );
1364cdf0e10cSrcweir         xDPDesc.setSourceRange( aSourceAddress );
1365cdf0e10cSrcweir         // settings for fields
1366cdf0e10cSrcweir         unoidl.com.sun.star.container.XIndexAccess xFields =
1367cdf0e10cSrcweir             xDPDesc.getDataPilotFields();
1368cdf0e10cSrcweir         uno.Any aFieldObj;
1369cdf0e10cSrcweir         unoidl.com.sun.star.beans.XPropertySet xFieldProp;
1370cdf0e10cSrcweir         // use first column as column field
1371cdf0e10cSrcweir         aFieldObj = xFields.getByIndex(0);
1372cdf0e10cSrcweir         xFieldProp = (unoidl.com.sun.star.beans.XPropertySet) aFieldObj.Value;
1373cdf0e10cSrcweir         xFieldProp.setPropertyValue(
1374cdf0e10cSrcweir             "Orientation",
1375cdf0e10cSrcweir             new uno.Any(
1376cdf0e10cSrcweir                 typeof (unoidl.com.sun.star.sheet.DataPilotFieldOrientation),
1377cdf0e10cSrcweir                 unoidl.com.sun.star.sheet.DataPilotFieldOrientation.COLUMN ) );
1378cdf0e10cSrcweir         // use second column as row field
1379cdf0e10cSrcweir         aFieldObj = xFields.getByIndex(1);
1380cdf0e10cSrcweir         xFieldProp = (unoidl.com.sun.star.beans.XPropertySet) aFieldObj.Value;
1381cdf0e10cSrcweir         xFieldProp.setPropertyValue(
1382cdf0e10cSrcweir             "Orientation",
1383cdf0e10cSrcweir             new uno.Any(
1384cdf0e10cSrcweir                 typeof (unoidl.com.sun.star.sheet.DataPilotFieldOrientation),
1385cdf0e10cSrcweir                 unoidl.com.sun.star.sheet.DataPilotFieldOrientation.ROW ) );
1386cdf0e10cSrcweir         // use third column as data field, calculating the sum
1387cdf0e10cSrcweir         aFieldObj = xFields.getByIndex(2);
1388cdf0e10cSrcweir         xFieldProp = (unoidl.com.sun.star.beans.XPropertySet) aFieldObj.Value;
1389cdf0e10cSrcweir         xFieldProp.setPropertyValue(
1390cdf0e10cSrcweir             "Orientation",
1391cdf0e10cSrcweir             new uno.Any(
1392cdf0e10cSrcweir                 typeof (unoidl.com.sun.star.sheet.DataPilotFieldOrientation),
1393cdf0e10cSrcweir                 unoidl.com.sun.star.sheet.DataPilotFieldOrientation.DATA ) );
1394cdf0e10cSrcweir         xFieldProp.setPropertyValue(
1395cdf0e10cSrcweir             "Function",
1396cdf0e10cSrcweir             new uno.Any(
1397cdf0e10cSrcweir                 typeof (unoidl.com.sun.star.sheet.GeneralFunction),
1398cdf0e10cSrcweir                 unoidl.com.sun.star.sheet.GeneralFunction.SUM ) );
1399cdf0e10cSrcweir         // select output position
1400cdf0e10cSrcweir         unoidl.com.sun.star.table.CellAddress aDestAddress =
1401cdf0e10cSrcweir             createCellAddress( xSheet, "A40" );
1402cdf0e10cSrcweir         xDPTables.insertNewByName( "DataPilotExample", aDestAddress, xDPDesc );
1403cdf0e10cSrcweir 
1404cdf0e10cSrcweir 
1405cdf0e10cSrcweir         // --- Modify the DataPilot table ---
1406cdf0e10cSrcweir         uno.Any aDPTableObj = xDPTables.getByName( "DataPilotExample" );
1407cdf0e10cSrcweir         xDPDesc =
1408cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XDataPilotDescriptor) aDPTableObj.Value;
1409cdf0e10cSrcweir         xFields = xDPDesc.getDataPilotFields();
1410cdf0e10cSrcweir         // add a second data field from the third column,
1411cdf0e10cSrcweir         // calculating the average
1412cdf0e10cSrcweir         aFieldObj = xFields.getByIndex(2);
1413cdf0e10cSrcweir         xFieldProp = (unoidl.com.sun.star.beans.XPropertySet) aFieldObj.Value;
1414cdf0e10cSrcweir         xFieldProp.setPropertyValue(
1415cdf0e10cSrcweir             "Orientation",
1416cdf0e10cSrcweir             new uno.Any(
1417cdf0e10cSrcweir                 typeof (unoidl.com.sun.star.sheet.DataPilotFieldOrientation),
1418cdf0e10cSrcweir                 unoidl.com.sun.star.sheet.DataPilotFieldOrientation.DATA ) );
1419cdf0e10cSrcweir         xFieldProp.setPropertyValue(
1420cdf0e10cSrcweir             "Function",
1421cdf0e10cSrcweir             new uno.Any(
1422cdf0e10cSrcweir                 typeof (unoidl.com.sun.star.sheet.GeneralFunction),
1423cdf0e10cSrcweir                 unoidl.com.sun.star.sheet.GeneralFunction.AVERAGE ) );
1424cdf0e10cSrcweir     }
1425cdf0e10cSrcweir 
1426cdf0e10cSrcweir // ________________________________________________________________
1427cdf0e10cSrcweir 
doFunctionAccessSamples()1428cdf0e10cSrcweir     private void doFunctionAccessSamples()
1429cdf0e10cSrcweir     {
1430cdf0e10cSrcweir         Console.WriteLine( "\n*** Samples for function handling ***\n" );
1431cdf0e10cSrcweir         unoidl.com.sun.star.lang.XMultiServiceFactory xServiceManager =
1432cdf0e10cSrcweir             getServiceManager();
1433cdf0e10cSrcweir 
1434cdf0e10cSrcweir 
1435cdf0e10cSrcweir         // --- Calculate a function ---
1436cdf0e10cSrcweir         Object aFuncInst = xServiceManager.createInstance(
1437cdf0e10cSrcweir             "com.sun.star.sheet.FunctionAccess" );
1438cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XFunctionAccess xFuncAcc =
1439cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XFunctionAccess) aFuncInst;
1440cdf0e10cSrcweir         // put the data in a two-dimensional array
1441cdf0e10cSrcweir         Double [][] aData = { new Double [] { 1.0, 2.0, 3.0 } };
1442cdf0e10cSrcweir         // construct the array of function arguments
1443cdf0e10cSrcweir         uno.Any [] aArgs = new uno.Any [2];
1444cdf0e10cSrcweir         aArgs[0] = new uno.Any( typeof (Double [][]), aData );
1445cdf0e10cSrcweir         aArgs[1] = new uno.Any( (Double) 2.0 );
1446cdf0e10cSrcweir         uno.Any aResult = xFuncAcc.callFunction( "ZTEST", aArgs );
1447cdf0e10cSrcweir         Console.WriteLine(
1448cdf0e10cSrcweir             "ZTEST result for data {1,2,3} and value 2 is " + aResult.Value );
1449cdf0e10cSrcweir 
1450cdf0e10cSrcweir 
1451cdf0e10cSrcweir         // --- Get the list of recently used functions ---
1452cdf0e10cSrcweir         Object aRecInst = xServiceManager.createInstance(
1453cdf0e10cSrcweir             "com.sun.star.sheet.RecentFunctions" );
1454cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XRecentFunctions xRecFunc =
1455cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XRecentFunctions) aRecInst;
1456cdf0e10cSrcweir         int[] nRecentIds = xRecFunc.getRecentFunctionIds();
1457cdf0e10cSrcweir 
1458cdf0e10cSrcweir 
1459cdf0e10cSrcweir         // --- Get the names for these functions ---
1460cdf0e10cSrcweir         Object aDescInst = xServiceManager.createInstance(
1461cdf0e10cSrcweir             "com.sun.star.sheet.FunctionDescriptions" );
1462cdf0e10cSrcweir         unoidl.com.sun.star.sheet.XFunctionDescriptions xFuncDesc =
1463cdf0e10cSrcweir             (unoidl.com.sun.star.sheet.XFunctionDescriptions) aDescInst;
1464cdf0e10cSrcweir         Console.Write("Recently used functions: ");
1465cdf0e10cSrcweir         for (int nFunction=0; nFunction<nRecentIds.Length; nFunction++)
1466cdf0e10cSrcweir         {
1467cdf0e10cSrcweir             unoidl.com.sun.star.beans.PropertyValue[] aProperties =
1468cdf0e10cSrcweir                 xFuncDesc.getById( nRecentIds[nFunction] );
1469cdf0e10cSrcweir             for (int nProp=0; nProp<aProperties.Length; nProp++)
1470cdf0e10cSrcweir                 if ( aProperties[nProp].Name.Equals( "Name" ) )
1471cdf0e10cSrcweir                     Console.Write( aProperties[nProp].Value + " " );
1472cdf0e10cSrcweir         }
1473cdf0e10cSrcweir         Console.WriteLine();
1474cdf0e10cSrcweir     }
1475cdf0e10cSrcweir 
1476cdf0e10cSrcweir // ________________________________________________________________
1477cdf0e10cSrcweir 
doApplicationSettingsSamples()1478cdf0e10cSrcweir     private void doApplicationSettingsSamples()
1479cdf0e10cSrcweir     {
1480cdf0e10cSrcweir         Console.WriteLine( "\n*** Samples for application settings ***\n" );
1481cdf0e10cSrcweir         unoidl.com.sun.star.lang.XMultiServiceFactory xServiceManager =
1482cdf0e10cSrcweir             getServiceManager();
1483cdf0e10cSrcweir 
1484cdf0e10cSrcweir 
1485cdf0e10cSrcweir         // --- Get the user defined sort lists ---
1486cdf0e10cSrcweir         Object aSettings = xServiceManager.createInstance(
1487cdf0e10cSrcweir             "com.sun.star.sheet.GlobalSheetSettings" );
1488cdf0e10cSrcweir         unoidl.com.sun.star.beans.XPropertySet xPropSet =
1489cdf0e10cSrcweir             (unoidl.com.sun.star.beans.XPropertySet) aSettings;
1490cdf0e10cSrcweir         String[] aEntries = (String [])
1491cdf0e10cSrcweir             xPropSet.getPropertyValue( "UserLists" ).Value;
1492cdf0e10cSrcweir         Console.WriteLine("User defined sort lists:");
1493cdf0e10cSrcweir         for ( int i=0; i<aEntries.Length; i++ )
1494cdf0e10cSrcweir             Console.WriteLine( aEntries[i] );
1495cdf0e10cSrcweir     }
1496cdf0e10cSrcweir 
1497cdf0e10cSrcweir // ________________________________________________________________
1498cdf0e10cSrcweir 
1499cdf0e10cSrcweir }
1500