1 /*************************************************************************
3 * $RCSfile: GeneralTableSample.java,v $
7 * last change: $Author: hr $ $Date: 2003-06-30 15:45:36 $
9 * The Contents of this file are made available subject to the terms of
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 import com
.sun
.star
.uno
.UnoRuntime
;
42 import com
.sun
.star
.uno
.RuntimeException
;
45 // __________ implementation ____________________________________
47 /** Create a spreadsheet document and provide access to table contents.
49 public class GeneralTableSample
extends SpreadsheetDocHelper
52 // ________________________________________________________________
54 public static void main( String args
[] )
58 GeneralTableSample aSample
= new GeneralTableSample( args
);
59 aSample
.doSampleFunction();
63 System
.out
.println( "Error: Sample caught exception!\nException Message = "
69 System
.out
.println( "Sample done." );
73 // ________________________________________________________________
75 /// This sample function modifies cells and cell ranges.
76 public void doSampleFunction() throws RuntimeException
, Exception
79 com
.sun
.star
.sheet
.XSpreadsheet xSheet
= getSpreadsheet( 0 );
80 com
.sun
.star
.beans
.XPropertySet xPropSet
= null;
81 com
.sun
.star
.table
.XCell xCell
= null;
82 com
.sun
.star
.table
.XCellRange xCellRange
= null;
84 // *** Access and modify a VALUE CELL ***
85 System
.out
.println( "*** Sample for service table.Cell ***" );
87 xCell
= xSheet
.getCellByPosition( 0, 0 );
89 xCell
.setValue( 1234 );
92 double nDblValue
= xCell
.getValue() * 2;
93 xSheet
.getCellByPosition( 0, 1 ).setValue( nDblValue
);
95 // *** Create a FORMULA CELL and query error type ***
96 xCell
= xSheet
.getCellByPosition( 0, 2 );
97 // Set formula string.
98 xCell
.setFormula( "=1/0" );
101 boolean bValid
= (xCell
.getError() == 0);
102 // Get formula string.
103 String aText
= "The formula " + xCell
.getFormula() + " is ";
104 aText
+= bValid ?
"valid." : "erroneous.";
106 // *** Insert a TEXT CELL using the XText interface ***
107 xCell
= xSheet
.getCellByPosition( 0, 3 );
108 com
.sun
.star
.text
.XText xCellText
= (com
.sun
.star
.text
.XText
)
109 UnoRuntime
.queryInterface( com
.sun
.star
.text
.XText
.class, xCell
);
110 com
.sun
.star
.text
.XTextCursor xTextCursor
= xCellText
.createTextCursor();
111 xCellText
.insertString( xTextCursor
, aText
, false );
113 // *** Change cell properties ***
114 int nValue
= bValid ?
0x00FF00 : 0xFF4040;
115 xPropSet
= (com
.sun
.star
.beans
.XPropertySet
)
116 UnoRuntime
.queryInterface( com
.sun
.star
.beans
.XPropertySet
.class, xCell
);
117 xPropSet
.setPropertyValue( "CellBackColor", new Integer( nValue
) );
120 // *** Accessing a CELL RANGE ***
121 System
.out
.println( "*** Sample for service table.CellRange ***" );
123 // Accessing a cell range over its position.
124 xCellRange
= xSheet
.getCellRangeByPosition( 2, 0, 3, 1 );
126 // Change properties of the range.
127 xPropSet
= (com
.sun
.star
.beans
.XPropertySet
)
128 UnoRuntime
.queryInterface( com
.sun
.star
.beans
.XPropertySet
.class, xCellRange
);
129 xPropSet
.setPropertyValue( "CellBackColor", new Integer( 0x8080FF ) );
131 // Accessing a cell range over its name.
132 xCellRange
= xSheet
.getCellRangeByName( "C4:D5" );
134 // Change properties of the range.
135 xPropSet
= (com
.sun
.star
.beans
.XPropertySet
)
136 UnoRuntime
.queryInterface( com
.sun
.star
.beans
.XPropertySet
.class, xCellRange
);
137 xPropSet
.setPropertyValue( "CellBackColor", new Integer( 0xFFFF80 ) );
140 // *** Using the CELL CURSOR to add some data below of the filled area ***
141 System
.out
.println( "*** Sample for service table.CellCursor ***" );
143 // Create a cursor using the XSpreadsheet method createCursorByRange()
144 xCellRange
= xSheet
.getCellRangeByName( "A1" );
145 com
.sun
.star
.sheet
.XSheetCellRange xSheetCellRange
= (com
.sun
.star
.sheet
.XSheetCellRange
)
146 UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XSheetCellRange
.class, xCellRange
);
148 com
.sun
.star
.sheet
.XSheetCellCursor xSheetCellCursor
=
149 xSheet
.createCursorByRange( xSheetCellRange
);
150 com
.sun
.star
.table
.XCellCursor xCursor
= (com
.sun
.star
.table
.XCellCursor
)
151 UnoRuntime
.queryInterface( com
.sun
.star
.table
.XCellCursor
.class, xSheetCellCursor
);
153 // Move to the last filled cell.
155 // Move one row down.
156 xCursor
.gotoOffset( 0, 1 );
157 xCursor
.getCellByPosition( 0, 0 ).setFormula( "Beyond of the last filled cell." );
160 // *** Modifying COLUMNS and ROWS ***
161 System
.out
.println( "*** Sample for services table.TableRows and table.TableColumns ***" );
163 com
.sun
.star
.table
.XColumnRowRange xCRRange
= (com
.sun
.star
.table
.XColumnRowRange
)
164 UnoRuntime
.queryInterface( com
.sun
.star
.table
.XColumnRowRange
.class, xSheet
);
165 com
.sun
.star
.table
.XTableColumns xColumns
= xCRRange
.getColumns();
166 com
.sun
.star
.table
.XTableRows xRows
= xCRRange
.getRows();
168 // Get column C by index (interface XIndexAccess).
169 Object aColumnObj
= xColumns
.getByIndex( 2 );
170 xPropSet
= (com
.sun
.star
.beans
.XPropertySet
)
171 UnoRuntime
.queryInterface( com
.sun
.star
.beans
.XPropertySet
.class, aColumnObj
);
172 xPropSet
.setPropertyValue( "Width", new Integer( 5000 ) );
174 // Get the name of the column.
175 com
.sun
.star
.container
.XNamed xNamed
= (com
.sun
.star
.container
.XNamed
)
176 UnoRuntime
.queryInterface( com
.sun
.star
.container
.XNamed
.class, aColumnObj
);
177 aText
= "The name of this column is " + xNamed
.getName() + ".";
178 xSheet
.getCellByPosition( 2, 2 ).setFormula( aText
);
180 // Get column D by name (interface XNameAccess).
181 com
.sun
.star
.container
.XNameAccess xColumnsName
= (com
.sun
.star
.container
.XNameAccess
)
182 UnoRuntime
.queryInterface( com
.sun
.star
.container
.XNameAccess
.class, xColumns
);
184 aColumnObj
= xColumnsName
.getByName( "D" );
185 xPropSet
= (com
.sun
.star
.beans
.XPropertySet
)
186 UnoRuntime
.queryInterface( com
.sun
.star
.beans
.XPropertySet
.class, aColumnObj
);
187 xPropSet
.setPropertyValue( "IsVisible", new Boolean( false ) );
189 // Get row 7 by index (interface XIndexAccess)
190 Object aRowObj
= xRows
.getByIndex( 6 );
191 xPropSet
= (com
.sun
.star
.beans
.XPropertySet
)
192 UnoRuntime
.queryInterface( com
.sun
.star
.beans
.XPropertySet
.class, aRowObj
);
193 xPropSet
.setPropertyValue( "Height", new Integer( 5000 ) );
195 xSheet
.getCellByPosition( 2, 6 ).setFormula( "What a big cell." );
197 // Create a cell series with the values 1 ... 7.
198 for (int nRow
= 8; nRow
< 15; ++nRow
)
199 xSheet
.getCellByPosition( 0, nRow
).setValue( nRow
- 7 );
200 // Insert a row between 1 and 2
201 xRows
.insertByIndex( 9, 1 );
202 // Delete the rows with the values 3 and 4.
203 xRows
.removeByIndex( 11, 2 );
205 // *** Inserting CHARTS ***
206 System
.out
.println( "*** Sample for service table.TableCharts ***" );
208 com
.sun
.star
.table
.XTableChartsSupplier xChartsSupp
=
209 (com
.sun
.star
.table
.XTableChartsSupplier
) UnoRuntime
.queryInterface(
210 com
.sun
.star
.table
.XTableChartsSupplier
.class, xSheet
);
211 com
.sun
.star
.table
.XTableCharts xCharts
= xChartsSupp
.getCharts();
213 // The chart will base on the last cell series, initializing all values.
214 String aName
= "newChart";
215 com
.sun
.star
.awt
.Rectangle aRect
= new com
.sun
.star
.awt
.Rectangle();
218 aRect
.Width
= aRect
.Height
= 5000;
219 com
.sun
.star
.table
.CellRangeAddress
[] aRanges
= new com
.sun
.star
.table
.CellRangeAddress
[1];
220 aRanges
[0] = createCellRangeAddress( xSheet
, "A9:A14" );
223 xCharts
.addNewByName( aName
, aRect
, aRanges
, false, false );
225 // Get the chart by name.
226 Object aChartObj
= xCharts
.getByName( aName
);
227 com
.sun
.star
.table
.XTableChart xChart
= (com
.sun
.star
.table
.XTableChart
)
228 UnoRuntime
.queryInterface( com
.sun
.star
.table
.XTableChart
.class, aChartObj
);
230 // Query the state of row and column headers.
231 aText
= "Chart has column headers: ";
232 aText
+= xChart
.getHasColumnHeaders() ?
"yes" : "no";
233 xSheet
.getCellByPosition( 2, 8 ).setFormula( aText
);
234 aText
= "Chart has row headers: ";
235 aText
+= xChart
.getHasRowHeaders() ?
"yes" : "no";
236 xSheet
.getCellByPosition( 2, 9 ).setFormula( aText
);
239 // ________________________________________________________________
241 public GeneralTableSample( String
[] args
)
246 // ________________________________________________________________