1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package util;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
27cdf0e10cSrcweir import com.sun.star.lang.IllegalArgumentException;
28cdf0e10cSrcweir import com.sun.star.lang.IndexOutOfBoundsException;
29cdf0e10cSrcweir import com.sun.star.lang.WrappedTargetException;
30cdf0e10cSrcweir import com.sun.star.lang.XComponent;
31cdf0e10cSrcweir import com.sun.star.sheet.XCellRangeData;
32cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheet;
33cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument;
34cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheets;
35cdf0e10cSrcweir import com.sun.star.table.XCellRange;
36cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
37cdf0e10cSrcweir import com.sun.star.uno.Exception;
38cdf0e10cSrcweir import com.sun.star.uno.Type;
39cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir /**
42cdf0e10cSrcweir  * This class contains some usefull mathods to handle Calc documents
43cdf0e10cSrcweir  * and its sheets.
44cdf0e10cSrcweir  */
45cdf0e10cSrcweir public class CalcTools {
46cdf0e10cSrcweir 
47cdf0e10cSrcweir     /**
48cdf0e10cSrcweir      * fills a range of a calc sheet with computed data of type
49cdf0e10cSrcweir      * <CODE>Double</CODE>.
50cdf0e10cSrcweir      * @param xSheetDoc the Clac documents wich should be filled
51cdf0e10cSrcweir      * @param sheetNumber the number of the sheet of <CODE>xSheetDoc</CODE>
52cdf0e10cSrcweir      * @param startCellX the cell number of the X start point (row) of the range to fill
53cdf0e10cSrcweir      * @param startCellY the cell number of the Y start point (column) of the range to fill
54cdf0e10cSrcweir      * @param rangeLengthX the size of the range expansion in X-direction
55cdf0e10cSrcweir      * @param rangeLengthY the size of the range expansion in Y-direction
56cdf0e10cSrcweir      * @throws java.lang.Exception on any error an <CODE>java.lang.Exception</CODE> was thrown
57cdf0e10cSrcweir      */
58cdf0e10cSrcweir     public static void fillCalcSheetWithContent(XComponent xSheetDoc, int sheetNumber,
59cdf0e10cSrcweir                         int startCellX, int startCellY, int rangeLengthX, int rangeLengthY)
60cdf0e10cSrcweir                   throws java.lang.Exception {
61cdf0e10cSrcweir         try{
62cdf0e10cSrcweir             XSpreadsheet xSheet = getSpreadSheetFromSheetDoc(xSheetDoc, sheetNumber);
63cdf0e10cSrcweir 
64cdf0e10cSrcweir             fillCalcSheetWithContent(xSheet, startCellX, startCellY, rangeLengthX, rangeLengthY);
65cdf0e10cSrcweir 
66cdf0e10cSrcweir         } catch (Exception e){
67cdf0e10cSrcweir                 throw new Exception(
68cdf0e10cSrcweir                         "Couldn't fill CalcSheet with content: " + e.toString());
69cdf0e10cSrcweir         }
70cdf0e10cSrcweir     }
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     /**
73cdf0e10cSrcweir      * fills a range of a calc sheet with computed data of type
74cdf0e10cSrcweir      * <CODE>Double</CODE>.
75cdf0e10cSrcweir      * @param xSheet the sheet to fill with content
76cdf0e10cSrcweir      * @param startCellX the cell number of the X start point (row) of the range to fill
77cdf0e10cSrcweir      * @param startCellY the cell number of the Y start point (column) of the range to fill
78cdf0e10cSrcweir      * @param rangeLengthX the size of the range expansion in X-direction
79cdf0e10cSrcweir      * @param rangeLengthY the size of the range expansion in Y-direction
80cdf0e10cSrcweir      * @throws java.lang.Exception on any error an <CODE>java.lang.Exception</CODE> was thrown
81cdf0e10cSrcweir      */
82cdf0e10cSrcweir     public static void fillCalcSheetWithContent(XSpreadsheet xSheet,
83cdf0e10cSrcweir                         int startCellX, int startCellY, int rangeLengthX, int rangeLengthY)
84cdf0e10cSrcweir                   throws java.lang.Exception {
85cdf0e10cSrcweir 
86cdf0e10cSrcweir         try{
87cdf0e10cSrcweir             // create a range with content
88cdf0e10cSrcweir             Object[][] newData = new Object[rangeLengthY][rangeLengthX];
89cdf0e10cSrcweir             for (int i=0; i<rangeLengthY; i++) {
90cdf0e10cSrcweir                 for (int j=0; j<rangeLengthX; j++) {
91cdf0e10cSrcweir                     newData[i][j] = new Double(10*i +j);
92cdf0e10cSrcweir                 }
93cdf0e10cSrcweir             }
94cdf0e10cSrcweir             XCellRange xRange = null;
95cdf0e10cSrcweir             try {
96cdf0e10cSrcweir                 xRange = xSheet.getCellRangeByPosition(startCellX, startCellY,
97cdf0e10cSrcweir                                     startCellX+rangeLengthX-1, startCellY+rangeLengthY-1);
98cdf0e10cSrcweir             } catch ( IndexOutOfBoundsException e){
99cdf0e10cSrcweir                     throw new Exception(
100cdf0e10cSrcweir                             "Couldn't get CellRange from sheett: " + e.toString());
101cdf0e10cSrcweir             }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir             XCellRangeData xRangeData = (XCellRangeData) UnoRuntime.queryInterface(XCellRangeData.class, xRange);
104cdf0e10cSrcweir 
105cdf0e10cSrcweir             xRangeData.setDataArray(newData);
106cdf0e10cSrcweir         } catch (Exception e){
107cdf0e10cSrcweir                 throw new Exception(
108cdf0e10cSrcweir                         "Couldn't fill CalcSheet with content: " + e.toString());
109cdf0e10cSrcweir         }
110cdf0e10cSrcweir     }
111cdf0e10cSrcweir 
112cdf0e10cSrcweir     /**
113cdf0e10cSrcweir      *
114cdf0e10cSrcweir      * returns an <CODE>XSpreadsheet</CODE> from a Calc document.
115cdf0e10cSrcweir      * @param xSheetDoc the Calc docuent which containes the sheet
116cdf0e10cSrcweir      * @param sheetNumber the number of the sheet to return
117cdf0e10cSrcweir      * @throws java.lang.Exception on any error an <CODE>java.lang.Exception</CODE> was thrown
118cdf0e10cSrcweir      * @return calc sheet
119cdf0e10cSrcweir      * @see com.sun.star.sheet.XSpreadsheet
120cdf0e10cSrcweir      */
121cdf0e10cSrcweir     public static XSpreadsheet getSpreadSheetFromSheetDoc(XComponent xSheetDoc, int sheetNumber)
122cdf0e10cSrcweir                   throws java.lang.Exception {
123cdf0e10cSrcweir 
124cdf0e10cSrcweir         XSpreadsheet xSheet = null;
125cdf0e10cSrcweir 
126cdf0e10cSrcweir         try{
127cdf0e10cSrcweir             XSpreadsheetDocument xSpreadsheetDoc = (XSpreadsheetDocument)
128cdf0e10cSrcweir                     UnoRuntime.queryInterface(XSpreadsheetDocument.class, xSheetDoc);
129cdf0e10cSrcweir 
130cdf0e10cSrcweir             XSpreadsheets xSpreadsheets = xSpreadsheetDoc.getSheets();
131cdf0e10cSrcweir 
132cdf0e10cSrcweir             XIndexAccess xSheetsIndexArray = (XIndexAccess)
133cdf0e10cSrcweir                         UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
134cdf0e10cSrcweir 
135cdf0e10cSrcweir             try{
136cdf0e10cSrcweir                 xSheet = (XSpreadsheet) AnyConverter.toObject(
137cdf0e10cSrcweir                         new Type(XSpreadsheet.class),xSheetsIndexArray.getByIndex(sheetNumber));
138cdf0e10cSrcweir 
139cdf0e10cSrcweir             } catch (IllegalArgumentException e){
140cdf0e10cSrcweir                 throw new Exception(
141cdf0e10cSrcweir                         "Couldn't get sheet '" +sheetNumber + "' : " + e.toString());
142cdf0e10cSrcweir             } catch (IndexOutOfBoundsException e){
143cdf0e10cSrcweir                 throw new Exception(
144cdf0e10cSrcweir                         "Couldn't get sheet '" +sheetNumber + "' : " + e.toString());
145cdf0e10cSrcweir             } catch (WrappedTargetException e){
146cdf0e10cSrcweir                 throw new Exception(
147cdf0e10cSrcweir                         "Couldn't get sheet '" +sheetNumber + "' : " + e.toString());
148cdf0e10cSrcweir             }
149cdf0e10cSrcweir         } catch (Exception e){
150cdf0e10cSrcweir             throw new Exception(
151cdf0e10cSrcweir                 "Couldn't get sheet '" +sheetNumber + "' : " + e.toString());
152cdf0e10cSrcweir         }
153cdf0e10cSrcweir          return xSheet;
154cdf0e10cSrcweir     }
155cdf0e10cSrcweir }
156