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