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