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.uno.UnoRuntime; 25cdf0e10cSrcweir import com.sun.star.uno.RuntimeException; 26cdf0e10cSrcweir 27cdf0e10cSrcweir 28cdf0e10cSrcweir // __________ implementation ____________________________________ 29cdf0e10cSrcweir 30cdf0e10cSrcweir /** Create a spreadsheet document and provide access to table contents. 31cdf0e10cSrcweir */ 32cdf0e10cSrcweir public class GeneralTableSample extends SpreadsheetDocHelper 33cdf0e10cSrcweir { 34cdf0e10cSrcweir 35cdf0e10cSrcweir // ________________________________________________________________ 36cdf0e10cSrcweir main( String args[] )37cdf0e10cSrcweir public static void main( String args[] ) 38cdf0e10cSrcweir { 39cdf0e10cSrcweir try 40cdf0e10cSrcweir { 41cdf0e10cSrcweir GeneralTableSample aSample = new GeneralTableSample( args ); 42cdf0e10cSrcweir aSample.doSampleFunction(); 43cdf0e10cSrcweir } 44cdf0e10cSrcweir catch (Exception ex) 45cdf0e10cSrcweir { 46cdf0e10cSrcweir System.out.println( "Error: Sample caught exception!\nException Message = " 47cdf0e10cSrcweir + ex.getMessage()); 48cdf0e10cSrcweir ex.printStackTrace(); 49cdf0e10cSrcweir System.exit( 1 ); 50cdf0e10cSrcweir } 51cdf0e10cSrcweir 52cdf0e10cSrcweir System.out.println( "Sample done." ); 53cdf0e10cSrcweir System.exit( 0 ); 54cdf0e10cSrcweir } 55cdf0e10cSrcweir 56cdf0e10cSrcweir // ________________________________________________________________ 57cdf0e10cSrcweir 58cdf0e10cSrcweir /// This sample function modifies cells and cell ranges. doSampleFunction()59cdf0e10cSrcweir public void doSampleFunction() throws RuntimeException, Exception 60cdf0e10cSrcweir { 61cdf0e10cSrcweir // for common usage 62cdf0e10cSrcweir com.sun.star.sheet.XSpreadsheet xSheet = getSpreadsheet( 0 ); 63cdf0e10cSrcweir com.sun.star.beans.XPropertySet xPropSet = null; 64cdf0e10cSrcweir com.sun.star.table.XCell xCell = null; 65cdf0e10cSrcweir com.sun.star.table.XCellRange xCellRange = null; 66cdf0e10cSrcweir 67cdf0e10cSrcweir // *** Access and modify a VALUE CELL *** 68cdf0e10cSrcweir System.out.println( "*** Sample for service table.Cell ***" ); 69cdf0e10cSrcweir 70cdf0e10cSrcweir xCell = xSheet.getCellByPosition( 0, 0 ); 71cdf0e10cSrcweir // Set cell value. 72cdf0e10cSrcweir xCell.setValue( 1234 ); 73cdf0e10cSrcweir 74cdf0e10cSrcweir // Get cell value. 75cdf0e10cSrcweir double nDblValue = xCell.getValue() * 2; 76cdf0e10cSrcweir xSheet.getCellByPosition( 0, 1 ).setValue( nDblValue ); 77cdf0e10cSrcweir 78cdf0e10cSrcweir // *** Create a FORMULA CELL and query error type *** 79cdf0e10cSrcweir xCell = xSheet.getCellByPosition( 0, 2 ); 80cdf0e10cSrcweir // Set formula string. 81cdf0e10cSrcweir xCell.setFormula( "=1/0" ); 82cdf0e10cSrcweir 83cdf0e10cSrcweir // Get error type. 84cdf0e10cSrcweir boolean bValid = (xCell.getError() == 0); 85cdf0e10cSrcweir // Get formula string. 86cdf0e10cSrcweir String aText = "The formula " + xCell.getFormula() + " is "; 87cdf0e10cSrcweir aText += bValid ? "valid." : "erroneous."; 88cdf0e10cSrcweir 89cdf0e10cSrcweir // *** Insert a TEXT CELL using the XText interface *** 90cdf0e10cSrcweir xCell = xSheet.getCellByPosition( 0, 3 ); 91cdf0e10cSrcweir com.sun.star.text.XText xCellText = (com.sun.star.text.XText) 92cdf0e10cSrcweir UnoRuntime.queryInterface( com.sun.star.text.XText.class, xCell ); 93cdf0e10cSrcweir com.sun.star.text.XTextCursor xTextCursor = xCellText.createTextCursor(); 94cdf0e10cSrcweir xCellText.insertString( xTextCursor, aText, false ); 95cdf0e10cSrcweir 96cdf0e10cSrcweir // *** Change cell properties *** 97cdf0e10cSrcweir int nValue = bValid ? 0x00FF00 : 0xFF4040; 98cdf0e10cSrcweir xPropSet = (com.sun.star.beans.XPropertySet) 99cdf0e10cSrcweir UnoRuntime.queryInterface( com.sun.star.beans.XPropertySet.class, xCell ); 100cdf0e10cSrcweir xPropSet.setPropertyValue( "CellBackColor", new Integer( nValue ) ); 101cdf0e10cSrcweir 102cdf0e10cSrcweir 103cdf0e10cSrcweir // *** Accessing a CELL RANGE *** 104cdf0e10cSrcweir System.out.println( "*** Sample for service table.CellRange ***" ); 105cdf0e10cSrcweir 106cdf0e10cSrcweir // Accessing a cell range over its position. 107cdf0e10cSrcweir xCellRange = xSheet.getCellRangeByPosition( 2, 0, 3, 1 ); 108cdf0e10cSrcweir 109cdf0e10cSrcweir // Change properties of the range. 110cdf0e10cSrcweir xPropSet = (com.sun.star.beans.XPropertySet) 111cdf0e10cSrcweir UnoRuntime.queryInterface( com.sun.star.beans.XPropertySet.class, xCellRange ); 112cdf0e10cSrcweir xPropSet.setPropertyValue( "CellBackColor", new Integer( 0x8080FF ) ); 113cdf0e10cSrcweir 114cdf0e10cSrcweir // Accessing a cell range over its name. 115cdf0e10cSrcweir xCellRange = xSheet.getCellRangeByName( "C4:D5" ); 116cdf0e10cSrcweir 117cdf0e10cSrcweir // Change properties of the range. 118cdf0e10cSrcweir xPropSet = (com.sun.star.beans.XPropertySet) 119cdf0e10cSrcweir UnoRuntime.queryInterface( com.sun.star.beans.XPropertySet.class, xCellRange ); 120cdf0e10cSrcweir xPropSet.setPropertyValue( "CellBackColor", new Integer( 0xFFFF80 ) ); 121cdf0e10cSrcweir 122cdf0e10cSrcweir 123cdf0e10cSrcweir // *** Using the CELL CURSOR to add some data below of the filled area *** 124cdf0e10cSrcweir System.out.println( "*** Sample for service table.CellCursor ***" ); 125cdf0e10cSrcweir 126cdf0e10cSrcweir // Create a cursor using the XSpreadsheet method createCursorByRange() 127cdf0e10cSrcweir xCellRange = xSheet.getCellRangeByName( "A1" ); 128cdf0e10cSrcweir com.sun.star.sheet.XSheetCellRange xSheetCellRange = (com.sun.star.sheet.XSheetCellRange) 129cdf0e10cSrcweir UnoRuntime.queryInterface( com.sun.star.sheet.XSheetCellRange.class, xCellRange ); 130cdf0e10cSrcweir 131cdf0e10cSrcweir com.sun.star.sheet.XSheetCellCursor xSheetCellCursor = 132cdf0e10cSrcweir xSheet.createCursorByRange( xSheetCellRange ); 133cdf0e10cSrcweir com.sun.star.table.XCellCursor xCursor = (com.sun.star.table.XCellCursor) 134cdf0e10cSrcweir UnoRuntime.queryInterface( com.sun.star.table.XCellCursor.class, xSheetCellCursor ); 135cdf0e10cSrcweir 136cdf0e10cSrcweir // Move to the last filled cell. 137cdf0e10cSrcweir xCursor.gotoEnd(); 138cdf0e10cSrcweir // Move one row down. 139cdf0e10cSrcweir xCursor.gotoOffset( 0, 1 ); 140cdf0e10cSrcweir xCursor.getCellByPosition( 0, 0 ).setFormula( "Beyond of the last filled cell." ); 141cdf0e10cSrcweir 142cdf0e10cSrcweir 143cdf0e10cSrcweir // *** Modifying COLUMNS and ROWS *** 144cdf0e10cSrcweir System.out.println( "*** Sample for services table.TableRows and table.TableColumns ***" ); 145cdf0e10cSrcweir 146cdf0e10cSrcweir com.sun.star.table.XColumnRowRange xCRRange = (com.sun.star.table.XColumnRowRange) 147cdf0e10cSrcweir UnoRuntime.queryInterface( com.sun.star.table.XColumnRowRange.class, xSheet ); 148cdf0e10cSrcweir com.sun.star.table.XTableColumns xColumns = xCRRange.getColumns(); 149cdf0e10cSrcweir com.sun.star.table.XTableRows xRows = xCRRange.getRows(); 150cdf0e10cSrcweir 151cdf0e10cSrcweir // Get column C by index (interface XIndexAccess). 152cdf0e10cSrcweir Object aColumnObj = xColumns.getByIndex( 2 ); 153cdf0e10cSrcweir xPropSet = (com.sun.star.beans.XPropertySet) 154cdf0e10cSrcweir UnoRuntime.queryInterface( com.sun.star.beans.XPropertySet.class, aColumnObj ); 155cdf0e10cSrcweir xPropSet.setPropertyValue( "Width", new Integer( 5000 ) ); 156cdf0e10cSrcweir 157cdf0e10cSrcweir // Get the name of the column. 158cdf0e10cSrcweir com.sun.star.container.XNamed xNamed = (com.sun.star.container.XNamed) 159cdf0e10cSrcweir UnoRuntime.queryInterface( com.sun.star.container.XNamed.class, aColumnObj ); 160cdf0e10cSrcweir aText = "The name of this column is " + xNamed.getName() + "."; 161cdf0e10cSrcweir xSheet.getCellByPosition( 2, 2 ).setFormula( aText ); 162cdf0e10cSrcweir 163cdf0e10cSrcweir // Get column D by name (interface XNameAccess). 164cdf0e10cSrcweir com.sun.star.container.XNameAccess xColumnsName = (com.sun.star.container.XNameAccess) 165cdf0e10cSrcweir UnoRuntime.queryInterface( com.sun.star.container.XNameAccess.class, xColumns ); 166cdf0e10cSrcweir 167cdf0e10cSrcweir aColumnObj = xColumnsName.getByName( "D" ); 168cdf0e10cSrcweir xPropSet = (com.sun.star.beans.XPropertySet) 169cdf0e10cSrcweir UnoRuntime.queryInterface( com.sun.star.beans.XPropertySet.class, aColumnObj ); 170cdf0e10cSrcweir xPropSet.setPropertyValue( "IsVisible", new Boolean( false ) ); 171cdf0e10cSrcweir 172cdf0e10cSrcweir // Get row 7 by index (interface XIndexAccess) 173cdf0e10cSrcweir Object aRowObj = xRows.getByIndex( 6 ); 174cdf0e10cSrcweir xPropSet = (com.sun.star.beans.XPropertySet) 175cdf0e10cSrcweir UnoRuntime.queryInterface( com.sun.star.beans.XPropertySet.class, aRowObj ); 176cdf0e10cSrcweir xPropSet.setPropertyValue( "Height", new Integer( 5000 ) ); 177cdf0e10cSrcweir 178cdf0e10cSrcweir xSheet.getCellByPosition( 2, 6 ).setFormula( "What a big cell." ); 179cdf0e10cSrcweir 180cdf0e10cSrcweir // Create a cell series with the values 1 ... 7. 181cdf0e10cSrcweir for (int nRow = 8; nRow < 15; ++nRow) 182cdf0e10cSrcweir xSheet.getCellByPosition( 0, nRow ).setValue( nRow - 7 ); 183cdf0e10cSrcweir // Insert a row between 1 and 2 184cdf0e10cSrcweir xRows.insertByIndex( 9, 1 ); 185cdf0e10cSrcweir // Delete the rows with the values 3 and 4. 186cdf0e10cSrcweir xRows.removeByIndex( 11, 2 ); 187cdf0e10cSrcweir 188cdf0e10cSrcweir // *** Inserting CHARTS *** 189cdf0e10cSrcweir System.out.println( "*** Sample for service table.TableCharts ***" ); 190cdf0e10cSrcweir 191cdf0e10cSrcweir com.sun.star.table.XTableChartsSupplier xChartsSupp = 192cdf0e10cSrcweir (com.sun.star.table.XTableChartsSupplier) UnoRuntime.queryInterface( 193cdf0e10cSrcweir com.sun.star.table.XTableChartsSupplier.class, xSheet ); 194cdf0e10cSrcweir com.sun.star.table.XTableCharts xCharts = xChartsSupp.getCharts(); 195cdf0e10cSrcweir 196cdf0e10cSrcweir // The chart will base on the last cell series, initializing all values. 197cdf0e10cSrcweir String aName = "newChart"; 198cdf0e10cSrcweir com.sun.star.awt.Rectangle aRect = new com.sun.star.awt.Rectangle(); 199cdf0e10cSrcweir aRect.X = 10000; 200cdf0e10cSrcweir aRect.Y = 3000; 201cdf0e10cSrcweir aRect.Width = aRect.Height = 5000; 202cdf0e10cSrcweir com.sun.star.table.CellRangeAddress[] aRanges = new com.sun.star.table.CellRangeAddress[1]; 203cdf0e10cSrcweir aRanges[0] = createCellRangeAddress( xSheet, "A9:A14" ); 204cdf0e10cSrcweir 205cdf0e10cSrcweir // Create the chart. 206cdf0e10cSrcweir xCharts.addNewByName( aName, aRect, aRanges, false, false ); 207cdf0e10cSrcweir 208cdf0e10cSrcweir // Get the chart by name. 209cdf0e10cSrcweir Object aChartObj = xCharts.getByName( aName ); 210cdf0e10cSrcweir com.sun.star.table.XTableChart xChart = (com.sun.star.table.XTableChart) 211cdf0e10cSrcweir UnoRuntime.queryInterface( com.sun.star.table.XTableChart.class, aChartObj ); 212cdf0e10cSrcweir 213cdf0e10cSrcweir // Query the state of row and column headers. 214cdf0e10cSrcweir aText = "Chart has column headers: "; 215cdf0e10cSrcweir aText += xChart.getHasColumnHeaders() ? "yes" : "no"; 216cdf0e10cSrcweir xSheet.getCellByPosition( 2, 8 ).setFormula( aText ); 217cdf0e10cSrcweir aText = "Chart has row headers: "; 218cdf0e10cSrcweir aText += xChart.getHasRowHeaders() ? "yes" : "no"; 219cdf0e10cSrcweir xSheet.getCellByPosition( 2, 9 ).setFormula( aText ); 220cdf0e10cSrcweir } 221cdf0e10cSrcweir 222cdf0e10cSrcweir // ________________________________________________________________ 223cdf0e10cSrcweir GeneralTableSample( String[] args )224cdf0e10cSrcweir public GeneralTableSample( String[] args ) 225cdf0e10cSrcweir { 226cdf0e10cSrcweir super( args ); 227cdf0e10cSrcweir } 228cdf0e10cSrcweir 229cdf0e10cSrcweir // ________________________________________________________________ 230cdf0e10cSrcweir } 231