update dev300-m58
[ooovba.git] / qadevOOo / runner / util / CalcTools.java
blobf51f24c8284937434ee8777068438f4032ca3018
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: CalcTools.java,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************
32 package util;
34 import com.sun.star.container.XIndexAccess;
35 import com.sun.star.lang.IllegalArgumentException;
36 import com.sun.star.lang.IndexOutOfBoundsException;
37 import com.sun.star.lang.WrappedTargetException;
38 import com.sun.star.lang.XComponent;
39 import com.sun.star.sheet.XCellRangeData;
40 import com.sun.star.sheet.XSpreadsheet;
41 import com.sun.star.sheet.XSpreadsheetDocument;
42 import com.sun.star.sheet.XSpreadsheets;
43 import com.sun.star.table.XCellRange;
44 import com.sun.star.uno.AnyConverter;
45 import com.sun.star.uno.Exception;
46 import com.sun.star.uno.Type;
47 import com.sun.star.uno.UnoRuntime;
49 /**
50 * This class contains some usefull mathods to handle Calc documents
51 * and its sheets.
53 public class CalcTools {
55 /**
56 * fills a range of a calc sheet with computed data of type
57 * <CODE>Double</CODE>.
58 * @param xSheetDoc the Clac documents wich should be filled
59 * @param sheetNumber the number of the sheet of <CODE>xSheetDoc</CODE>
60 * @param startCellX the cell number of the X start point (row) of the range to fill
61 * @param startCellY the cell number of the Y start point (column) of the range to fill
62 * @param rangeLengthX the size of the range expansion in X-direction
63 * @param rangeLengthY the size of the range expansion in Y-direction
64 * @throws java.lang.Exception on any error an <CODE>java.lang.Exception</CODE> was thrown
66 public static void fillCalcSheetWithContent(XComponent xSheetDoc, int sheetNumber,
67 int startCellX, int startCellY, int rangeLengthX, int rangeLengthY)
68 throws java.lang.Exception {
69 try{
70 XSpreadsheet xSheet = getSpreadSheetFromSheetDoc(xSheetDoc, sheetNumber);
72 fillCalcSheetWithContent(xSheet, startCellX, startCellY, rangeLengthX, rangeLengthY);
74 } catch (Exception e){
75 throw new Exception(
76 "Couldn't fill CalcSheet with content: " + e.toString());
80 /**
81 * fills a range of a calc sheet with computed data of type
82 * <CODE>Double</CODE>.
83 * @param xSheet the sheet to fill with content
84 * @param startCellX the cell number of the X start point (row) of the range to fill
85 * @param startCellY the cell number of the Y start point (column) of the range to fill
86 * @param rangeLengthX the size of the range expansion in X-direction
87 * @param rangeLengthY the size of the range expansion in Y-direction
88 * @throws java.lang.Exception on any error an <CODE>java.lang.Exception</CODE> was thrown
90 public static void fillCalcSheetWithContent(XSpreadsheet xSheet,
91 int startCellX, int startCellY, int rangeLengthX, int rangeLengthY)
92 throws java.lang.Exception {
94 try{
95 // create a range with content
96 Object[][] newData = new Object[rangeLengthY][rangeLengthX];
97 for (int i=0; i<rangeLengthY; i++) {
98 for (int j=0; j<rangeLengthX; j++) {
99 newData[i][j] = new Double(10*i +j);
102 XCellRange xRange = null;
103 try {
104 xRange = xSheet.getCellRangeByPosition(startCellX, startCellY,
105 startCellX+rangeLengthX-1, startCellY+rangeLengthY-1);
106 } catch ( IndexOutOfBoundsException e){
107 throw new Exception(
108 "Couldn't get CellRange from sheett: " + e.toString());
111 XCellRangeData xRangeData = (XCellRangeData) UnoRuntime.queryInterface(XCellRangeData.class, xRange);
113 xRangeData.setDataArray(newData);
114 } catch (Exception e){
115 throw new Exception(
116 "Couldn't fill CalcSheet with content: " + e.toString());
122 * returns an <CODE>XSpreadsheet</CODE> from a Calc document.
123 * @param xSheetDoc the Calc docuent which containes the sheet
124 * @param sheetNumber the number of the sheet to return
125 * @throws java.lang.Exception on any error an <CODE>java.lang.Exception</CODE> was thrown
126 * @return calc sheet
127 * @see com.sun.star.sheet.XSpreadsheet
129 public static XSpreadsheet getSpreadSheetFromSheetDoc(XComponent xSheetDoc, int sheetNumber)
130 throws java.lang.Exception {
132 XSpreadsheet xSheet = null;
134 try{
135 XSpreadsheetDocument xSpreadsheetDoc = (XSpreadsheetDocument)
136 UnoRuntime.queryInterface(XSpreadsheetDocument.class, xSheetDoc);
138 XSpreadsheets xSpreadsheets = xSpreadsheetDoc.getSheets();
140 XIndexAccess xSheetsIndexArray = (XIndexAccess)
141 UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
143 try{
144 xSheet = (XSpreadsheet) AnyConverter.toObject(
145 new Type(XSpreadsheet.class),xSheetsIndexArray.getByIndex(sheetNumber));
147 } catch (IllegalArgumentException e){
148 throw new Exception(
149 "Couldn't get sheet '" +sheetNumber + "' : " + e.toString());
150 } catch (IndexOutOfBoundsException e){
151 throw new Exception(
152 "Couldn't get sheet '" +sheetNumber + "' : " + e.toString());
153 } catch (WrappedTargetException e){
154 throw new Exception(
155 "Couldn't get sheet '" +sheetNumber + "' : " + e.toString());
157 } catch (Exception e){
158 throw new Exception(
159 "Couldn't get sheet '" +sheetNumber + "' : " + e.toString());
161 return xSheet;