1*34dd1e25SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 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 10cdf0e10cSrcweir * 11*34dd1e25SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 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. 19cdf0e10cSrcweir * 20*34dd1e25SAndrew Rist *************************************************************/ 21*34dd1e25SAndrew Rist 22*34dd1e25SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // __________ Imports __________ 25cdf0e10cSrcweir 26cdf0e10cSrcweir import com.sun.star.beans.PropertyValue; 27cdf0e10cSrcweir import com.sun.star.container.XIndexAccess; 28cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader; 29cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory; 30cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheet; 31cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheets; 32cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument; 33cdf0e10cSrcweir import com.sun.star.table.XCell; 34cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 35cdf0e10cSrcweir import com.sun.star.uno.XComponentContext; 36cdf0e10cSrcweir 37cdf0e10cSrcweir 38cdf0e10cSrcweir // __________ Implementation __________ 39cdf0e10cSrcweir 40cdf0e10cSrcweir /** Create a spreadsheet document and provide access to a sheet framework that 41cdf0e10cSrcweir is then used to modify some number formats. 42cdf0e10cSrcweir @author Eike Rathke 43cdf0e10cSrcweir */ 44cdf0e10cSrcweir public class Number_Formats 45cdf0e10cSrcweir { 46cdf0e10cSrcweir // __________ public members and methods __________ 47cdf0e10cSrcweir 48cdf0e10cSrcweir 49cdf0e10cSrcweir // ____________________ 50cdf0e10cSrcweir main( String args[] )51cdf0e10cSrcweir public static void main( String args[] ) 52cdf0e10cSrcweir { 53cdf0e10cSrcweir try 54cdf0e10cSrcweir { 55cdf0e10cSrcweir Number_Formats aSample = new Number_Formats( args ); 56cdf0e10cSrcweir aSample.doFunction(); 57cdf0e10cSrcweir } 58cdf0e10cSrcweir catch( Exception ex ) 59cdf0e10cSrcweir { 60cdf0e10cSrcweir System.err.println( "Sample caught exception! " + ex ); 61cdf0e10cSrcweir ex.printStackTrace(); 62cdf0e10cSrcweir System.exit(1); 63cdf0e10cSrcweir } 64cdf0e10cSrcweir 65cdf0e10cSrcweir System.out.println( "Sample done." ); 66cdf0e10cSrcweir System.exit(0); 67cdf0e10cSrcweir } 68cdf0e10cSrcweir 69cdf0e10cSrcweir // ____________________ 70cdf0e10cSrcweir doFunction()71cdf0e10cSrcweir public void doFunction() throws RuntimeException, Exception 72cdf0e10cSrcweir { 73cdf0e10cSrcweir // Assume: 74cdf0e10cSrcweir // com.sun.star.sheet.XSpreadsheetDocument maSpreadsheetDoc; 75cdf0e10cSrcweir // com.sun.star.sheet.XSpreadsheet maSheet; 76cdf0e10cSrcweir 77cdf0e10cSrcweir // Query the number formats supplier of the spreadsheet document 78cdf0e10cSrcweir com.sun.star.util.XNumberFormatsSupplier xNumberFormatsSupplier = 79cdf0e10cSrcweir (com.sun.star.util.XNumberFormatsSupplier) 80cdf0e10cSrcweir UnoRuntime.queryInterface( 81cdf0e10cSrcweir com.sun.star.util.XNumberFormatsSupplier.class, maSpreadsheetDoc ); 82cdf0e10cSrcweir 83cdf0e10cSrcweir // Get the number formats from the supplier 84cdf0e10cSrcweir com.sun.star.util.XNumberFormats xNumberFormats = 85cdf0e10cSrcweir xNumberFormatsSupplier.getNumberFormats(); 86cdf0e10cSrcweir 87cdf0e10cSrcweir // Query the XNumberFormatTypes interface 88cdf0e10cSrcweir com.sun.star.util.XNumberFormatTypes xNumberFormatTypes = 89cdf0e10cSrcweir (com.sun.star.util.XNumberFormatTypes) 90cdf0e10cSrcweir UnoRuntime.queryInterface( 91cdf0e10cSrcweir com.sun.star.util.XNumberFormatTypes.class, xNumberFormats ); 92cdf0e10cSrcweir 93cdf0e10cSrcweir // Get the number format index key of the default currency format, 94cdf0e10cSrcweir // note the empty locale for default locale 95cdf0e10cSrcweir com.sun.star.lang.Locale aLocale = new com.sun.star.lang.Locale(); 96cdf0e10cSrcweir int nCurrencyKey = xNumberFormatTypes.getStandardFormat( 97cdf0e10cSrcweir com.sun.star.util.NumberFormat.CURRENCY, aLocale ); 98cdf0e10cSrcweir 99cdf0e10cSrcweir // Get cell range B3:B11 100cdf0e10cSrcweir com.sun.star.table.XCellRange xCellRange = 101cdf0e10cSrcweir maSheet.getCellRangeByPosition( 1, 2, 1, 10 ); 102cdf0e10cSrcweir 103cdf0e10cSrcweir // Query the property set of the cell range 104cdf0e10cSrcweir com.sun.star.beans.XPropertySet xCellProp = 105cdf0e10cSrcweir (com.sun.star.beans.XPropertySet) 106cdf0e10cSrcweir UnoRuntime.queryInterface( 107cdf0e10cSrcweir com.sun.star.beans.XPropertySet.class, xCellRange ); 108cdf0e10cSrcweir 109cdf0e10cSrcweir // Set number format to default currency 110cdf0e10cSrcweir xCellProp.setPropertyValue( "NumberFormat", new Integer(nCurrencyKey) ); 111cdf0e10cSrcweir 112cdf0e10cSrcweir // Get cell B3 113cdf0e10cSrcweir com.sun.star.table.XCell xCell = maSheet.getCellByPosition( 1, 2 ); 114cdf0e10cSrcweir 115cdf0e10cSrcweir // Query the property set of the cell 116cdf0e10cSrcweir xCellProp = (com.sun.star.beans.XPropertySet) 117cdf0e10cSrcweir UnoRuntime.queryInterface( 118cdf0e10cSrcweir com.sun.star.beans.XPropertySet.class, xCell ); 119cdf0e10cSrcweir 120cdf0e10cSrcweir // Get the number format index key of the cell's properties 121cdf0e10cSrcweir int nIndexKey = ((Integer) xCellProp.getPropertyValue( "NumberFormat" )).intValue(); 122cdf0e10cSrcweir if ( nIndexKey != nCurrencyKey ) 123cdf0e10cSrcweir System.out.println( "Number format doesn't match!" ); 124cdf0e10cSrcweir 125cdf0e10cSrcweir // Get the properties of the number format 126cdf0e10cSrcweir com.sun.star.beans.XPropertySet xProp = xNumberFormats.getByKey( nIndexKey ); 127cdf0e10cSrcweir 128cdf0e10cSrcweir // Get the format code string of the number format's properties 129cdf0e10cSrcweir String aFormatCode = (String) xProp.getPropertyValue( "FormatString" ); 130cdf0e10cSrcweir System.out.println( "FormatString: `" + aFormatCode + "'" ); 131cdf0e10cSrcweir 132cdf0e10cSrcweir // Create an arbitrary format code 133cdf0e10cSrcweir aFormatCode = "\"wonderful \"" + aFormatCode; 134cdf0e10cSrcweir 135cdf0e10cSrcweir // Test if it's already present 136cdf0e10cSrcweir nIndexKey = xNumberFormats.queryKey( aFormatCode, aLocale, false ); 137cdf0e10cSrcweir 138cdf0e10cSrcweir // If not, add to number formats collection 139cdf0e10cSrcweir if ( nIndexKey == -1 ) 140cdf0e10cSrcweir { 141cdf0e10cSrcweir try 142cdf0e10cSrcweir { 143cdf0e10cSrcweir nIndexKey = xNumberFormats.addNew( aFormatCode, aLocale ); 144cdf0e10cSrcweir } 145cdf0e10cSrcweir catch( com.sun.star.util.MalformedNumberFormatException ex ) 146cdf0e10cSrcweir { 147cdf0e10cSrcweir System.err.println( "Bad number format code: " + ex ); 148cdf0e10cSrcweir ex.printStackTrace(); 149cdf0e10cSrcweir nIndexKey = -1; 150cdf0e10cSrcweir } 151cdf0e10cSrcweir } 152cdf0e10cSrcweir 153cdf0e10cSrcweir // Set the new format at the cell 154cdf0e10cSrcweir if ( nIndexKey != -1 ) 155cdf0e10cSrcweir xCellProp.setPropertyValue( "NumberFormat", new Integer(nIndexKey) ); 156cdf0e10cSrcweir 157cdf0e10cSrcweir 158cdf0e10cSrcweir // Set column containing the example values to optimal width to show 159cdf0e10cSrcweir // the new format of cell B3 160cdf0e10cSrcweir com.sun.star.table.XColumnRowRange xColRowRange = 161cdf0e10cSrcweir (com.sun.star.table.XColumnRowRange) 162cdf0e10cSrcweir UnoRuntime.queryInterface(com.sun.star.table.XColumnRowRange.class, 163cdf0e10cSrcweir maSheet); 164cdf0e10cSrcweir 165cdf0e10cSrcweir com.sun.star.container.XIndexAccess xIndexAccess = 166cdf0e10cSrcweir (com.sun.star.container.XIndexAccess) 167cdf0e10cSrcweir UnoRuntime.queryInterface(com.sun.star.container.XIndexAccess.class, 168cdf0e10cSrcweir xColRowRange.getColumns()); 169cdf0e10cSrcweir 170cdf0e10cSrcweir com.sun.star.beans.XPropertySet xColPropSet = 171cdf0e10cSrcweir (com.sun.star.beans.XPropertySet) 172cdf0e10cSrcweir UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, 173cdf0e10cSrcweir xIndexAccess.getByIndex(1)); 174cdf0e10cSrcweir 175cdf0e10cSrcweir xColPropSet.setPropertyValue( "OptimalWidth", new Boolean(true) ); 176cdf0e10cSrcweir } 177cdf0e10cSrcweir 178cdf0e10cSrcweir // ____________________ 179cdf0e10cSrcweir Number_Formats( String[] args )180cdf0e10cSrcweir public Number_Formats( String[] args ) throws java.lang.Exception 181cdf0e10cSrcweir { 182cdf0e10cSrcweir // get the remote office context. If necessary a new office 183cdf0e10cSrcweir // process is started 184cdf0e10cSrcweir maOfficeContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); 185cdf0e10cSrcweir System.out.println("Connected to a running office ..."); 186cdf0e10cSrcweir maServiceManager = maOfficeContext.getServiceManager(); 187cdf0e10cSrcweir 188cdf0e10cSrcweir // create a new spreadsheet document 189cdf0e10cSrcweir XComponentLoader aLoader = (XComponentLoader) UnoRuntime.queryInterface( 190cdf0e10cSrcweir XComponentLoader.class, maServiceManager.createInstanceWithContext( 191cdf0e10cSrcweir "com.sun.star.frame.Desktop", maOfficeContext) ); 192cdf0e10cSrcweir 193cdf0e10cSrcweir maSpreadsheetDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface( 194cdf0e10cSrcweir XSpreadsheetDocument.class, 195cdf0e10cSrcweir aLoader.loadComponentFromURL( "private:factory/scalc", 196cdf0e10cSrcweir "_blank", 197cdf0e10cSrcweir 0, 198cdf0e10cSrcweir new PropertyValue[ 0 ] ) ); 199cdf0e10cSrcweir 200cdf0e10cSrcweir if ( !initSpreadsheet() ) 201cdf0e10cSrcweir System.exit( 0 ); 202cdf0e10cSrcweir } 203cdf0e10cSrcweir 204cdf0e10cSrcweir 205cdf0e10cSrcweir // __________ private members and methods __________ 206cdf0e10cSrcweir private final String msDataSheetName = "Data"; 207cdf0e10cSrcweir 208cdf0e10cSrcweir private XComponentContext maOfficeContext; 209cdf0e10cSrcweir private XMultiComponentFactory maServiceManager; 210cdf0e10cSrcweir private XSpreadsheetDocument maSpreadsheetDoc; 211cdf0e10cSrcweir private XSpreadsheet maSheet; // the first sheet 212cdf0e10cSrcweir 213cdf0e10cSrcweir 214cdf0e10cSrcweir // ____________________ 215cdf0e10cSrcweir 216cdf0e10cSrcweir /** init the first sheet 217cdf0e10cSrcweir */ initSpreadsheet()218cdf0e10cSrcweir private boolean initSpreadsheet() 219cdf0e10cSrcweir { 220cdf0e10cSrcweir boolean bOk = true; 221cdf0e10cSrcweir XSpreadsheets aSheets = maSpreadsheetDoc.getSheets(); 222cdf0e10cSrcweir try 223cdf0e10cSrcweir { 224cdf0e10cSrcweir XIndexAccess aSheetsIA = (XIndexAccess) UnoRuntime.queryInterface( XIndexAccess.class, aSheets ); 225cdf0e10cSrcweir maSheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, aSheetsIA.getByIndex( 0 )); 226cdf0e10cSrcweir 227cdf0e10cSrcweir // enter some values in B3:B11 228cdf0e10cSrcweir for( int iCounter=1; iCounter < 10; iCounter++ ) 229cdf0e10cSrcweir { 230cdf0e10cSrcweir XCell aCell = maSheet.getCellByPosition( 1, 1 + iCounter ); 231cdf0e10cSrcweir aCell.setValue( (double) iCounter ); 232cdf0e10cSrcweir } 233cdf0e10cSrcweir } 234cdf0e10cSrcweir catch( Exception ex ) 235cdf0e10cSrcweir { 236cdf0e10cSrcweir System.err.println( "Couldn't initialize Spreadsheet Document: " + ex ); 237cdf0e10cSrcweir ex.printStackTrace(); 238cdf0e10cSrcweir bOk = false; 239cdf0e10cSrcweir } 240cdf0e10cSrcweir return bOk; 241cdf0e10cSrcweir } 242cdf0e10cSrcweir } 243