1*34dd1e25SAndrew Rist /************************************************************** 2*34dd1e25SAndrew Rist * 3*34dd1e25SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*34dd1e25SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*34dd1e25SAndrew Rist * distributed with this work for additional information 6*34dd1e25SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*34dd1e25SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*34dd1e25SAndrew Rist * "License"); you may not use this file except in compliance 9*34dd1e25SAndrew Rist * with the License. You may obtain a copy of the License at 10*34dd1e25SAndrew Rist * 11*34dd1e25SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*34dd1e25SAndrew Rist * 13*34dd1e25SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*34dd1e25SAndrew Rist * software distributed under the License is distributed on an 15*34dd1e25SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*34dd1e25SAndrew Rist * KIND, either express or implied. See the License for the 17*34dd1e25SAndrew Rist * specific language governing permissions and limitations 18*34dd1e25SAndrew Rist * under the License. 19*34dd1e25SAndrew Rist * 20*34dd1e25SAndrew Rist *************************************************************/ 21*34dd1e25SAndrew Rist 22*34dd1e25SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir import com.sun.star.comp.servicemanager.ServiceManager; 25cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver; 26cdf0e10cSrcweir import com.sun.star.uno.XNamingService; 27cdf0e10cSrcweir import com.sun.star.frame.XDesktop; 28cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader; 29cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 30cdf0e10cSrcweir import com.sun.star.lang.XComponent; 31cdf0e10cSrcweir 32cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 33cdf0e10cSrcweir import com.sun.star.uno.RuntimeException; 34cdf0e10cSrcweir 35cdf0e10cSrcweir // __________ implementation ____________________________________ 36cdf0e10cSrcweir 37cdf0e10cSrcweir /** This is a helper class for the spreadsheet and table samples. 38cdf0e10cSrcweir It connects to a running office and creates a spreadsheet document. 39cdf0e10cSrcweir Additionally it contains various helper functions. 40cdf0e10cSrcweir */ 41cdf0e10cSrcweir public class SpreadsheetDocHelper 42cdf0e10cSrcweir { 43cdf0e10cSrcweir 44cdf0e10cSrcweir // __ private members ___________________________________________ 45cdf0e10cSrcweir 46cdf0e10cSrcweir private final String msDataSheetName = "Data"; 47cdf0e10cSrcweir 48cdf0e10cSrcweir private com.sun.star.uno.XComponentContext mxRemoteContext; 49cdf0e10cSrcweir private com.sun.star.lang.XMultiComponentFactory mxRemoteServiceManager; 50cdf0e10cSrcweir // private com.sun.star.lang.XMultiServiceFactory mxMSFactory; 51cdf0e10cSrcweir private com.sun.star.sheet.XSpreadsheetDocument mxDocument; 52cdf0e10cSrcweir 53cdf0e10cSrcweir // ________________________________________________________________ 54cdf0e10cSrcweir SpreadsheetDocHelper( String[] args )55cdf0e10cSrcweir public SpreadsheetDocHelper( String[] args ) 56cdf0e10cSrcweir { 57cdf0e10cSrcweir // Connect to a running office and get the service manager 58cdf0e10cSrcweir connect(); 59cdf0e10cSrcweir 60cdf0e10cSrcweir // Create a new spreadsheet document 61cdf0e10cSrcweir try 62cdf0e10cSrcweir { 63cdf0e10cSrcweir mxDocument = initDocument(); 64cdf0e10cSrcweir } 65cdf0e10cSrcweir catch (Exception ex) 66cdf0e10cSrcweir { 67cdf0e10cSrcweir System.err.println( "Couldn't create document: " + ex ); 68cdf0e10cSrcweir System.err.println( "Error: Couldn't create Document\nException Message = " 69cdf0e10cSrcweir + ex.getMessage()); 70cdf0e10cSrcweir ex.printStackTrace(); 71cdf0e10cSrcweir System.exit( 1 ); 72cdf0e10cSrcweir } 73cdf0e10cSrcweir } 74cdf0e10cSrcweir 75cdf0e10cSrcweir // __ helper methods ____________________________________________ 76cdf0e10cSrcweir 77cdf0e10cSrcweir /** Returns the service manager of the connected office. 78cdf0e10cSrcweir @return XMultiComponentFactory interface of the service manager. */ getServiceManager()79cdf0e10cSrcweir public com.sun.star.lang.XMultiComponentFactory getServiceManager() 80cdf0e10cSrcweir { 81cdf0e10cSrcweir return mxRemoteServiceManager; 82cdf0e10cSrcweir } 83cdf0e10cSrcweir 84cdf0e10cSrcweir /** Returns the component context of the connected office 85cdf0e10cSrcweir @return XComponentContext interface of the context. */ getContext()86cdf0e10cSrcweir public com.sun.star.uno.XComponentContext getContext() 87cdf0e10cSrcweir { 88cdf0e10cSrcweir return mxRemoteContext; 89cdf0e10cSrcweir } 90cdf0e10cSrcweir 91cdf0e10cSrcweir /** Returns the whole spreadsheet document. 92cdf0e10cSrcweir @return XSpreadsheetDocument interface of the document. */ getDocument()93cdf0e10cSrcweir public com.sun.star.sheet.XSpreadsheetDocument getDocument() 94cdf0e10cSrcweir { 95cdf0e10cSrcweir return mxDocument; 96cdf0e10cSrcweir } 97cdf0e10cSrcweir 98cdf0e10cSrcweir /** Returns the spreadsheet with the specified index (0-based). 99cdf0e10cSrcweir @param nIndex The index of the sheet. 100cdf0e10cSrcweir @return XSpreadsheet interface of the sheet. */ getSpreadsheet( int nIndex )101cdf0e10cSrcweir public com.sun.star.sheet.XSpreadsheet getSpreadsheet( int nIndex ) 102cdf0e10cSrcweir { 103cdf0e10cSrcweir // Collection of sheets 104cdf0e10cSrcweir com.sun.star.sheet.XSpreadsheets xSheets = mxDocument.getSheets(); 105cdf0e10cSrcweir com.sun.star.sheet.XSpreadsheet xSheet = null; 106cdf0e10cSrcweir try 107cdf0e10cSrcweir { 108cdf0e10cSrcweir com.sun.star.container.XIndexAccess xSheetsIA = 109cdf0e10cSrcweir (com.sun.star.container.XIndexAccess)UnoRuntime.queryInterface( 110cdf0e10cSrcweir com.sun.star.container.XIndexAccess.class, xSheets ); 111cdf0e10cSrcweir xSheet = (com.sun.star.sheet.XSpreadsheet) UnoRuntime.queryInterface( 112cdf0e10cSrcweir com.sun.star.sheet.XSpreadsheet.class, xSheetsIA.getByIndex(nIndex)); 113cdf0e10cSrcweir } 114cdf0e10cSrcweir catch (Exception ex) 115cdf0e10cSrcweir { 116cdf0e10cSrcweir System.err.println( "Error: caught exception in getSpreadsheet()!\nException Message = " 117cdf0e10cSrcweir + ex.getMessage()); 118cdf0e10cSrcweir ex.printStackTrace(); 119cdf0e10cSrcweir } 120cdf0e10cSrcweir return xSheet; 121cdf0e10cSrcweir } 122cdf0e10cSrcweir 123cdf0e10cSrcweir /** Inserts a new empty spreadsheet with the specified name. 124cdf0e10cSrcweir @param aName The name of the new sheet. 125cdf0e10cSrcweir @param nIndex The insertion index. 126cdf0e10cSrcweir @return The XSpreadsheet interface of the new sheet. */ insertSpreadsheet( String aName, short nIndex )127cdf0e10cSrcweir public com.sun.star.sheet.XSpreadsheet insertSpreadsheet( 128cdf0e10cSrcweir String aName, short nIndex ) 129cdf0e10cSrcweir { 130cdf0e10cSrcweir // Collection of sheets 131cdf0e10cSrcweir com.sun.star.sheet.XSpreadsheets xSheets = mxDocument.getSheets(); 132cdf0e10cSrcweir com.sun.star.sheet.XSpreadsheet xSheet = null; 133cdf0e10cSrcweir try 134cdf0e10cSrcweir { 135cdf0e10cSrcweir xSheets.insertNewByName( aName, nIndex ); 136cdf0e10cSrcweir xSheet = (com.sun.star.sheet.XSpreadsheet) 137cdf0e10cSrcweir UnoRuntime.queryInterface(com.sun.star.sheet.XSpreadsheet.class, 138cdf0e10cSrcweir xSheets.getByName( aName )); 139cdf0e10cSrcweir } 140cdf0e10cSrcweir catch (Exception ex) 141cdf0e10cSrcweir { 142cdf0e10cSrcweir System.err.println( "Error: caught exception in insertSpreadsheet()!\nException Message = " 143cdf0e10cSrcweir + ex.getMessage()); 144cdf0e10cSrcweir ex.printStackTrace(); 145cdf0e10cSrcweir } 146cdf0e10cSrcweir return xSheet; 147cdf0e10cSrcweir } 148cdf0e10cSrcweir 149cdf0e10cSrcweir // ________________________________________________________________ 150cdf0e10cSrcweir // Methods to fill values into cells. 151cdf0e10cSrcweir 152cdf0e10cSrcweir /** Writes a double value into a spreadsheet. 153cdf0e10cSrcweir @param xSheet The XSpreadsheet interface of the spreadsheet. 154cdf0e10cSrcweir @param aCellName The address of the cell (or a named range). 155cdf0e10cSrcweir @param fValue The value to write into the cell. */ setValue( com.sun.star.sheet.XSpreadsheet xSheet, String aCellName, double fValue )156cdf0e10cSrcweir public void setValue( 157cdf0e10cSrcweir com.sun.star.sheet.XSpreadsheet xSheet, 158cdf0e10cSrcweir String aCellName, 159cdf0e10cSrcweir double fValue ) throws RuntimeException, Exception 160cdf0e10cSrcweir { 161cdf0e10cSrcweir xSheet.getCellRangeByName( aCellName ).getCellByPosition( 0, 0 ).setValue( fValue ); 162cdf0e10cSrcweir } 163cdf0e10cSrcweir 164cdf0e10cSrcweir /** Writes a formula into a spreadsheet. 165cdf0e10cSrcweir @param xSheet The XSpreadsheet interface of the spreadsheet. 166cdf0e10cSrcweir @param aCellName The address of the cell (or a named range). 167cdf0e10cSrcweir @param aFormula The formula to write into the cell. */ setFormula( com.sun.star.sheet.XSpreadsheet xSheet, String aCellName, String aFormula )168cdf0e10cSrcweir public void setFormula( 169cdf0e10cSrcweir com.sun.star.sheet.XSpreadsheet xSheet, 170cdf0e10cSrcweir String aCellName, 171cdf0e10cSrcweir String aFormula ) throws RuntimeException, Exception 172cdf0e10cSrcweir { 173cdf0e10cSrcweir xSheet.getCellRangeByName( aCellName ).getCellByPosition( 0, 0 ).setFormula( aFormula ); 174cdf0e10cSrcweir } 175cdf0e10cSrcweir 176cdf0e10cSrcweir /** Writes a date with standard date format into a spreadsheet. 177cdf0e10cSrcweir @param xSheet The XSpreadsheet interface of the spreadsheet. 178cdf0e10cSrcweir @param aCellName The address of the cell (or a named range). 179cdf0e10cSrcweir @param nDay The day of the date. 180cdf0e10cSrcweir @param nMonth The month of the date. 181cdf0e10cSrcweir @param nYear The year of the date. */ setDate( com.sun.star.sheet.XSpreadsheet xSheet, String aCellName, int nDay, int nMonth, int nYear )182cdf0e10cSrcweir public void setDate( 183cdf0e10cSrcweir com.sun.star.sheet.XSpreadsheet xSheet, 184cdf0e10cSrcweir String aCellName, 185cdf0e10cSrcweir int nDay, int nMonth, int nYear ) throws RuntimeException, Exception 186cdf0e10cSrcweir { 187cdf0e10cSrcweir // Set the date value. 188cdf0e10cSrcweir com.sun.star.table.XCell xCell = xSheet.getCellRangeByName( aCellName ).getCellByPosition( 0, 0 ); 189cdf0e10cSrcweir String aDateStr = nMonth + "/" + nDay + "/" + nYear; 190cdf0e10cSrcweir xCell.setFormula( aDateStr ); 191cdf0e10cSrcweir 192cdf0e10cSrcweir // Set standard date format. 193cdf0e10cSrcweir com.sun.star.util.XNumberFormatsSupplier xFormatsSupplier = 194cdf0e10cSrcweir (com.sun.star.util.XNumberFormatsSupplier) UnoRuntime.queryInterface( 195cdf0e10cSrcweir com.sun.star.util.XNumberFormatsSupplier.class, getDocument() ); 196cdf0e10cSrcweir com.sun.star.util.XNumberFormatTypes xFormatTypes = 197cdf0e10cSrcweir (com.sun.star.util.XNumberFormatTypes) UnoRuntime.queryInterface( 198cdf0e10cSrcweir com.sun.star.util.XNumberFormatTypes.class, xFormatsSupplier.getNumberFormats() ); 199cdf0e10cSrcweir int nFormat = xFormatTypes.getStandardFormat( 200cdf0e10cSrcweir com.sun.star.util.NumberFormat.DATE, new com.sun.star.lang.Locale() ); 201cdf0e10cSrcweir 202cdf0e10cSrcweir com.sun.star.beans.XPropertySet xPropSet = (com.sun.star.beans.XPropertySet) 203cdf0e10cSrcweir UnoRuntime.queryInterface( com.sun.star.beans.XPropertySet.class, xCell ); 204cdf0e10cSrcweir xPropSet.setPropertyValue( "NumberFormat", new Integer( nFormat ) ); 205cdf0e10cSrcweir } 206cdf0e10cSrcweir 207cdf0e10cSrcweir /** Draws a colored border around the range and writes the headline in the 208cdf0e10cSrcweir first cell. 209cdf0e10cSrcweir @param xSheet The XSpreadsheet interface of the spreadsheet. 210cdf0e10cSrcweir @param aRange The address of the cell range (or a named range). 211cdf0e10cSrcweir @param aHeadline The headline text. */ prepareRange( com.sun.star.sheet.XSpreadsheet xSheet, String aRange, String aHeadline )212cdf0e10cSrcweir public void prepareRange( 213cdf0e10cSrcweir com.sun.star.sheet.XSpreadsheet xSheet, 214cdf0e10cSrcweir String aRange, String aHeadline ) throws RuntimeException, Exception 215cdf0e10cSrcweir { 216cdf0e10cSrcweir com.sun.star.beans.XPropertySet xPropSet = null; 217cdf0e10cSrcweir com.sun.star.table.XCellRange xCellRange = null; 218cdf0e10cSrcweir 219cdf0e10cSrcweir // draw border 220cdf0e10cSrcweir xCellRange = xSheet.getCellRangeByName( aRange ); 221cdf0e10cSrcweir xPropSet = (com.sun.star.beans.XPropertySet) 222cdf0e10cSrcweir UnoRuntime.queryInterface( com.sun.star.beans.XPropertySet.class, xCellRange ); 223cdf0e10cSrcweir com.sun.star.table.BorderLine aLine = new com.sun.star.table.BorderLine(); 224cdf0e10cSrcweir aLine.Color = 0x99CCFF; 225cdf0e10cSrcweir aLine.InnerLineWidth = aLine.LineDistance = 0; 226cdf0e10cSrcweir aLine.OuterLineWidth = 100; 227cdf0e10cSrcweir com.sun.star.table.TableBorder aBorder = new com.sun.star.table.TableBorder(); 228cdf0e10cSrcweir aBorder.TopLine = aBorder.BottomLine = aBorder.LeftLine = aBorder.RightLine = aLine; 229cdf0e10cSrcweir aBorder.IsTopLineValid = aBorder.IsBottomLineValid = true; 230cdf0e10cSrcweir aBorder.IsLeftLineValid = aBorder.IsRightLineValid = true; 231cdf0e10cSrcweir xPropSet.setPropertyValue( "TableBorder", aBorder ); 232cdf0e10cSrcweir 233cdf0e10cSrcweir // draw headline 234cdf0e10cSrcweir com.sun.star.sheet.XCellRangeAddressable xAddr = (com.sun.star.sheet.XCellRangeAddressable) 235cdf0e10cSrcweir UnoRuntime.queryInterface( com.sun.star.sheet.XCellRangeAddressable.class, xCellRange ); 236cdf0e10cSrcweir com.sun.star.table.CellRangeAddress aAddr = xAddr.getRangeAddress(); 237cdf0e10cSrcweir 238cdf0e10cSrcweir xCellRange = xSheet.getCellRangeByPosition( 239cdf0e10cSrcweir aAddr.StartColumn, aAddr.StartRow, aAddr.EndColumn, aAddr.StartRow ); 240cdf0e10cSrcweir xPropSet = (com.sun.star.beans.XPropertySet) 241cdf0e10cSrcweir UnoRuntime.queryInterface( com.sun.star.beans.XPropertySet.class, xCellRange ); 242cdf0e10cSrcweir xPropSet.setPropertyValue( "CellBackColor", new Integer( 0x99CCFF ) ); 243cdf0e10cSrcweir // write headline 244cdf0e10cSrcweir com.sun.star.table.XCell xCell = xCellRange.getCellByPosition( 0, 0 ); 245cdf0e10cSrcweir xCell.setFormula( aHeadline ); 246cdf0e10cSrcweir xPropSet = (com.sun.star.beans.XPropertySet) 247cdf0e10cSrcweir UnoRuntime.queryInterface( com.sun.star.beans.XPropertySet.class, xCell ); 248cdf0e10cSrcweir xPropSet.setPropertyValue( "CharColor", new Integer( 0x003399 ) ); 249cdf0e10cSrcweir xPropSet.setPropertyValue( "CharWeight", new Float( com.sun.star.awt.FontWeight.BOLD ) ); 250cdf0e10cSrcweir } 251cdf0e10cSrcweir 252cdf0e10cSrcweir // ________________________________________________________________ 253cdf0e10cSrcweir // Methods to create cell addresses and range addresses. 254cdf0e10cSrcweir 255cdf0e10cSrcweir /** Creates a com.sun.star.table.CellAddress and initializes it 256cdf0e10cSrcweir with the given range. 257cdf0e10cSrcweir @param xSheet The XSpreadsheet interface of the spreadsheet. 258cdf0e10cSrcweir @param aCell The address of the cell (or a named cell). */ createCellAddress( com.sun.star.sheet.XSpreadsheet xSheet, String aCell )259cdf0e10cSrcweir public com.sun.star.table.CellAddress createCellAddress( 260cdf0e10cSrcweir com.sun.star.sheet.XSpreadsheet xSheet, 261cdf0e10cSrcweir String aCell ) throws RuntimeException, Exception 262cdf0e10cSrcweir { 263cdf0e10cSrcweir com.sun.star.sheet.XCellAddressable xAddr = (com.sun.star.sheet.XCellAddressable) 264cdf0e10cSrcweir UnoRuntime.queryInterface( com.sun.star.sheet.XCellAddressable.class, 265cdf0e10cSrcweir xSheet.getCellRangeByName( aCell ).getCellByPosition( 0, 0 ) ); 266cdf0e10cSrcweir return xAddr.getCellAddress(); 267cdf0e10cSrcweir } 268cdf0e10cSrcweir 269cdf0e10cSrcweir /** Creates a com.sun.star.table.CellRangeAddress and initializes 270cdf0e10cSrcweir it with the given range. 271cdf0e10cSrcweir @param xSheet The XSpreadsheet interface of the spreadsheet. 272cdf0e10cSrcweir @param aRange The address of the cell range (or a named range). */ createCellRangeAddress( com.sun.star.sheet.XSpreadsheet xSheet, String aRange )273cdf0e10cSrcweir public com.sun.star.table.CellRangeAddress createCellRangeAddress( 274cdf0e10cSrcweir com.sun.star.sheet.XSpreadsheet xSheet, String aRange ) 275cdf0e10cSrcweir { 276cdf0e10cSrcweir com.sun.star.sheet.XCellRangeAddressable xAddr = (com.sun.star.sheet.XCellRangeAddressable) 277cdf0e10cSrcweir UnoRuntime.queryInterface( com.sun.star.sheet.XCellRangeAddressable.class, 278cdf0e10cSrcweir xSheet.getCellRangeByName( aRange ) ); 279cdf0e10cSrcweir return xAddr.getRangeAddress(); 280cdf0e10cSrcweir } 281cdf0e10cSrcweir 282cdf0e10cSrcweir // ________________________________________________________________ 283cdf0e10cSrcweir // Methods to convert cell addresses and range addresses to strings. 284cdf0e10cSrcweir 285cdf0e10cSrcweir /** Returns the text address of the cell. 286cdf0e10cSrcweir @param nColumn The column index. 287cdf0e10cSrcweir @param nRow The row index. 288cdf0e10cSrcweir @return A string containing the cell address. */ getCellAddressString( int nColumn, int nRow )289cdf0e10cSrcweir public String getCellAddressString( int nColumn, int nRow ) 290cdf0e10cSrcweir { 291cdf0e10cSrcweir String aStr = ""; 292cdf0e10cSrcweir if (nColumn > 25) 293cdf0e10cSrcweir aStr += (char) ('A' + nColumn / 26 - 1); 294cdf0e10cSrcweir aStr += (char) ('A' + nColumn % 26); 295cdf0e10cSrcweir aStr += (nRow + 1); 296cdf0e10cSrcweir return aStr; 297cdf0e10cSrcweir } 298cdf0e10cSrcweir 299cdf0e10cSrcweir /** Returns the text address of the cell range. 300cdf0e10cSrcweir @param aCellRange The cell range address. 301cdf0e10cSrcweir @return A string containing the cell range address. */ getCellRangeAddressString( com.sun.star.table.CellRangeAddress aCellRange )302cdf0e10cSrcweir public String getCellRangeAddressString( 303cdf0e10cSrcweir com.sun.star.table.CellRangeAddress aCellRange ) 304cdf0e10cSrcweir { 305cdf0e10cSrcweir return 306cdf0e10cSrcweir getCellAddressString( aCellRange.StartColumn, aCellRange.StartRow ) 307cdf0e10cSrcweir + ":" 308cdf0e10cSrcweir + getCellAddressString( aCellRange.EndColumn, aCellRange.EndRow ); 309cdf0e10cSrcweir } 310cdf0e10cSrcweir 311cdf0e10cSrcweir /** Returns the text address of the cell range. 312cdf0e10cSrcweir @param xCellRange The XSheetCellRange interface of the cell range. 313cdf0e10cSrcweir @param bWithSheet true = Include sheet name. 314cdf0e10cSrcweir @return A string containing the cell range address. */ getCellRangeAddressString( com.sun.star.sheet.XSheetCellRange xCellRange, boolean bWithSheet )315cdf0e10cSrcweir public String getCellRangeAddressString( 316cdf0e10cSrcweir com.sun.star.sheet.XSheetCellRange xCellRange, 317cdf0e10cSrcweir boolean bWithSheet ) 318cdf0e10cSrcweir { 319cdf0e10cSrcweir String aStr = ""; 320cdf0e10cSrcweir if (bWithSheet) 321cdf0e10cSrcweir { 322cdf0e10cSrcweir com.sun.star.sheet.XSpreadsheet xSheet = xCellRange.getSpreadsheet(); 323cdf0e10cSrcweir com.sun.star.container.XNamed xNamed = (com.sun.star.container.XNamed) 324cdf0e10cSrcweir UnoRuntime.queryInterface( com.sun.star.container.XNamed.class, xSheet ); 325cdf0e10cSrcweir aStr += xNamed.getName() + "."; 326cdf0e10cSrcweir } 327cdf0e10cSrcweir com.sun.star.sheet.XCellRangeAddressable xAddr = (com.sun.star.sheet.XCellRangeAddressable) 328cdf0e10cSrcweir UnoRuntime.queryInterface( com.sun.star.sheet.XCellRangeAddressable.class, xCellRange ); 329cdf0e10cSrcweir aStr += getCellRangeAddressString( xAddr.getRangeAddress() ); 330cdf0e10cSrcweir return aStr; 331cdf0e10cSrcweir } 332cdf0e10cSrcweir 333cdf0e10cSrcweir /** Returns a list of addresses of all cell ranges contained in the collection. 334cdf0e10cSrcweir @param xRangesIA The XIndexAccess interface of the collection. 335cdf0e10cSrcweir @return A string containing the cell range address list. */ getCellRangeListString( com.sun.star.container.XIndexAccess xRangesIA )336cdf0e10cSrcweir public String getCellRangeListString( 337cdf0e10cSrcweir com.sun.star.container.XIndexAccess xRangesIA ) throws RuntimeException, Exception 338cdf0e10cSrcweir { 339cdf0e10cSrcweir String aStr = ""; 340cdf0e10cSrcweir int nCount = xRangesIA.getCount(); 341cdf0e10cSrcweir for (int nIndex = 0; nIndex < nCount; ++nIndex) 342cdf0e10cSrcweir { 343cdf0e10cSrcweir if (nIndex > 0) 344cdf0e10cSrcweir aStr += " "; 345cdf0e10cSrcweir Object aRangeObj = xRangesIA.getByIndex( nIndex ); 346cdf0e10cSrcweir com.sun.star.sheet.XSheetCellRange xCellRange = (com.sun.star.sheet.XSheetCellRange) 347cdf0e10cSrcweir UnoRuntime.queryInterface( com.sun.star.sheet.XSheetCellRange.class, aRangeObj ); 348cdf0e10cSrcweir aStr += getCellRangeAddressString( xCellRange, false ); 349cdf0e10cSrcweir } 350cdf0e10cSrcweir return aStr; 351cdf0e10cSrcweir } 352cdf0e10cSrcweir 353cdf0e10cSrcweir // ________________________________________________________________ 354cdf0e10cSrcweir 355cdf0e10cSrcweir // Connect to a running office that is accepting connections. connect()356cdf0e10cSrcweir private void connect() 357cdf0e10cSrcweir { 358cdf0e10cSrcweir if (mxRemoteContext == null && mxRemoteServiceManager == null) { 359cdf0e10cSrcweir try { 360cdf0e10cSrcweir // First step: get the remote office component context 361cdf0e10cSrcweir mxRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); 362cdf0e10cSrcweir System.out.println("Connected to a running office ..."); 363cdf0e10cSrcweir 364cdf0e10cSrcweir mxRemoteServiceManager = mxRemoteContext.getServiceManager(); 365cdf0e10cSrcweir } 366cdf0e10cSrcweir catch( Exception e) { 367cdf0e10cSrcweir System.err.println("ERROR: can't get a component context from a running office ..."); 368cdf0e10cSrcweir e.printStackTrace(); 369cdf0e10cSrcweir System.exit(1); 370cdf0e10cSrcweir } 371cdf0e10cSrcweir } 372cdf0e10cSrcweir } 373cdf0e10cSrcweir 374cdf0e10cSrcweir /** Creates an empty spreadsheet document. 375cdf0e10cSrcweir @return The XSpreadsheetDocument interface of the document. */ initDocument()376cdf0e10cSrcweir private com.sun.star.sheet.XSpreadsheetDocument initDocument() 377cdf0e10cSrcweir throws RuntimeException, Exception 378cdf0e10cSrcweir { 379cdf0e10cSrcweir XComponentLoader aLoader = (XComponentLoader) 380cdf0e10cSrcweir UnoRuntime.queryInterface( 381cdf0e10cSrcweir XComponentLoader.class, 382cdf0e10cSrcweir mxRemoteServiceManager.createInstanceWithContext( 383cdf0e10cSrcweir "com.sun.star.frame.Desktop", mxRemoteContext)); 384cdf0e10cSrcweir 385cdf0e10cSrcweir XComponent xComponent = aLoader.loadComponentFromURL( 386cdf0e10cSrcweir "private:factory/scalc", "_blank", 0, 387cdf0e10cSrcweir new com.sun.star.beans.PropertyValue[0] ); 388cdf0e10cSrcweir 389cdf0e10cSrcweir return (com.sun.star.sheet.XSpreadsheetDocument)UnoRuntime.queryInterface( 390cdf0e10cSrcweir com.sun.star.sheet.XSpreadsheetDocument.class, xComponent ); 391cdf0e10cSrcweir } 392cdf0e10cSrcweir 393cdf0e10cSrcweir // ________________________________________________________________ 394cdf0e10cSrcweir } 395