merged tag ooo/DEV300_m102
[LibreOffice.git] / qadevOOo / runner / util / CalcTools.java
blob08a8b945839a71b1f118dc42ca059669c0a89efc
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************
29 package util;
31 import com.sun.star.container.XIndexAccess;
32 import com.sun.star.lang.IllegalArgumentException;
33 import com.sun.star.lang.IndexOutOfBoundsException;
34 import com.sun.star.lang.WrappedTargetException;
35 import com.sun.star.lang.XComponent;
36 import com.sun.star.sheet.XCellRangeData;
37 import com.sun.star.sheet.XSpreadsheet;
38 import com.sun.star.sheet.XSpreadsheetDocument;
39 import com.sun.star.sheet.XSpreadsheets;
40 import com.sun.star.table.XCellRange;
41 import com.sun.star.uno.AnyConverter;
42 import com.sun.star.uno.Exception;
43 import com.sun.star.uno.Type;
44 import com.sun.star.uno.UnoRuntime;
46 /**
47 * This class contains some usefull mathods to handle Calc documents
48 * and its sheets.
50 public class CalcTools {
52 /**
53 * fills a range of a calc sheet with computed data of type
54 * <CODE>Double</CODE>.
55 * @param xSheetDoc the Clac documents wich should be filled
56 * @param sheetNumber the number of the sheet of <CODE>xSheetDoc</CODE>
57 * @param startCellX the cell number of the X start point (row) of the range to fill
58 * @param startCellY the cell number of the Y start point (column) of the range to fill
59 * @param rangeLengthX the size of the range expansion in X-direction
60 * @param rangeLengthY the size of the range expansion in Y-direction
61 * @throws java.lang.Exception on any error an <CODE>java.lang.Exception</CODE> was thrown
63 public static void fillCalcSheetWithContent(XComponent xSheetDoc, int sheetNumber,
64 int startCellX, int startCellY, int rangeLengthX, int rangeLengthY)
65 throws java.lang.Exception {
66 try{
67 XSpreadsheet xSheet = getSpreadSheetFromSheetDoc(xSheetDoc, sheetNumber);
69 fillCalcSheetWithContent(xSheet, startCellX, startCellY, rangeLengthX, rangeLengthY);
71 } catch (Exception e){
72 throw new Exception(
73 "Couldn't fill CalcSheet with content: " + e.toString());
77 /**
78 * fills a range of a calc sheet with computed data of type
79 * <CODE>Double</CODE>.
80 * @param xSheet the sheet to fill with content
81 * @param startCellX the cell number of the X start point (row) of the range to fill
82 * @param startCellY the cell number of the Y start point (column) of the range to fill
83 * @param rangeLengthX the size of the range expansion in X-direction
84 * @param rangeLengthY the size of the range expansion in Y-direction
85 * @throws java.lang.Exception on any error an <CODE>java.lang.Exception</CODE> was thrown
87 public static void fillCalcSheetWithContent(XSpreadsheet xSheet,
88 int startCellX, int startCellY, int rangeLengthX, int rangeLengthY)
89 throws java.lang.Exception {
91 try{
92 // create a range with content
93 Object[][] newData = new Object[rangeLengthY][rangeLengthX];
94 for (int i=0; i<rangeLengthY; i++) {
95 for (int j=0; j<rangeLengthX; j++) {
96 newData[i][j] = new Double(10*i +j);
99 XCellRange xRange = null;
100 try {
101 xRange = xSheet.getCellRangeByPosition(startCellX, startCellY,
102 startCellX+rangeLengthX-1, startCellY+rangeLengthY-1);
103 } catch ( IndexOutOfBoundsException e){
104 throw new Exception(
105 "Couldn't get CellRange from sheett: " + e.toString());
108 XCellRangeData xRangeData = (XCellRangeData) UnoRuntime.queryInterface(XCellRangeData.class, xRange);
110 xRangeData.setDataArray(newData);
111 } catch (Exception e){
112 throw new Exception(
113 "Couldn't fill CalcSheet with content: " + e.toString());
119 * returns an <CODE>XSpreadsheet</CODE> from a Calc document.
120 * @param xSheetDoc the Calc docuent which containes the sheet
121 * @param sheetNumber the number of the sheet to return
122 * @throws java.lang.Exception on any error an <CODE>java.lang.Exception</CODE> was thrown
123 * @return calc sheet
124 * @see com.sun.star.sheet.XSpreadsheet
126 public static XSpreadsheet getSpreadSheetFromSheetDoc(XComponent xSheetDoc, int sheetNumber)
127 throws java.lang.Exception {
129 XSpreadsheet xSheet = null;
131 try{
132 XSpreadsheetDocument xSpreadsheetDoc = (XSpreadsheetDocument)
133 UnoRuntime.queryInterface(XSpreadsheetDocument.class, xSheetDoc);
135 XSpreadsheets xSpreadsheets = xSpreadsheetDoc.getSheets();
137 XIndexAccess xSheetsIndexArray = (XIndexAccess)
138 UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
140 try{
141 xSheet = (XSpreadsheet) AnyConverter.toObject(
142 new Type(XSpreadsheet.class),xSheetsIndexArray.getByIndex(sheetNumber));
144 } catch (IllegalArgumentException e){
145 throw new Exception(
146 "Couldn't get sheet '" +sheetNumber + "' : " + e.toString());
147 } catch (IndexOutOfBoundsException e){
148 throw new Exception(
149 "Couldn't get sheet '" +sheetNumber + "' : " + e.toString());
150 } catch (WrappedTargetException e){
151 throw new Exception(
152 "Couldn't get sheet '" +sheetNumber + "' : " + e.toString());
154 } catch (Exception e){
155 throw new Exception(
156 "Couldn't get sheet '" +sheetNumber + "' : " + e.toString());
158 return xSheet;