2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import com
.sun
.star
.container
.XIndexAccess
;
22 import com
.sun
.star
.lang
.IllegalArgumentException
;
23 import com
.sun
.star
.lang
.IndexOutOfBoundsException
;
24 import com
.sun
.star
.lang
.WrappedTargetException
;
25 import com
.sun
.star
.lang
.XComponent
;
26 import com
.sun
.star
.sheet
.XCellRangeData
;
27 import com
.sun
.star
.sheet
.XSpreadsheet
;
28 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
29 import com
.sun
.star
.sheet
.XSpreadsheets
;
30 import com
.sun
.star
.table
.XCellRange
;
31 import com
.sun
.star
.uno
.AnyConverter
;
32 import com
.sun
.star
.uno
.Exception
;
33 import com
.sun
.star
.uno
.Type
;
34 import com
.sun
.star
.uno
.UnoRuntime
;
37 * This class contains some useful methods to handle Calc documents
40 public class CalcTools
{
43 * fills a range of a calc sheet with computed data of type
44 * <CODE>Double</CODE>.
45 * @param xSheetDoc the Calc documents which should be filled
46 * @param sheetNumber the number of the sheet of <CODE>xSheetDoc</CODE>
47 * @param startCellX the cell number of the X start point (row) of the range to fill
48 * @param startCellY the cell number of the Y start point (column) of the range to fill
49 * @param rangeLengthX the size of the range expansion in X-direction
50 * @param rangeLengthY the size of the range expansion in Y-direction
51 * @throws java.lang.Exception on any error an <CODE>java.lang.Exception</CODE> was thrown
53 public static void fillCalcSheetWithContent(XComponent xSheetDoc
, int sheetNumber
,
54 int startCellX
, int startCellY
, int rangeLengthX
, int rangeLengthY
)
55 throws java
.lang
.Exception
{
56 XSpreadsheet xSheet
= getSpreadSheetFromSheetDoc(xSheetDoc
, sheetNumber
);
58 fillCalcSheetWithContent(xSheet
, startCellX
, startCellY
, rangeLengthX
, rangeLengthY
);
62 * fills a range of a calc sheet with computed data of type
63 * <CODE>Double</CODE>.
64 * @param xSheet the sheet to fill with content
65 * @param startCellX the cell number of the X start point (row) of the range to fill
66 * @param startCellY the cell number of the Y start point (column) of the range to fill
67 * @param rangeLengthX the size of the range expansion in X-direction
68 * @param rangeLengthY the size of the range expansion in Y-direction
69 * @throws java.lang.Exception on any error an <CODE>java.lang.Exception</CODE> was thrown
71 public static void fillCalcSheetWithContent(XSpreadsheet xSheet
,
72 int startCellX
, int startCellY
, int rangeLengthX
, int rangeLengthY
)
73 throws java
.lang
.Exception
{
74 // create a range with content
75 Object
[][] newData
= new Object
[rangeLengthY
][rangeLengthX
];
76 for (int i
=0; i
<rangeLengthY
; i
++) {
77 for (int j
=0; j
<rangeLengthX
; j
++) {
78 newData
[i
][j
] = new Double(10*i
+j
);
81 XCellRange xRange
= null;
83 xRange
= xSheet
.getCellRangeByPosition(startCellX
, startCellY
,
84 startCellX
+rangeLengthX
-1, startCellY
+rangeLengthY
-1);
85 } catch ( IndexOutOfBoundsException ex
){
86 throw new Exception(ex
, "Couldn't get CellRange from sheet");
89 XCellRangeData xRangeData
= UnoRuntime
.queryInterface(XCellRangeData
.class, xRange
);
91 xRangeData
.setDataArray(newData
);
96 * returns an <CODE>XSpreadsheet</CODE> from a Calc document.
97 * @param xSheetDoc the Calc document which contains the sheet
98 * @param sheetNumber the number of the sheet to return
99 * @throws java.lang.Exception on any error an <CODE>java.lang.Exception</CODE> was thrown
101 * @see com.sun.star.sheet.XSpreadsheet
103 private static XSpreadsheet
getSpreadSheetFromSheetDoc(XComponent xSheetDoc
, int sheetNumber
)
104 throws java
.lang
.Exception
{
106 XSpreadsheet xSheet
= null;
109 XSpreadsheetDocument xSpreadsheetDoc
= UnoRuntime
.queryInterface(XSpreadsheetDocument
.class, xSheetDoc
);
111 XSpreadsheets xSpreadsheets
= xSpreadsheetDoc
.getSheets();
113 XIndexAccess xSheetsIndexArray
= UnoRuntime
.queryInterface(XIndexAccess
.class, xSpreadsheets
);
115 xSheet
= (XSpreadsheet
) AnyConverter
.toObject(
116 new Type(XSpreadsheet
.class),xSheetsIndexArray
.getByIndex(sheetNumber
));
118 } catch (IllegalArgumentException ex
){
119 throw new Exception(ex
, "Couldn't get sheet '" +sheetNumber
+ "'");
120 } catch (IndexOutOfBoundsException ex
){
121 throw new Exception(ex
, "Couldn't get sheet '" +sheetNumber
+ "'");
122 } catch (WrappedTargetException ex
) {
123 throw new Exception(ex
, "Couldn't get sheet '" +sheetNumber
+ "'");