1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *************************************************************************/
35 import com
.sun
.star
.uno
.UnoRuntime
;
36 import com
.sun
.star
.uno
.RuntimeException
;
37 import com
.sun
.star
.uno
.AnyConverter
;
39 // __________ implementation ____________________________________
41 /** Create and modify a spreadsheet document.
43 public class SpreadsheetSample
extends SpreadsheetDocHelper
48 public static void main( String args
[] )
52 SpreadsheetSample aSample
= new SpreadsheetSample( args
);
53 aSample
.doSampleFunction();
57 System
.out
.println( "Error: Sample caught exception!\nException Message = "
62 System
.out
.println( "\nSamples done." );
68 public SpreadsheetSample( String
[] args
)
75 /** This sample function performs all changes on the document. */
76 public void doSampleFunction()
84 System
.out
.println( "\nError: Cell sample caught exception!\nException Message = "
95 System
.out
.println( "\nError: Cell range sample caught exception!\nException Message = "
102 doCellRangesSamples();
106 System
.out
.println( "\nError: Cell range container sample caught exception!\nException Message = "
108 ex
.printStackTrace();
113 doCellCursorSamples();
117 System
.out
.println( "\nError: Cell cursor sample caught exception!\nException Message = "
119 ex
.printStackTrace();
124 doFormattingSamples();
128 System
.out
.println( "\nError: Formatting sample caught exception!\nException Message = "
130 ex
.printStackTrace();
139 System
.out
.println( "\nError: Document sample caught exception!\nException Message = "
141 ex
.printStackTrace();
148 catch( Exception ex
)
150 System
.out
.println( "\nError: Database sample caught exception!\nException Message = "
152 ex
.printStackTrace();
157 doDataPilotSamples();
161 System
.out
.println( "\nError: Dota pilot sample caught exception!\nException Message = "
163 ex
.printStackTrace();
168 doNamedRangesSamples();
170 catch( Exception ex
)
172 System
.out
.println( "\nError: Named ranges sample caught exception!\nException Message = "
174 ex
.printStackTrace();
179 doFunctionAccessSamples();
183 System
.out
.println( "\nError: Function access sample caught exception!\nException Message = "
185 ex
.printStackTrace();
190 doApplicationSettingsSamples();
194 System
.out
.println( "\nError: Application settings sample caught exception!\nException Message = "
196 ex
.printStackTrace();
202 /** All samples regarding the service com.sun.star.sheet.SheetCell. */
203 private void doCellSamples() throws RuntimeException
, Exception
205 System
.out
.println( "\n*** Samples for service sheet.SheetCell ***\n" );
206 com
.sun
.star
.sheet
.XSpreadsheet xSheet
= getSpreadsheet( 0 );
207 com
.sun
.star
.table
.XCell xCell
= null;
208 com
.sun
.star
.beans
.XPropertySet xPropSet
= null;
210 prepareRange( xSheet
, "A1:C7", "Cells and Cell Ranges" );
212 // --- Get cell B3 by position - (column, row) ---
213 xCell
= xSheet
.getCellByPosition( 1, 2 );
216 // --- Insert two text paragraphs into the cell. ---
217 com
.sun
.star
.text
.XText xText
= UnoRuntime
.queryInterface( com
.sun
.star
.text
.XText
.class, xCell
);
218 com
.sun
.star
.text
.XTextCursor xTextCursor
= xText
.createTextCursor();
220 xText
.insertString( xTextCursor
, "Text in first line.", false );
221 xText
.insertControlCharacter( xTextCursor
,
222 com
.sun
.star
.text
.ControlCharacter
.PARAGRAPH_BREAK
, false );
223 xText
.insertString( xTextCursor
, "And a ", false );
225 // create a hyperlink
226 com
.sun
.star
.lang
.XMultiServiceFactory xServiceMan
= UnoRuntime
.queryInterface( com
.sun
.star
.lang
.XMultiServiceFactory
.class, getDocument() );
227 Object aHyperlinkObj
= xServiceMan
.createInstance( "com.sun.star.text.TextField.URL" );
228 xPropSet
= UnoRuntime
.queryInterface( com
.sun
.star
.beans
.XPropertySet
.class, aHyperlinkObj
);
229 xPropSet
.setPropertyValue( "URL", "http://www.example.org" );
230 xPropSet
.setPropertyValue( "Representation", "hyperlink" );
232 com
.sun
.star
.text
.XTextContent xContent
= UnoRuntime
.queryInterface( com
.sun
.star
.text
.XTextContent
.class, aHyperlinkObj
);
233 xText
.insertTextContent( xTextCursor
, xContent
, false );
236 // --- Query the separate paragraphs. ---
237 com
.sun
.star
.container
.XEnumerationAccess xParaEA
=
238 UnoRuntime
.queryInterface(
239 com
.sun
.star
.container
.XEnumerationAccess
.class, xCell
);
240 com
.sun
.star
.container
.XEnumeration xParaEnum
= xParaEA
.createEnumeration();
241 // Go through the paragraphs
242 while( xParaEnum
.hasMoreElements() )
244 Object aPortionObj
= xParaEnum
.nextElement();
245 com
.sun
.star
.container
.XEnumerationAccess xPortionEA
=
246 UnoRuntime
.queryInterface(
247 com
.sun
.star
.container
.XEnumerationAccess
.class, aPortionObj
);
248 com
.sun
.star
.container
.XEnumeration xPortionEnum
= xPortionEA
.createEnumeration();
250 // Go through all text portions of a paragraph and construct string.
251 while( xPortionEnum
.hasMoreElements() )
253 com
.sun
.star
.text
.XTextRange xRange
= UnoRuntime
.queryInterface(com
.sun
.star
.text
.XTextRange
.class,
254 xPortionEnum
.nextElement());
255 aText
+= xRange
.getString();
257 System
.out
.println( "Paragraph text: " + aText
);
261 // --- Change cell properties. ---
262 xPropSet
= UnoRuntime
.queryInterface( com
.sun
.star
.beans
.XPropertySet
.class, xCell
);
263 // from styles.CharacterProperties
264 xPropSet
.setPropertyValue( "CharColor", Integer
.valueOf( 0x003399 ) );
265 xPropSet
.setPropertyValue( "CharHeight", new Float( 20.0 ) );
266 // from styles.ParagraphProperties
267 xPropSet
.setPropertyValue( "ParaLeftMargin", Integer
.valueOf( 500 ) );
268 // from table.CellProperties
269 xPropSet
.setPropertyValue( "IsCellBackgroundTransparent", Boolean
.FALSE
);
270 xPropSet
.setPropertyValue( "CellBackColor", Integer
.valueOf( 0x99CCFF ) );
273 // --- Get cell address. ---
274 com
.sun
.star
.sheet
.XCellAddressable xCellAddr
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XCellAddressable
.class, xCell
);
275 com
.sun
.star
.table
.CellAddress aAddress
= xCellAddr
.getCellAddress();
276 aText
= "Address of this cell: Column=" + aAddress
.Column
;
277 aText
+= "; Row=" + aAddress
.Row
;
278 aText
+= "; Sheet=" + aAddress
.Sheet
;
279 System
.out
.println( aText
);
282 // --- Insert an annotation ---
283 com
.sun
.star
.sheet
.XSheetAnnotationsSupplier xAnnotationsSupp
=
284 UnoRuntime
.queryInterface(
285 com
.sun
.star
.sheet
.XSheetAnnotationsSupplier
.class, xSheet
);
286 com
.sun
.star
.sheet
.XSheetAnnotations xAnnotations
= xAnnotationsSupp
.getAnnotations();
287 xAnnotations
.insertNew( aAddress
, "This is an annotation" );
289 com
.sun
.star
.sheet
.XSheetAnnotationAnchor xAnnotAnchor
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XSheetAnnotationAnchor
.class, xCell
);
290 com
.sun
.star
.sheet
.XSheetAnnotation xAnnotation
= xAnnotAnchor
.getAnnotation();
291 xAnnotation
.setIsVisible( true );
296 /** All samples regarding the service com.sun.star.sheet.SheetCellRange. */
297 private void doCellRangeSamples() throws RuntimeException
, Exception
299 System
.out
.println( "\n*** Samples for service sheet.SheetCellRange ***\n" );
300 com
.sun
.star
.sheet
.XSpreadsheet xSheet
= getSpreadsheet( 0 );
301 com
.sun
.star
.table
.XCellRange xCellRange
= null;
302 com
.sun
.star
.beans
.XPropertySet xPropSet
= null;
303 com
.sun
.star
.table
.CellRangeAddress aRangeAddress
= null;
306 setFormula( xSheet
, "B5", "First cell" );
307 setFormula( xSheet
, "B6", "Second cell" );
308 // Get cell range B5:B6 by position - (column, row, column, row)
309 xCellRange
= xSheet
.getCellRangeByPosition( 1, 4, 1, 5 );
312 // --- Change cell range properties. ---
313 xPropSet
= UnoRuntime
.queryInterface( com
.sun
.star
.beans
.XPropertySet
.class, xCellRange
);
314 // from com.sun.star.styles.CharacterProperties
315 xPropSet
.setPropertyValue( "CharColor", Integer
.valueOf( 0x003399 ) );
316 xPropSet
.setPropertyValue( "CharHeight", new Float( 20.0 ) );
317 // from com.sun.star.styles.ParagraphProperties
318 xPropSet
.setPropertyValue( "ParaLeftMargin", Integer
.valueOf( 500 ) );
319 // from com.sun.star.table.CellProperties
320 xPropSet
.setPropertyValue( "IsCellBackgroundTransparent", Boolean
.FALSE
);
321 xPropSet
.setPropertyValue( "CellBackColor", Integer
.valueOf( 0x99CCFF ) );
324 // --- Replace text in all cells. ---
325 com
.sun
.star
.util
.XReplaceable xReplace
= UnoRuntime
.queryInterface( com
.sun
.star
.util
.XReplaceable
.class, xCellRange
);
326 com
.sun
.star
.util
.XReplaceDescriptor xReplaceDesc
= xReplace
.createReplaceDescriptor();
327 xReplaceDesc
.setSearchString( "cell" );
328 xReplaceDesc
.setReplaceString( "text" );
329 // property SearchWords searches for whole cells!
330 xReplaceDesc
.setPropertyValue( "SearchWords", Boolean
.FALSE
);
331 int nCount
= xReplace
.replaceAll( xReplaceDesc
);
332 System
.out
.println( "Search text replaced " + nCount
+ " times." );
335 // --- Merge cells. ---
336 xCellRange
= xSheet
.getCellRangeByName( "F3:G6" );
337 prepareRange( xSheet
, "E1:H7", "XMergeable" );
338 com
.sun
.star
.util
.XMergeable xMerge
= UnoRuntime
.queryInterface( com
.sun
.star
.util
.XMergeable
.class, xCellRange
);
339 xMerge
.merge( true );
342 // --- Change indentation. ---
343 /* does not work (bug in XIndent implementation)
344 prepareRange( xSheet, "I20:I23", "XIndent" );
345 setValue( xSheet, "I21", 1 );
346 setValue( xSheet, "I22", 1 );
347 setValue( xSheet, "I23", 1 );
349 xCellRange = xSheet.getCellRangeByName( "I21:I22" );
350 com.sun.star.util.XIndent xIndent = (com.sun.star.util.XIndent)
351 UnoRuntime.queryInterface( com.sun.star.util.XIndent.class, xCellRange );
352 xIndent.incrementIndent();
354 xCellRange = xSheet.getCellRangeByName( "I22:I23" );
355 xIndent = (com.sun.star.util.XIndent)
356 UnoRuntime.queryInterface( com.sun.star.util.XIndent.class, xCellRange );
357 xIndent.incrementIndent();
361 // --- Column properties. ---
362 xCellRange
= xSheet
.getCellRangeByName( "B1" );
363 com
.sun
.star
.table
.XColumnRowRange xColRowRange
= UnoRuntime
.queryInterface( com
.sun
.star
.table
.XColumnRowRange
.class, xCellRange
);
364 com
.sun
.star
.table
.XTableColumns xColumns
= xColRowRange
.getColumns();
366 Object aColumnObj
= xColumns
.getByIndex( 0 );
367 xPropSet
= UnoRuntime
.queryInterface(
368 com
.sun
.star
.beans
.XPropertySet
.class, aColumnObj
);
369 xPropSet
.setPropertyValue( "Width", Integer
.valueOf( 6000 ) );
371 com
.sun
.star
.container
.XNamed xNamed
= UnoRuntime
.queryInterface( com
.sun
.star
.container
.XNamed
.class, aColumnObj
);
372 System
.out
.println( "The name of the wide column is " + xNamed
.getName() + "." );
375 // --- Cell range data ---
376 prepareRange( xSheet
, "A9:C30", "XCellRangeData" );
378 xCellRange
= xSheet
.getCellRangeByName( "A10:C30" );
379 com
.sun
.star
.sheet
.XCellRangeData xData
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XCellRangeData
.class, xCellRange
);
382 { "Name", "Fruit", "Quantity" },
383 { "Alice", "Apples", new Double( 3.0 ) },
384 { "Alice", "Oranges", new Double( 7.0 ) },
385 { "Bob", "Apples", new Double( 3.0 ) },
386 { "Alice", "Apples", new Double( 9.0 ) },
387 { "Bob", "Apples", new Double( 5.0 ) },
388 { "Bob", "Oranges", new Double( 6.0 ) },
389 { "Alice", "Oranges", new Double( 3.0 ) },
390 { "Alice", "Apples", new Double( 8.0 ) },
391 { "Alice", "Oranges", new Double( 1.0 ) },
392 { "Bob", "Oranges", new Double( 2.0 ) },
393 { "Bob", "Oranges", new Double( 7.0 ) },
394 { "Bob", "Apples", new Double( 1.0 ) },
395 { "Alice", "Apples", new Double( 8.0 ) },
396 { "Alice", "Oranges", new Double( 8.0 ) },
397 { "Alice", "Apples", new Double( 7.0 ) },
398 { "Bob", "Apples", new Double( 1.0 ) },
399 { "Bob", "Oranges", new Double( 9.0 ) },
400 { "Bob", "Oranges", new Double( 3.0 ) },
401 { "Alice", "Oranges", new Double( 4.0 ) },
402 { "Alice", "Apples", new Double( 9.0 ) }
404 xData
.setDataArray( aValues
);
407 // --- Get cell range address. ---
408 com
.sun
.star
.sheet
.XCellRangeAddressable xRangeAddr
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XCellRangeAddressable
.class, xCellRange
);
409 aRangeAddress
= xRangeAddr
.getRangeAddress();
410 System
.out
.println( "Address of this range: Sheet=" + aRangeAddress
.Sheet
);
411 System
.out
.println( "Start column=" + aRangeAddress
.StartColumn
+ "; Start row=" + aRangeAddress
.StartRow
);
412 System
.out
.println( "End column =" + aRangeAddress
.EndColumn
+ "; End row =" + aRangeAddress
.EndRow
);
415 // --- Sheet operation. ---
416 // uses the range filled with XCellRangeData
417 com
.sun
.star
.sheet
.XSheetOperation xSheetOp
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XSheetOperation
.class, xData
);
418 double fResult
= xSheetOp
.computeFunction( com
.sun
.star
.sheet
.GeneralFunction
.AVERAGE
);
419 System
.out
.println( "Average value of the data table A10:C30: " + fResult
);
422 // --- Fill series ---
423 // Prepare the example
424 setValue( xSheet
, "E10", 1 );
425 setValue( xSheet
, "E11", 4 );
426 setDate( xSheet
, "E12", 30, 1, 2002 );
427 setFormula( xSheet
, "I13", "Text 10" );
428 setFormula( xSheet
, "E14", "Jan" );
429 setValue( xSheet
, "K14", 10 );
430 setValue( xSheet
, "E16", 1 );
431 setValue( xSheet
, "F16", 2 );
432 setDate( xSheet
, "E17", 28, 2, 2002 );
433 setDate( xSheet
, "F17", 28, 1, 2002 );
434 setValue( xSheet
, "E18", 6 );
435 setValue( xSheet
, "F18", 4 );
437 com
.sun
.star
.sheet
.XCellSeries xSeries
= null;
438 // Fill 2 rows linear with end value -> 2nd series is not filled completely
439 xSeries
= getCellSeries( xSheet
, "E10:I11" );
440 xSeries
.fillSeries( com
.sun
.star
.sheet
.FillDirection
.TO_RIGHT
, com
.sun
.star
.sheet
.FillMode
.LINEAR
,
441 com
.sun
.star
.sheet
.FillDateMode
.FILL_DATE_DAY
, 2, 9 );
442 // Add months to a date
443 xSeries
= getCellSeries( xSheet
, "E12:I12" );
444 xSeries
.fillSeries( com
.sun
.star
.sheet
.FillDirection
.TO_RIGHT
, com
.sun
.star
.sheet
.FillMode
.DATE
,
445 com
.sun
.star
.sheet
.FillDateMode
.FILL_DATE_MONTH
, 1, 0x7FFFFFFF );
446 // Fill right to left with a text containing a value
447 xSeries
= getCellSeries( xSheet
, "E13:I13" );
448 xSeries
.fillSeries( com
.sun
.star
.sheet
.FillDirection
.TO_LEFT
, com
.sun
.star
.sheet
.FillMode
.LINEAR
,
449 com
.sun
.star
.sheet
.FillDateMode
.FILL_DATE_DAY
, 10, 0x7FFFFFFF );
450 // Fill with an user defined list
451 xSeries
= getCellSeries( xSheet
, "E14:I14" );
452 xSeries
.fillSeries( com
.sun
.star
.sheet
.FillDirection
.TO_RIGHT
, com
.sun
.star
.sheet
.FillMode
.AUTO
,
453 com
.sun
.star
.sheet
.FillDateMode
.FILL_DATE_DAY
, 1, 0x7FFFFFFF );
454 // Fill bottom to top with a geometric series
455 xSeries
= getCellSeries( xSheet
, "K10:K14" );
456 xSeries
.fillSeries( com
.sun
.star
.sheet
.FillDirection
.TO_TOP
, com
.sun
.star
.sheet
.FillMode
.GROWTH
,
457 com
.sun
.star
.sheet
.FillDateMode
.FILL_DATE_DAY
, 2, 0x7FFFFFFF );
459 xSeries
= getCellSeries( xSheet
, "E16:K18" );
460 xSeries
.fillAuto( com
.sun
.star
.sheet
.FillDirection
.TO_RIGHT
, 2 );
461 // Fill series copies cell formats -> draw border here
462 prepareRange( xSheet
, "E9:K18", "XCellSeries" );
465 // --- Array formulas ---
466 xCellRange
= xSheet
.getCellRangeByName( "E21:G23" );
467 prepareRange( xSheet
, "E20:G23", "XArrayFormulaRange" );
468 com
.sun
.star
.sheet
.XArrayFormulaRange xArrayFormula
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XArrayFormulaRange
.class, xCellRange
);
469 // Insert a 3x3 unit matrix.
470 xArrayFormula
.setArrayFormula( "=A10:C12" );
471 System
.out
.println( "Array formula is: " + xArrayFormula
.getArrayFormula() );
474 // --- Multiple operations ---
475 setFormula( xSheet
, "E26", "=E27^F26" );
476 setValue( xSheet
, "E27", 1 );
477 setValue( xSheet
, "F26", 1 );
478 getCellSeries( xSheet
, "E27:E31" ).fillAuto( com
.sun
.star
.sheet
.FillDirection
.TO_BOTTOM
, 1 );
479 getCellSeries( xSheet
, "F26:J26" ).fillAuto( com
.sun
.star
.sheet
.FillDirection
.TO_RIGHT
, 1 );
480 setFormula( xSheet
, "F33", "=SIN(E33)" );
481 setFormula( xSheet
, "G33", "=COS(E33)" );
482 setFormula( xSheet
, "H33", "=TAN(E33)" );
483 setValue( xSheet
, "E34", 0 );
484 setValue( xSheet
, "E35", 0.2 );
485 getCellSeries( xSheet
, "E34:E38" ).fillAuto( com
.sun
.star
.sheet
.FillDirection
.TO_BOTTOM
, 2 );
486 prepareRange( xSheet
, "E25:J38", "XMultipleOperation" );
488 com
.sun
.star
.table
.CellRangeAddress aFormulaRange
= createCellRangeAddress( xSheet
, "E26" );
489 com
.sun
.star
.table
.CellAddress aColCell
= createCellAddress( xSheet
, "E27" );
490 com
.sun
.star
.table
.CellAddress aRowCell
= createCellAddress( xSheet
, "F26" );
492 xCellRange
= xSheet
.getCellRangeByName( "E26:J31" );
493 com
.sun
.star
.sheet
.XMultipleOperation xMultOp
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XMultipleOperation
.class, xCellRange
);
494 xMultOp
.setTableOperation(
495 aFormulaRange
, com
.sun
.star
.sheet
.TableOperationMode
.BOTH
, aColCell
, aRowCell
);
497 aFormulaRange
= createCellRangeAddress( xSheet
, "F33:H33" );
498 aColCell
= createCellAddress( xSheet
, "E33" );
499 // Row cell not needed
501 xCellRange
= xSheet
.getCellRangeByName( "E34:H38" );
502 xMultOp
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XMultipleOperation
.class, xCellRange
);
503 xMultOp
.setTableOperation(
504 aFormulaRange
, com
.sun
.star
.sheet
.TableOperationMode
.COLUMN
, aColCell
, aRowCell
);
507 // --- Cell Ranges Query ---
508 xCellRange
= xSheet
.getCellRangeByName( "A10:C30" );
509 com
.sun
.star
.sheet
.XCellRangesQuery xRangesQuery
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XCellRangesQuery
.class, xCellRange
);
510 com
.sun
.star
.sheet
.XSheetCellRanges xCellRanges
=
511 xRangesQuery
.queryContentCells( (short)com
.sun
.star
.sheet
.CellFlags
.STRING
);
513 "Cells in A10:C30 containing text: "
514 + xCellRanges
.getRangeAddressesAsString() );
517 /** Returns the XCellSeries interface of a cell range.
518 @param xSheet The spreadsheet containing the cell range.
519 @param aRange The address of the cell range.
520 @return The XCellSeries interface. */
521 private com
.sun
.star
.sheet
.XCellSeries
getCellSeries(
522 com
.sun
.star
.sheet
.XSpreadsheet xSheet
, String aRange
)
524 return UnoRuntime
.queryInterface(
525 com
.sun
.star
.sheet
.XCellSeries
.class, xSheet
.getCellRangeByName( aRange
) );
530 /** All samples regarding cell range collections. */
531 private void doCellRangesSamples() throws RuntimeException
, Exception
533 System
.out
.println( "\n*** Samples for cell range collections ***\n" );
535 // Create a new cell range container
536 com
.sun
.star
.lang
.XMultiServiceFactory xDocFactory
=
537 UnoRuntime
.queryInterface(
538 com
.sun
.star
.lang
.XMultiServiceFactory
.class, getDocument() );
539 com
.sun
.star
.sheet
.XSheetCellRangeContainer xRangeCont
=
540 UnoRuntime
.queryInterface(
541 com
.sun
.star
.sheet
.XSheetCellRangeContainer
.class,
542 xDocFactory
.createInstance( "com.sun.star.sheet.SheetCellRanges" ) );
545 // --- Insert ranges ---
546 insertRange( xRangeCont
, 0, 0, 0, 0, 0, false ); // A1:A1
547 insertRange( xRangeCont
, 0, 0, 1, 0, 2, true ); // A2:A3
548 insertRange( xRangeCont
, 0, 1, 0, 1, 2, false ); // B1:B3
551 // --- Query the list of filled cells ---
552 System
.out
.print( "All filled cells: " );
553 com
.sun
.star
.container
.XEnumerationAccess xCellsEA
= xRangeCont
.getCells();
554 com
.sun
.star
.container
.XEnumeration xEnum
= xCellsEA
.createEnumeration();
555 while( xEnum
.hasMoreElements() )
557 Object aCellObj
= xEnum
.nextElement();
558 com
.sun
.star
.sheet
.XCellAddressable xAddr
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XCellAddressable
.class, aCellObj
);
559 com
.sun
.star
.table
.CellAddress aAddr
= xAddr
.getCellAddress();
560 System
.out
.print( getCellAddressString( aAddr
.Column
, aAddr
.Row
) + " " );
562 System
.out
.println();
565 /** Inserts a cell range address into a cell range container and prints
567 @param xContainer The com.sun.star.sheet.XSheetCellRangeContainer interface of the container.
568 @param nSheet Index of sheet of the range.
569 @param nStartCol Index of first column of the range.
570 @param nStartRow Index of first row of the range.
571 @param nEndCol Index of last column of the range.
572 @param nEndRow Index of last row of the range.
573 @param bMerge Determines whether the new range should be merged with the existing ranges. */
574 private void insertRange(
575 com
.sun
.star
.sheet
.XSheetCellRangeContainer xContainer
,
576 int nSheet
, int nStartCol
, int nStartRow
, int nEndCol
, int nEndRow
,
577 boolean bMerge
) throws RuntimeException
, Exception
579 com
.sun
.star
.table
.CellRangeAddress aAddress
= new com
.sun
.star
.table
.CellRangeAddress();
580 aAddress
.Sheet
= (short)nSheet
;
581 aAddress
.StartColumn
= nStartCol
;
582 aAddress
.StartRow
= nStartRow
;
583 aAddress
.EndColumn
= nEndCol
;
584 aAddress
.EndRow
= nEndRow
;
585 xContainer
.addRangeAddress( aAddress
, bMerge
);
587 "Inserting " + getCellRangeAddressString( aAddress
)
588 + " " + (bMerge ?
" with" : "without") + " merge,"
589 + " resulting list: " + xContainer
.getRangeAddressesAsString() );
594 /** All samples regarding cell cursors. */
595 private void doCellCursorSamples() throws RuntimeException
, Exception
597 System
.out
.println( "\n*** Samples for cell cursor ***\n" );
598 com
.sun
.star
.sheet
.XSpreadsheet xSheet
= getSpreadsheet( 0 );
601 // --- Find the array formula using a cell cursor ---
602 com
.sun
.star
.table
.XCellRange xRange
= xSheet
.getCellRangeByName( "F22" );
603 com
.sun
.star
.sheet
.XSheetCellRange xCellRange
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XSheetCellRange
.class, xRange
);
604 com
.sun
.star
.sheet
.XSheetCellCursor xCursor
= xSheet
.createCursorByRange( xCellRange
);
606 xCursor
.collapseToCurrentArray();
607 com
.sun
.star
.sheet
.XArrayFormulaRange xArray
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XArrayFormulaRange
.class, xCursor
);
609 "Array formula in " + getCellRangeAddressString( xCursor
, false )
610 + " contains formula " + xArray
.getArrayFormula() );
613 // --- Find the used area ---
614 com
.sun
.star
.sheet
.XUsedAreaCursor xUsedCursor
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XUsedAreaCursor
.class, xCursor
);
615 xUsedCursor
.gotoStartOfUsedArea( false );
616 xUsedCursor
.gotoEndOfUsedArea( true );
617 // xUsedCursor and xCursor are interfaces of the same object -
618 // so modifying xUsedCursor takes effect on xCursor:
619 System
.out
.println( "The used area is: " + getCellRangeAddressString( xCursor
, true ) );
624 /** All samples regarding the formatting of cells and ranges. */
625 private void doFormattingSamples() throws RuntimeException
, Exception
627 System
.out
.println( "\n*** Formatting samples ***\n" );
628 com
.sun
.star
.sheet
.XSpreadsheet xSheet
= getSpreadsheet( 1 );
629 com
.sun
.star
.table
.XCellRange xCellRange
;
630 com
.sun
.star
.beans
.XPropertySet xPropSet
= null;
631 com
.sun
.star
.container
.XIndexAccess xRangeIA
= null;
632 com
.sun
.star
.lang
.XMultiServiceFactory xDocServiceManager
;
635 // --- Cell styles ---
636 // get the cell style container
637 com
.sun
.star
.style
.XStyleFamiliesSupplier xFamiliesSupplier
= UnoRuntime
.queryInterface( com
.sun
.star
.style
.XStyleFamiliesSupplier
.class, getDocument() );
638 com
.sun
.star
.container
.XNameAccess xFamiliesNA
= xFamiliesSupplier
.getStyleFamilies();
639 Object aCellStylesObj
= xFamiliesNA
.getByName( "CellStyles" );
640 com
.sun
.star
.container
.XNameContainer xCellStylesNA
= UnoRuntime
.queryInterface( com
.sun
.star
.container
.XNameContainer
.class, aCellStylesObj
);
642 // create a new cell style
643 xDocServiceManager
= UnoRuntime
.queryInterface( com
.sun
.star
.lang
.XMultiServiceFactory
.class, getDocument() );
644 Object aCellStyle
= xDocServiceManager
.createInstance( "com.sun.star.style.CellStyle" );
645 String aStyleName
= "MyNewCellStyle";
646 xCellStylesNA
.insertByName( aStyleName
, aCellStyle
);
648 // modify properties of the new style
649 xPropSet
= UnoRuntime
.queryInterface( com
.sun
.star
.beans
.XPropertySet
.class, aCellStyle
);
650 xPropSet
.setPropertyValue( "CellBackColor", Integer
.valueOf( 0x888888 ) );
651 xPropSet
.setPropertyValue( "IsCellBackgroundTransparent", Boolean
.FALSE
);
655 // --- Query equal-formatted cell ranges ---
656 // prepare example, use the new cell style
657 xCellRange
= xSheet
.getCellRangeByName( "D2:F2" );
658 xPropSet
= UnoRuntime
.queryInterface(
659 com
.sun
.star
.beans
.XPropertySet
.class, xCellRange
);
660 xPropSet
.setPropertyValue( "CellStyle", aStyleName
);
662 xCellRange
= xSheet
.getCellRangeByName( "A3:G3" );
663 xPropSet
= UnoRuntime
.queryInterface(
664 com
.sun
.star
.beans
.XPropertySet
.class, xCellRange
);
665 xPropSet
.setPropertyValue( "CellStyle", aStyleName
);
667 // All ranges in one container
668 xCellRange
= xSheet
.getCellRangeByName( "A1:G3" );
669 System
.out
.println( "Service CellFormatRanges:" );
670 com
.sun
.star
.sheet
.XCellFormatRangesSupplier xFormatSupp
=
671 UnoRuntime
.queryInterface(
672 com
.sun
.star
.sheet
.XCellFormatRangesSupplier
.class, xCellRange
);
673 xRangeIA
= xFormatSupp
.getCellFormatRanges();
674 System
.out
.println( getCellRangeListString( xRangeIA
) );
676 // Ranges sorted in SheetCellRanges containers
677 System
.out
.println( "\nService UniqueCellFormatRanges:" );
678 com
.sun
.star
.sheet
.XUniqueCellFormatRangesSupplier xUniqueFormatSupp
=
679 UnoRuntime
.queryInterface(
680 com
.sun
.star
.sheet
.XUniqueCellFormatRangesSupplier
.class, xCellRange
);
681 com
.sun
.star
.container
.XIndexAccess xRangesIA
= xUniqueFormatSupp
.getUniqueCellFormatRanges();
682 int nCount
= xRangesIA
.getCount();
683 for (int nIndex
= 0; nIndex
< nCount
; ++nIndex
)
685 Object aRangesObj
= xRangesIA
.getByIndex( nIndex
);
686 xRangeIA
= UnoRuntime
.queryInterface(
687 com
.sun
.star
.container
.XIndexAccess
.class, aRangesObj
);
689 "Container " + (nIndex
+ 1) + ": " + getCellRangeListString( xRangeIA
) );
693 // --- Table auto formats ---
694 // get the global collection of table auto formats, use global service
696 com
.sun
.star
.lang
.XMultiComponentFactory xServiceManager
= getServiceManager();
698 Object aAutoFormatsObj
= xServiceManager
.createInstanceWithContext(
699 "com.sun.star.sheet.TableAutoFormats", getContext());
700 com
.sun
.star
.container
.XNameContainer xAutoFormatsNA
=
701 UnoRuntime
.queryInterface(
702 com
.sun
.star
.container
.XNameContainer
.class, aAutoFormatsObj
);
704 // create a new table auto format and insert into the container
705 String aAutoFormatName
= "Temp_Example";
706 boolean bExistsAlready
= xAutoFormatsNA
.hasByName( aAutoFormatName
);
707 Object aAutoFormatObj
= null;
709 // auto format already exists -> use it
710 aAutoFormatObj
= xAutoFormatsNA
.getByName( aAutoFormatName
);
713 aAutoFormatObj
= xDocServiceManager
.createInstance(
714 "com.sun.star.sheet.TableAutoFormat" );
715 xAutoFormatsNA
.insertByName( aAutoFormatName
, aAutoFormatObj
);
717 // index access to the auto format fields
718 com
.sun
.star
.container
.XIndexAccess xAutoFormatIA
=
719 UnoRuntime
.queryInterface(
720 com
.sun
.star
.container
.XIndexAccess
.class, aAutoFormatObj
);
722 // set properties of all auto format fields
723 for (int nRow
= 0; nRow
< 4; ++nRow
)
728 case 0: nRowColor
= 0x999999; break;
729 case 1: nRowColor
= 0xFFFFCC; break;
730 case 2: nRowColor
= 0xEEEEEE; break;
731 case 3: nRowColor
= 0x999999; break;
734 for (int nColumn
= 0; nColumn
< 4; ++nColumn
)
736 int nColor
= nRowColor
;
737 if ((nColumn
== 0) || (nColumn
== 3))
740 // get the auto format field and apply properties
741 Object aFieldObj
= xAutoFormatIA
.getByIndex( 4 * nRow
+ nColumn
);
742 xPropSet
= UnoRuntime
.queryInterface(
743 com
.sun
.star
.beans
.XPropertySet
.class, aFieldObj
);
744 xPropSet
.setPropertyValue( "CellBackColor", Integer
.valueOf( nColor
) );
748 // set the auto format to the spreadsheet
749 xCellRange
= xSheet
.getCellRangeByName( "A5:H25" );
750 com
.sun
.star
.table
.XAutoFormattable xAutoForm
= UnoRuntime
.queryInterface( com
.sun
.star
.table
.XAutoFormattable
.class, xCellRange
);
751 xAutoForm
.autoFormat( aAutoFormatName
);
753 // remove the auto format
755 xAutoFormatsNA
.removeByName( aAutoFormatName
);
758 // --- Conditional formats ---
759 xSheet
= getSpreadsheet( 0 );
760 prepareRange( xSheet
, "K20:K23", "Cond. Format" );
761 setValue( xSheet
, "K21", 1 );
762 setValue( xSheet
, "K22", 2 );
763 setValue( xSheet
, "K23", 3 );
765 // get the conditional format object of the cell range
766 xCellRange
= xSheet
.getCellRangeByName( "K21:K23" );
767 xPropSet
= UnoRuntime
.queryInterface( com
.sun
.star
.beans
.XPropertySet
.class, xCellRange
);
768 com
.sun
.star
.sheet
.XSheetConditionalEntries xEntries
=
769 UnoRuntime
.queryInterface(
770 com
.sun
.star
.sheet
.XSheetConditionalEntries
.class,
771 xPropSet
.getPropertyValue( "ConditionalFormat" ));
773 // create a condition and apply it to the range
774 com
.sun
.star
.beans
.PropertyValue
[] aCondition
= new com
.sun
.star
.beans
.PropertyValue
[3];
775 aCondition
[0] = new com
.sun
.star
.beans
.PropertyValue();
776 aCondition
[0].Name
= "Operator";
777 aCondition
[0].Value
= com
.sun
.star
.sheet
.ConditionOperator
.GREATER
;
778 aCondition
[1] = new com
.sun
.star
.beans
.PropertyValue();
779 aCondition
[1].Name
= "Formula1";
780 aCondition
[1].Value
= "1";
781 aCondition
[2] = new com
.sun
.star
.beans
.PropertyValue();
782 aCondition
[2].Name
= "StyleName";
783 aCondition
[2].Value
= aStyleName
;
784 xEntries
.addNew( aCondition
);
785 xPropSet
.setPropertyValue( "ConditionalFormat", xEntries
);
790 /** All samples regarding the spreadsheet document. */
791 private void doDocumentSamples() throws RuntimeException
, Exception
793 System
.out
.println( "\n*** Samples for spreadsheet document ***\n" );
796 // --- Insert a new spreadsheet ---
797 com
.sun
.star
.sheet
.XSpreadsheet xSheet
= insertSpreadsheet( "A new sheet", (short)0x7FFF );
800 // --- Copy a cell range ---
801 prepareRange( xSheet
, "A1:B3", "Copy from" );
802 prepareRange( xSheet
, "D1:E3", "To" );
803 setValue( xSheet
, "A2", 123 );
804 setValue( xSheet
, "B2", 345 );
805 setFormula( xSheet
, "A3", "=SUM(A2:B2)" );
806 setFormula( xSheet
, "B3", "=FORMULA(A3)" );
808 com
.sun
.star
.sheet
.XCellRangeMovement xMovement
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XCellRangeMovement
.class, xSheet
);
809 com
.sun
.star
.table
.CellRangeAddress aSourceRange
= createCellRangeAddress( xSheet
, "A2:B3" );
810 com
.sun
.star
.table
.CellAddress aDestCell
= createCellAddress( xSheet
, "D2" );
811 xMovement
.copyRange( aDestCell
, aSourceRange
);
814 // --- Print automatic column page breaks ---
815 com
.sun
.star
.sheet
.XSheetPageBreak xPageBreak
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XSheetPageBreak
.class, xSheet
);
816 com
.sun
.star
.sheet
.TablePageBreakData
[] aPageBreakArray
= xPageBreak
.getColumnPageBreaks();
818 System
.out
.print( "Automatic column page breaks:" );
819 for (int nIndex
= 0; nIndex
< aPageBreakArray
.length
; ++nIndex
)
820 if (!aPageBreakArray
[nIndex
].ManualBreak
)
821 System
.out
.print( " " + aPageBreakArray
[nIndex
].Position
);
822 System
.out
.println();
825 // --- Document properties ---
826 com
.sun
.star
.beans
.XPropertySet xPropSet
= UnoRuntime
.queryInterface( com
.sun
.star
.beans
.XPropertySet
.class, getDocument() );
828 String aText
= "Value of property IsIterationEnabled: ";
829 aText
+= AnyConverter
.toBoolean(xPropSet
.getPropertyValue( "IsIterationEnabled" ));
830 System
.out
.println( aText
);
831 aText
= "Value of property IterationCount: ";
832 aText
+= AnyConverter
.toInt(xPropSet
.getPropertyValue( "IterationCount" ));
833 System
.out
.println( aText
);
834 aText
= "Value of property NullDate: ";
835 com
.sun
.star
.util
.Date aDate
= (com
.sun
.star
.util
.Date
)
836 AnyConverter
.toObject(com
.sun
.star
.util
.Date
.class, xPropSet
.getPropertyValue( "NullDate" ));
837 aText
+= aDate
.Year
+ "-" + aDate
.Month
+ "-" + aDate
.Day
;
838 System
.out
.println( aText
);
841 // --- Data validation ---
842 prepareRange( xSheet
, "A5:C7", "Validation" );
843 setFormula( xSheet
, "A6", "Insert values between 0.0 and 5.0 below:" );
845 com
.sun
.star
.table
.XCellRange xCellRange
= xSheet
.getCellRangeByName( "A7:C7" );
846 com
.sun
.star
.beans
.XPropertySet xCellPropSet
= UnoRuntime
.queryInterface( com
.sun
.star
.beans
.XPropertySet
.class, xCellRange
);
847 // validation properties
848 com
.sun
.star
.beans
.XPropertySet xValidPropSet
= UnoRuntime
.queryInterface(com
.sun
.star
.beans
.XPropertySet
.class,
849 xCellPropSet
.getPropertyValue( "Validation" ));
850 xValidPropSet
.setPropertyValue( "Type", com
.sun
.star
.sheet
.ValidationType
.DECIMAL
);
851 xValidPropSet
.setPropertyValue( "ShowErrorMessage", Boolean
.TRUE
);
852 xValidPropSet
.setPropertyValue( "ErrorMessage", "This is an invalid value!" );
853 xValidPropSet
.setPropertyValue( "ErrorAlertStyle", com
.sun
.star
.sheet
.ValidationAlertStyle
.STOP
);
855 com
.sun
.star
.sheet
.XSheetCondition xCondition
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XSheetCondition
.class, xValidPropSet
);
856 xCondition
.setOperator( com
.sun
.star
.sheet
.ConditionOperator
.BETWEEN
);
857 xCondition
.setFormula1( "0.0" );
858 xCondition
.setFormula2( "5.0" );
859 // apply on cell range
860 xCellPropSet
.setPropertyValue( "Validation", xValidPropSet
);
863 Object
[][] aValues
= new Object
[2][2];
865 aValues
[0][0] = new Double( 11 );
866 aValues
[0][1] = new Double( 12 );
867 aValues
[1][0] = "Test13";
868 aValues
[1][1] = "Test14";
869 insertScenario( xSheet
, "B10:C11", aValues
, "First Scenario", "The first scenario." );
871 aValues
[0][0] = "Test21";
872 aValues
[0][1] = "Test22";
873 aValues
[1][0] = new Double( 23 );
874 aValues
[1][1] = new Double( 24 );
875 insertScenario( xSheet
, "B10:C11", aValues
, "Second Scenario", "The visible scenario." );
877 aValues
[0][0] = new Double( 31 );
878 aValues
[0][1] = new Double( 32 );
879 aValues
[1][0] = "Test33";
880 aValues
[1][1] = "Test34";
881 insertScenario( xSheet
, "B10:C11", aValues
, "Third Scenario", "The last scenario." );
883 // show second scenario
884 showScenario( xSheet
, "Second Scenario" );
887 /** Inserts a scenario containing one cell range into a sheet and
888 applies the value array.
889 @param xSheet The XSpreadsheet interface of the spreadsheet.
890 @param aRange The range address for the scenario.
891 @param aValueArray The array of cell contents.
892 @param aScenarioName The name of the new scenario.
893 @param aScenarioComment The user comment for the scenario. */
894 private void insertScenario(
895 com
.sun
.star
.sheet
.XSpreadsheet xSheet
,
897 Object
[][] aValueArray
,
898 String aScenarioName
,
899 String aScenarioComment
) throws RuntimeException
, Exception
901 // get the cell range with the given address
902 com
.sun
.star
.table
.XCellRange xCellRange
= xSheet
.getCellRangeByName( aRange
);
904 // create the range address sequence
905 com
.sun
.star
.sheet
.XCellRangeAddressable xAddr
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XCellRangeAddressable
.class, xCellRange
);
906 com
.sun
.star
.table
.CellRangeAddress
[] aRangesSeq
= new com
.sun
.star
.table
.CellRangeAddress
[1];
907 aRangesSeq
[0] = xAddr
.getRangeAddress();
909 // create the scenario
910 com
.sun
.star
.sheet
.XScenariosSupplier xScenSupp
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XScenariosSupplier
.class, xSheet
);
911 com
.sun
.star
.sheet
.XScenarios xScenarios
= xScenSupp
.getScenarios();
912 xScenarios
.addNewByName( aScenarioName
, aRangesSeq
, aScenarioComment
);
914 // insert the values into the range
915 com
.sun
.star
.sheet
.XCellRangeData xData
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XCellRangeData
.class, xCellRange
);
916 xData
.setDataArray( aValueArray
);
919 /** Activates a scenario.
920 @param xSheet The XSpreadsheet interface of the spreadsheet.
921 @param aScenarioName The name of the scenario. */
922 private void showScenario(
923 com
.sun
.star
.sheet
.XSpreadsheet xSheet
,
924 String aScenarioName
) throws RuntimeException
, Exception
926 // get the scenario set
927 com
.sun
.star
.sheet
.XScenariosSupplier xScenSupp
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XScenariosSupplier
.class, xSheet
);
928 com
.sun
.star
.sheet
.XScenarios xScenarios
= xScenSupp
.getScenarios();
930 // get the scenario and activate it
931 Object aScenarioObj
= xScenarios
.getByName( aScenarioName
);
932 com
.sun
.star
.sheet
.XScenario xScenario
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XScenario
.class, aScenarioObj
);
938 private void doNamedRangesSamples() throws RuntimeException
, Exception
940 System
.out
.println( "\n*** Samples for named ranges ***\n" );
941 com
.sun
.star
.sheet
.XSpreadsheetDocument xDocument
= getDocument();
942 com
.sun
.star
.sheet
.XSpreadsheet xSheet
= getSpreadsheet( 0 );
945 // --- Named ranges ---
946 prepareRange( xSheet
, "G42:H45", "Named ranges" );
947 xSheet
.getCellByPosition( 6, 42 ).setValue( 1 );
948 xSheet
.getCellByPosition( 6, 43 ).setValue( 2 );
949 xSheet
.getCellByPosition( 7, 42 ).setValue( 3 );
950 xSheet
.getCellByPosition( 7, 43 ).setValue( 4 );
952 // insert a named range
953 com
.sun
.star
.beans
.XPropertySet xDocProp
= UnoRuntime
.queryInterface( com
.sun
.star
.beans
.XPropertySet
.class, xDocument
);
954 Object aRangesObj
= xDocProp
.getPropertyValue( "NamedRanges" );
955 com
.sun
.star
.sheet
.XNamedRanges xNamedRanges
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XNamedRanges
.class, aRangesObj
);
956 com
.sun
.star
.table
.CellAddress aRefPos
= new com
.sun
.star
.table
.CellAddress();
960 xNamedRanges
.addNewByName( "ExampleName", "SUM(G43:G44)", aRefPos
, 0 );
962 // use the named range in formulas
963 xSheet
.getCellByPosition( 6, 44 ).setFormula( "=ExampleName" );
964 xSheet
.getCellByPosition( 7, 44 ).setFormula( "=ExampleName" );
967 // --- Label ranges ---
968 prepareRange( xSheet
, "G47:I50", "Label ranges" );
969 com
.sun
.star
.table
.XCellRange xRange
= xSheet
.getCellRangeByPosition( 6, 47, 7, 49 );
970 com
.sun
.star
.sheet
.XCellRangeData xData
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XCellRangeData
.class, xRange
);
973 { "Apples", "Oranges" },
974 { new Double( 5 ), new Double( 7 ) },
975 { new Double( 6 ), new Double( 8 ) }
977 xData
.setDataArray( aValues
);
979 // insert a column label range
980 Object aLabelsObj
= xDocProp
.getPropertyValue( "ColumnLabelRanges" );
981 com
.sun
.star
.sheet
.XLabelRanges xLabelRanges
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XLabelRanges
.class, aLabelsObj
);
982 com
.sun
.star
.table
.CellRangeAddress aLabelArea
= new com
.sun
.star
.table
.CellRangeAddress();
983 aLabelArea
.Sheet
= 0;
984 aLabelArea
.StartColumn
= 6;
985 aLabelArea
.StartRow
= 47;
986 aLabelArea
.EndColumn
= 7;
987 aLabelArea
.EndRow
= 47;
988 com
.sun
.star
.table
.CellRangeAddress aDataArea
= new com
.sun
.star
.table
.CellRangeAddress();
990 aDataArea
.StartColumn
= 6;
991 aDataArea
.StartRow
= 48;
992 aDataArea
.EndColumn
= 7;
993 aDataArea
.EndRow
= 49;
994 xLabelRanges
.addNew( aLabelArea
, aDataArea
);
996 // use the label range in formulas
997 xSheet
.getCellByPosition( 8, 48 ).setFormula( "=Apples+Oranges" );
998 xSheet
.getCellByPosition( 8, 49 ).setFormula( "=Apples+Oranges" );
1003 /** Helper for doDatabaseSamples: get name of first database. */
1004 private String
getFirstDatabaseName()
1006 String aDatabase
= null;
1009 com
.sun
.star
.lang
.XMultiComponentFactory xServiceManager
= getServiceManager();
1010 com
.sun
.star
.container
.XNameAccess xContext
=
1011 UnoRuntime
.queryInterface(
1012 com
.sun
.star
.container
.XNameAccess
.class,
1013 xServiceManager
.createInstanceWithContext(
1014 "com.sun.star.sdb.DatabaseContext", getContext()) );
1015 String
[] aNames
= xContext
.getElementNames();
1016 if ( aNames
.length
> 0 )
1017 aDatabase
= aNames
[0];
1019 catch ( Exception e
)
1021 System
.out
.println( "\nError: caught exception in getFirstDatabaseName()!\n" +
1022 "Exception Message = "
1024 e
.printStackTrace();
1029 /** Helper for doDatabaseSamples: get name of first table in a database. */
1030 private String
getFirstTableName( String aDatabase
)
1032 if ( aDatabase
== null )
1035 String aTable
= null;
1038 com
.sun
.star
.lang
.XMultiComponentFactory xServiceManager
= getServiceManager();
1039 com
.sun
.star
.container
.XNameAccess xContext
= UnoRuntime
.queryInterface( com
.sun
.star
.container
.XNameAccess
.class,
1040 xServiceManager
.createInstanceWithContext(
1041 "com.sun.star.sdb.DatabaseContext", getContext()) );
1042 com
.sun
.star
.sdb
.XCompletedConnection xSource
=
1043 UnoRuntime
.queryInterface(
1044 com
.sun
.star
.sdb
.XCompletedConnection
.class,
1045 xContext
.getByName( aDatabase
) );
1046 com
.sun
.star
.task
.XInteractionHandler xHandler
=
1047 UnoRuntime
.queryInterface(
1048 com
.sun
.star
.task
.XInteractionHandler
.class,
1049 xServiceManager
.createInstanceWithContext(
1050 "com.sun.star.task.InteractionHandler", getContext()) );
1051 com
.sun
.star
.sdbcx
.XTablesSupplier xSupplier
=
1052 UnoRuntime
.queryInterface(
1053 com
.sun
.star
.sdbcx
.XTablesSupplier
.class,
1054 xSource
.connectWithCompletion( xHandler
) );
1055 com
.sun
.star
.container
.XNameAccess xTables
= xSupplier
.getTables();
1056 String
[] aNames
= xTables
.getElementNames();
1057 if ( aNames
.length
> 0 )
1060 catch ( Exception e
)
1062 System
.out
.println( "\nError: caught exception in getFirstTableName()!\n" +
1063 "Exception Message = "
1065 e
.printStackTrace();
1070 private void doDatabaseSamples() throws Exception
1072 System
.out
.println( "\n*** Samples for database operations ***\n" );
1073 com
.sun
.star
.sheet
.XSpreadsheet xSheet
= getSpreadsheet( 2 );
1076 // --- put some example data into the sheet ---
1077 com
.sun
.star
.table
.XCellRange xRange
= xSheet
.getCellRangeByName( "B3:D24" );
1078 com
.sun
.star
.sheet
.XCellRangeData xData
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XCellRangeData
.class, xRange
);
1079 Object
[][] aValues
=
1081 { "Name", "Year", "Sales" },
1082 { "Alice", new Double( 2001 ), new Double( 4.0 ) },
1083 { "Carol", new Double( 1997 ), new Double( 3.0 ) },
1084 { "Carol", new Double( 1998 ), new Double( 8.0 ) },
1085 { "Bob", new Double( 1997 ), new Double( 8.0 ) },
1086 { "Alice", new Double( 2002 ), new Double( 9.0 ) },
1087 { "Alice", new Double( 1999 ), new Double( 7.0 ) },
1088 { "Alice", new Double( 1996 ), new Double( 3.0 ) },
1089 { "Bob", new Double( 2000 ), new Double( 1.0 ) },
1090 { "Carol", new Double( 1999 ), new Double( 5.0 ) },
1091 { "Bob", new Double( 2002 ), new Double( 1.0 ) },
1092 { "Carol", new Double( 2001 ), new Double( 5.0 ) },
1093 { "Carol", new Double( 2000 ), new Double( 1.0 ) },
1094 { "Carol", new Double( 1996 ), new Double( 8.0 ) },
1095 { "Bob", new Double( 1996 ), new Double( 7.0 ) },
1096 { "Alice", new Double( 1997 ), new Double( 3.0 ) },
1097 { "Alice", new Double( 2000 ), new Double( 9.0 ) },
1098 { "Bob", new Double( 1998 ), new Double( 1.0 ) },
1099 { "Bob", new Double( 1999 ), new Double( 6.0 ) },
1100 { "Carol", new Double( 2002 ), new Double( 8.0 ) },
1101 { "Alice", new Double( 1998 ), new Double( 5.0 ) },
1102 { "Bob", new Double( 2001 ), new Double( 6.0 ) }
1104 xData
.setDataArray( aValues
);
1107 // --- filter for second column >= 1998 ---
1108 com
.sun
.star
.sheet
.XSheetFilterable xFilter
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XSheetFilterable
.class, xRange
);
1109 com
.sun
.star
.sheet
.XSheetFilterDescriptor xFilterDesc
=
1110 xFilter
.createFilterDescriptor( true );
1111 com
.sun
.star
.sheet
.TableFilterField
[] aFilterFields
=
1112 new com
.sun
.star
.sheet
.TableFilterField
[1];
1113 aFilterFields
[0] = new com
.sun
.star
.sheet
.TableFilterField();
1114 aFilterFields
[0].Field
= 1;
1115 aFilterFields
[0].IsNumeric
= true;
1116 aFilterFields
[0].Operator
= com
.sun
.star
.sheet
.FilterOperator
.GREATER_EQUAL
;
1117 aFilterFields
[0].NumericValue
= 1998;
1118 xFilterDesc
.setFilterFields( aFilterFields
);
1119 com
.sun
.star
.beans
.XPropertySet xFilterProp
= UnoRuntime
.queryInterface( com
.sun
.star
.beans
.XPropertySet
.class, xFilterDesc
);
1120 xFilterProp
.setPropertyValue( "ContainsHeader", Boolean
.TRUE
);
1121 xFilter
.filter( xFilterDesc
);
1124 // --- do the same filter as above, using criteria from a cell range ---
1125 com
.sun
.star
.table
.XCellRange xCritRange
= xSheet
.getCellRangeByName( "B27:B28" );
1126 com
.sun
.star
.sheet
.XCellRangeData xCritData
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XCellRangeData
.class, xCritRange
);
1127 Object
[][] aCritValues
=
1132 xCritData
.setDataArray( aCritValues
);
1133 com
.sun
.star
.sheet
.XSheetFilterableEx xCriteria
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XSheetFilterableEx
.class, xCritRange
);
1134 xFilterDesc
= xCriteria
.createFilterDescriptorByObject( xFilter
);
1135 if ( xFilterDesc
!= null )
1136 xFilter
.filter( xFilterDesc
);
1139 // --- sort by second column, ascending ---
1140 com
.sun
.star
.table
.TableSortField
[] aSortFields
= new com
.sun
.star
.table
.TableSortField
[1];
1141 aSortFields
[0] = new com
.sun
.star
.table
.TableSortField();
1142 aSortFields
[0].Field
= 1;
1143 aSortFields
[0].IsAscending
= false;
1144 aSortFields
[0].IsCaseSensitive
= false;
1147 com
.sun
.star
.beans
.PropertyValue
[] aSortDesc
= new com
.sun
.star
.beans
.PropertyValue
[2];
1148 aSortDesc
[0] = new com
.sun
.star
.beans
.PropertyValue();
1149 aSortDesc
[0].Name
= "SortFields";
1150 aSortDesc
[0].Value
= aSortFields
;
1151 aSortDesc
[1] = new com
.sun
.star
.beans
.PropertyValue();
1152 aSortDesc
[1].Name
= "ContainsHeader";
1153 aSortDesc
[1].Value
= Boolean
.TRUE
;
1155 com
.sun
.star
.util
.XSortable xSort
= UnoRuntime
.queryInterface( com
.sun
.star
.util
.XSortable
.class, xRange
);
1156 xSort
.sort( aSortDesc
);
1159 // --- insert subtotals ---
1160 com
.sun
.star
.sheet
.XSubTotalCalculatable xSub
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XSubTotalCalculatable
.class, xRange
);
1161 com
.sun
.star
.sheet
.XSubTotalDescriptor xSubDesc
= xSub
.createSubTotalDescriptor( true );
1162 com
.sun
.star
.sheet
.SubTotalColumn
[] aColumns
= new com
.sun
.star
.sheet
.SubTotalColumn
[1];
1163 // calculate sum of third column
1164 aColumns
[0] = new com
.sun
.star
.sheet
.SubTotalColumn();
1165 aColumns
[0].Column
= 2;
1166 aColumns
[0].Function
= com
.sun
.star
.sheet
.GeneralFunction
.SUM
;
1167 // group by first column
1168 xSubDesc
.addNew( aColumns
, 0 );
1169 xSub
.applySubTotals( xSubDesc
, true );
1171 String aDatabase
= getFirstDatabaseName();
1172 String aTableName
= getFirstTableName( aDatabase
);
1173 if ( aDatabase
!= null && aTableName
!= null )
1175 // --- import from database ---
1176 com
.sun
.star
.beans
.PropertyValue
[] aImportDesc
= new com
.sun
.star
.beans
.PropertyValue
[3];
1177 aImportDesc
[0] = new com
.sun
.star
.beans
.PropertyValue();
1178 aImportDesc
[0].Name
= "DatabaseName";
1179 aImportDesc
[0].Value
= aDatabase
;
1180 aImportDesc
[1] = new com
.sun
.star
.beans
.PropertyValue();
1181 aImportDesc
[1].Name
= "SourceType";
1182 aImportDesc
[1].Value
= com
.sun
.star
.sheet
.DataImportMode
.TABLE
;
1183 aImportDesc
[2] = new com
.sun
.star
.beans
.PropertyValue();
1184 aImportDesc
[2].Name
= "SourceObject";
1185 aImportDesc
[2].Value
= aTableName
;
1187 com
.sun
.star
.table
.XCellRange xImportRange
= xSheet
.getCellRangeByName( "B35:B35" );
1188 com
.sun
.star
.util
.XImportable xImport
= UnoRuntime
.queryInterface( com
.sun
.star
.util
.XImportable
.class, xImportRange
);
1189 xImport
.doImport( aImportDesc
);
1192 // --- use the temporary database range to find the imported data's size ---
1193 com
.sun
.star
.beans
.XPropertySet xDocProp
= UnoRuntime
.queryInterface( com
.sun
.star
.beans
.XPropertySet
.class, getDocument() );
1194 Object aRangesObj
= xDocProp
.getPropertyValue( "DatabaseRanges" );
1195 com
.sun
.star
.container
.XNameAccess xRanges
=
1196 UnoRuntime
.queryInterface(
1197 com
.sun
.star
.container
.XNameAccess
.class, aRangesObj
);
1198 String
[] aNames
= xRanges
.getElementNames();
1199 for ( int i
=0; i
<aNames
.length
; i
++ )
1201 Object aRangeObj
= xRanges
.getByName( aNames
[i
] );
1202 com
.sun
.star
.beans
.XPropertySet xRangeProp
= UnoRuntime
.queryInterface( com
.sun
.star
.beans
.XPropertySet
.class, aRangeObj
);
1203 boolean bUser
= AnyConverter
.toBoolean(xRangeProp
.getPropertyValue( "IsUserDefined" ));
1206 // this is the temporary database range - get the cell range and format it
1207 com
.sun
.star
.sheet
.XCellRangeReferrer xRef
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XCellRangeReferrer
.class, aRangeObj
);
1208 com
.sun
.star
.table
.XCellRange xResultRange
= xRef
.getReferredCells();
1209 com
.sun
.star
.beans
.XPropertySet xResultProp
= UnoRuntime
.queryInterface( com
.sun
.star
.beans
.XPropertySet
.class, xResultRange
);
1210 xResultProp
.setPropertyValue( "IsCellBackgroundTransparent", Boolean
.FALSE
);
1211 xResultProp
.setPropertyValue( "CellBackColor", Integer
.valueOf( 0xFFFFCC ) );
1216 System
.out
.println("can't get database");
1221 private void doDataPilotSamples() throws Exception
1223 System
.out
.println( "\n*** Samples for Data Pilot ***\n" );
1224 com
.sun
.star
.sheet
.XSpreadsheet xSheet
= getSpreadsheet( 0 );
1227 // --- Create a new DataPilot table ---
1228 prepareRange( xSheet
, "A38:C38", "Data Pilot" );
1229 com
.sun
.star
.sheet
.XDataPilotTablesSupplier xDPSupp
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XDataPilotTablesSupplier
.class, xSheet
);
1230 com
.sun
.star
.sheet
.XDataPilotTables xDPTables
= xDPSupp
.getDataPilotTables();
1231 com
.sun
.star
.sheet
.XDataPilotDescriptor xDPDesc
= xDPTables
.createDataPilotDescriptor();
1232 // set source range (use data range from CellRange test)
1233 com
.sun
.star
.table
.CellRangeAddress aSourceAddress
= createCellRangeAddress( xSheet
, "A10:C30" );
1234 xDPDesc
.setSourceRange( aSourceAddress
);
1235 // settings for fields
1236 com
.sun
.star
.container
.XIndexAccess xFields
= xDPDesc
.getDataPilotFields();
1238 com
.sun
.star
.beans
.XPropertySet xFieldProp
;
1239 // use first column as column field
1240 aFieldObj
= xFields
.getByIndex(0);
1241 xFieldProp
= UnoRuntime
.queryInterface( com
.sun
.star
.beans
.XPropertySet
.class, aFieldObj
);
1242 xFieldProp
.setPropertyValue( "Orientation", com
.sun
.star
.sheet
.DataPilotFieldOrientation
.COLUMN
);
1243 // use second column as row field
1244 aFieldObj
= xFields
.getByIndex(1);
1245 xFieldProp
= UnoRuntime
.queryInterface( com
.sun
.star
.beans
.XPropertySet
.class, aFieldObj
);
1246 xFieldProp
.setPropertyValue( "Orientation", com
.sun
.star
.sheet
.DataPilotFieldOrientation
.ROW
);
1247 // use third column as data field, calculating the sum
1248 aFieldObj
= xFields
.getByIndex(2);
1249 xFieldProp
= UnoRuntime
.queryInterface( com
.sun
.star
.beans
.XPropertySet
.class, aFieldObj
);
1250 xFieldProp
.setPropertyValue( "Orientation", com
.sun
.star
.sheet
.DataPilotFieldOrientation
.DATA
);
1251 xFieldProp
.setPropertyValue( "Function", com
.sun
.star
.sheet
.GeneralFunction
.SUM
);
1252 // select output position
1253 com
.sun
.star
.table
.CellAddress aDestAddress
= createCellAddress( xSheet
, "A40" );
1254 xDPTables
.insertNewByName( "DataPilotExample", aDestAddress
, xDPDesc
);
1257 // --- Modify the DataPilot table ---
1258 Object aDPTableObj
= xDPTables
.getByName( "DataPilotExample" );
1259 xDPDesc
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XDataPilotDescriptor
.class, aDPTableObj
);
1260 xFields
= xDPDesc
.getDataPilotFields();
1261 // add a second data field from the third column, calculating the average
1262 aFieldObj
= xFields
.getByIndex(2);
1263 xFieldProp
= UnoRuntime
.queryInterface( com
.sun
.star
.beans
.XPropertySet
.class, aFieldObj
);
1264 xFieldProp
.setPropertyValue( "Orientation", com
.sun
.star
.sheet
.DataPilotFieldOrientation
.DATA
);
1265 xFieldProp
.setPropertyValue( "Function", com
.sun
.star
.sheet
.GeneralFunction
.AVERAGE
);
1270 private void doFunctionAccessSamples() throws RuntimeException
, Exception
1272 System
.out
.println( "\n*** Samples for function handling ***\n" );
1273 com
.sun
.star
.lang
.XMultiComponentFactory xServiceManager
= getServiceManager();
1276 // --- Calculate a function ---
1277 Object aFuncInst
= xServiceManager
.createInstanceWithContext(
1278 "com.sun.star.sheet.FunctionAccess", getContext());
1279 com
.sun
.star
.sheet
.XFunctionAccess xFuncAcc
=
1280 UnoRuntime
.queryInterface(
1281 com
.sun
.star
.sheet
.XFunctionAccess
.class, aFuncInst
);
1282 // put the data in a two-dimensional array
1283 double[][] aData
= { { 1.0, 2.0, 3.0 } };
1284 // construct the array of function arguments
1285 Object
[] aArgs
= new Object
[2];
1287 aArgs
[1] = new Double( 2.0 );
1288 Object aResult
= xFuncAcc
.callFunction( "ZTEST", aArgs
);
1289 System
.out
.println("ZTEST result for data {1,2,3} and value 2 is "
1290 + ((Double
)aResult
).doubleValue() );
1293 // --- Get the list of recently used functions ---
1294 Object aRecInst
= xServiceManager
.createInstanceWithContext(
1295 "com.sun.star.sheet.RecentFunctions", getContext());
1296 com
.sun
.star
.sheet
.XRecentFunctions xRecFunc
=
1297 UnoRuntime
.queryInterface(
1298 com
.sun
.star
.sheet
.XRecentFunctions
.class, aRecInst
);
1299 int[] nRecentIds
= xRecFunc
.getRecentFunctionIds();
1302 // --- Get the names for these functions ---
1303 Object aDescInst
= xServiceManager
.createInstanceWithContext(
1304 "com.sun.star.sheet.FunctionDescriptions", getContext());
1305 com
.sun
.star
.sheet
.XFunctionDescriptions xFuncDesc
=
1306 UnoRuntime
.queryInterface(
1307 com
.sun
.star
.sheet
.XFunctionDescriptions
.class, aDescInst
);
1308 System
.out
.print("Recently used functions: ");
1309 for (int nFunction
=0; nFunction
<nRecentIds
.length
; nFunction
++)
1311 com
.sun
.star
.beans
.PropertyValue
[] aProperties
=
1312 xFuncDesc
.getById( nRecentIds
[nFunction
] );
1313 for (int nProp
=0; nProp
<aProperties
.length
; nProp
++)
1314 if ( aProperties
[nProp
].Name
.equals( "Name" ) )
1315 System
.out
.print( aProperties
[nProp
].Value
+ " " );
1317 System
.out
.println();
1322 private void doApplicationSettingsSamples() throws RuntimeException
, Exception
1324 System
.out
.println( "\n*** Samples for application settings ***\n" );
1325 com
.sun
.star
.lang
.XMultiComponentFactory xServiceManager
= getServiceManager();
1328 // --- Get the user defined sort lists ---
1329 Object aSettings
= xServiceManager
.createInstanceWithContext(
1330 "com.sun.star.sheet.GlobalSheetSettings", getContext());
1331 com
.sun
.star
.beans
.XPropertySet xPropSet
=
1332 UnoRuntime
.queryInterface(
1333 com
.sun
.star
.beans
.XPropertySet
.class, aSettings
);
1334 String
[] aEntries
= (String
[])
1335 AnyConverter
.toObject(String
[].class,
1336 xPropSet
.getPropertyValue( "UserLists" ));
1337 System
.out
.println("User defined sort lists:");
1338 for ( int i
=0; i
<aEntries
.length
; i
++ )
1339 System
.out
.println( aEntries
[i
] );