xref: /AOO41X/test/testuno/source/testlib/uno/SCUtil.java (revision e6e6073ddaad3a04a985e8f05823629a884eb203)
1*e6e6073dSLiu Zhe /**************************************************************
2*e6e6073dSLiu Zhe  *
3*e6e6073dSLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
4*e6e6073dSLiu Zhe  * or more contributor license agreements.  See the NOTICE file
5*e6e6073dSLiu Zhe  * distributed with this work for additional information
6*e6e6073dSLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
7*e6e6073dSLiu Zhe  * to you under the Apache License, Version 2.0 (the
8*e6e6073dSLiu Zhe  * "License"); you may not use this file except in compliance
9*e6e6073dSLiu Zhe  * with the License.  You may obtain a copy of the License at
10*e6e6073dSLiu Zhe  *
11*e6e6073dSLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
12*e6e6073dSLiu Zhe  *
13*e6e6073dSLiu Zhe  * Unless required by applicable law or agreed to in writing,
14*e6e6073dSLiu Zhe  * software distributed under the License is distributed on an
15*e6e6073dSLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*e6e6073dSLiu Zhe  * KIND, either express or implied.  See the License for the
17*e6e6073dSLiu Zhe  * specific language governing permissions and limitations
18*e6e6073dSLiu Zhe  * under the License.
19*e6e6073dSLiu Zhe  *
20*e6e6073dSLiu Zhe  *************************************************************/
21*e6e6073dSLiu Zhe 
22*e6e6073dSLiu Zhe 
23*e6e6073dSLiu Zhe package testlib.uno;
24*e6e6073dSLiu Zhe 
25*e6e6073dSLiu Zhe import java.util.HashMap;
26*e6e6073dSLiu Zhe 
27*e6e6073dSLiu Zhe import org.openoffice.test.common.FileUtil;
28*e6e6073dSLiu Zhe import org.openoffice.test.common.Testspace;
29*e6e6073dSLiu Zhe import org.openoffice.test.uno.UnoApp;
30*e6e6073dSLiu Zhe 
31*e6e6073dSLiu Zhe import com.sun.star.beans.PropertyValue;
32*e6e6073dSLiu Zhe import com.sun.star.beans.XPropertySet;
33*e6e6073dSLiu Zhe import com.sun.star.container.XIndexAccess;
34*e6e6073dSLiu Zhe import com.sun.star.container.XNamed;
35*e6e6073dSLiu Zhe import com.sun.star.frame.XController;
36*e6e6073dSLiu Zhe import com.sun.star.frame.XModel;
37*e6e6073dSLiu Zhe import com.sun.star.frame.XStorable;
38*e6e6073dSLiu Zhe import com.sun.star.lang.XComponent;
39*e6e6073dSLiu Zhe import com.sun.star.sheet.XSpreadsheet;
40*e6e6073dSLiu Zhe import com.sun.star.sheet.XSpreadsheetDocument;
41*e6e6073dSLiu Zhe import com.sun.star.sheet.XSpreadsheetView;
42*e6e6073dSLiu Zhe import com.sun.star.sheet.XSpreadsheets;
43*e6e6073dSLiu Zhe import com.sun.star.table.XCell;
44*e6e6073dSLiu Zhe import com.sun.star.table.XCellRange;
45*e6e6073dSLiu Zhe import com.sun.star.table.XColumnRowRange;
46*e6e6073dSLiu Zhe import com.sun.star.table.XTableColumns;
47*e6e6073dSLiu Zhe import com.sun.star.table.XTableRows;
48*e6e6073dSLiu Zhe import com.sun.star.text.XText;
49*e6e6073dSLiu Zhe import com.sun.star.uno.UnoRuntime;
50*e6e6073dSLiu Zhe import com.sun.star.util.XCloseable;
51*e6e6073dSLiu Zhe 
52*e6e6073dSLiu Zhe 
53*e6e6073dSLiu Zhe /**
54*e6e6073dSLiu Zhe  * Utilities of Spreadsheet
55*e6e6073dSLiu Zhe  *
56*e6e6073dSLiu Zhe  */
57*e6e6073dSLiu Zhe 
58*e6e6073dSLiu Zhe public class SCUtil {
59*e6e6073dSLiu Zhe 
60*e6e6073dSLiu Zhe     private static final String scTempDir = "output/sc/"; //Spreadsheet temp file directory
61*e6e6073dSLiu Zhe     private static HashMap filterName = new HashMap();
62*e6e6073dSLiu Zhe 
63*e6e6073dSLiu Zhe     private SCUtil() {
64*e6e6073dSLiu Zhe 
65*e6e6073dSLiu Zhe     }
66*e6e6073dSLiu Zhe 
67*e6e6073dSLiu Zhe     /**
68*e6e6073dSLiu Zhe      * Get spreadsheet document object
69*e6e6073dSLiu Zhe      * @param xSpreadsheetComponent
70*e6e6073dSLiu Zhe      * @return
71*e6e6073dSLiu Zhe      * @throws Exception
72*e6e6073dSLiu Zhe      */
73*e6e6073dSLiu Zhe     public static XSpreadsheetDocument getSCDocument(XComponent xSpreadsheetComponent) throws Exception {
74*e6e6073dSLiu Zhe         XSpreadsheetDocument xSpreadsheetDocument =
75*e6e6073dSLiu Zhe                 (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, xSpreadsheetComponent);
76*e6e6073dSLiu Zhe 
77*e6e6073dSLiu Zhe         return xSpreadsheetDocument;
78*e6e6073dSLiu Zhe     }
79*e6e6073dSLiu Zhe 
80*e6e6073dSLiu Zhe     /**
81*e6e6073dSLiu Zhe      * Get sheet object by sheet name
82*e6e6073dSLiu Zhe      * @param xSpreadsheetDocument
83*e6e6073dSLiu Zhe      * @param sheetName
84*e6e6073dSLiu Zhe      * @return
85*e6e6073dSLiu Zhe      * @throws Exception
86*e6e6073dSLiu Zhe      */
87*e6e6073dSLiu Zhe     public static XSpreadsheet getSCSheetByName(XSpreadsheetDocument xSpreadsheetDocument, String sheetName) throws Exception {
88*e6e6073dSLiu Zhe         XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
89*e6e6073dSLiu Zhe         XSpreadsheet xSpreadsheet =
90*e6e6073dSLiu Zhe                 (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, xSpreadsheets.getByName(sheetName));
91*e6e6073dSLiu Zhe 
92*e6e6073dSLiu Zhe         return xSpreadsheet;
93*e6e6073dSLiu Zhe     }
94*e6e6073dSLiu Zhe 
95*e6e6073dSLiu Zhe     /**
96*e6e6073dSLiu Zhe      * Get sheet object by sheet index
97*e6e6073dSLiu Zhe      * @param xSpreadsheetDocument
98*e6e6073dSLiu Zhe      * @param index   (Short) 0,1,2,...
99*e6e6073dSLiu Zhe      * @return
100*e6e6073dSLiu Zhe      * @throws Exception
101*e6e6073dSLiu Zhe      */
102*e6e6073dSLiu Zhe     public static XSpreadsheet getSCSheetByIndex(XSpreadsheetDocument xSpreadsheetDocument, short index) throws Exception {
103*e6e6073dSLiu Zhe         XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
104*e6e6073dSLiu Zhe         XIndexAccess xIndexAccess =
105*e6e6073dSLiu Zhe                 (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
106*e6e6073dSLiu Zhe         XSpreadsheet xSpreadsheet =
107*e6e6073dSLiu Zhe                 (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, xIndexAccess.getByIndex(index));
108*e6e6073dSLiu Zhe 
109*e6e6073dSLiu Zhe         return xSpreadsheet;
110*e6e6073dSLiu Zhe     }
111*e6e6073dSLiu Zhe 
112*e6e6073dSLiu Zhe     /**
113*e6e6073dSLiu Zhe      * Get sheet name by sheet index
114*e6e6073dSLiu Zhe      *
115*e6e6073dSLiu Zhe      * @param xSpreadsheetDocument
116*e6e6073dSLiu Zhe      * @param index
117*e6e6073dSLiu Zhe      *            (Short) 0,1,2,...
118*e6e6073dSLiu Zhe      * @return
119*e6e6073dSLiu Zhe      * @throws Exception
120*e6e6073dSLiu Zhe      */
121*e6e6073dSLiu Zhe     public static String getSCSheetNameByIndex(
122*e6e6073dSLiu Zhe             XSpreadsheetDocument xSpreadsheetDocument, short index)
123*e6e6073dSLiu Zhe             throws Exception {
124*e6e6073dSLiu Zhe         XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
125*e6e6073dSLiu Zhe         XIndexAccess xIndexAccess = (XIndexAccess) UnoRuntime.queryInterface(
126*e6e6073dSLiu Zhe                 XIndexAccess.class, xSpreadsheets);
127*e6e6073dSLiu Zhe         XSpreadsheet xSpreadsheet = (XSpreadsheet) UnoRuntime.queryInterface(
128*e6e6073dSLiu Zhe                 XSpreadsheet.class, xIndexAccess.getByIndex(index));
129*e6e6073dSLiu Zhe         XNamed xsheetname = (XNamed) UnoRuntime.queryInterface(XNamed.class,
130*e6e6073dSLiu Zhe                 xSpreadsheet);
131*e6e6073dSLiu Zhe         return xsheetname.getName();
132*e6e6073dSLiu Zhe     }
133*e6e6073dSLiu Zhe 
134*e6e6073dSLiu Zhe     /**
135*e6e6073dSLiu Zhe      * Set sheet name by sheet index
136*e6e6073dSLiu Zhe      *
137*e6e6073dSLiu Zhe      * @param xSpreadsheetDocument
138*e6e6073dSLiu Zhe      * @param index
139*e6e6073dSLiu Zhe      *            (Short) 0,1,2,...
140*e6e6073dSLiu Zhe      * @return
141*e6e6073dSLiu Zhe      * @throws Exception
142*e6e6073dSLiu Zhe      */
143*e6e6073dSLiu Zhe     public static void setSCSheetNameByIndex(
144*e6e6073dSLiu Zhe             XSpreadsheetDocument xSpreadsheetDocument, short index,
145*e6e6073dSLiu Zhe             String sheetname) throws Exception {
146*e6e6073dSLiu Zhe         XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
147*e6e6073dSLiu Zhe         XIndexAccess xIndexAccess = (XIndexAccess) UnoRuntime.queryInterface(
148*e6e6073dSLiu Zhe                 XIndexAccess.class, xSpreadsheets);
149*e6e6073dSLiu Zhe         XSpreadsheet xSpreadsheet = (XSpreadsheet) UnoRuntime.queryInterface(
150*e6e6073dSLiu Zhe                 XSpreadsheet.class, xIndexAccess.getByIndex(index));
151*e6e6073dSLiu Zhe         XNamed xsheetname = (XNamed) UnoRuntime.queryInterface(XNamed.class,
152*e6e6073dSLiu Zhe                 xSpreadsheet);
153*e6e6073dSLiu Zhe         xsheetname.setName(sheetname);
154*e6e6073dSLiu Zhe     }
155*e6e6073dSLiu Zhe 
156*e6e6073dSLiu Zhe     /**
157*e6e6073dSLiu Zhe      * Get rows object
158*e6e6073dSLiu Zhe      * @param xSpreadsheet
159*e6e6073dSLiu Zhe      * @return
160*e6e6073dSLiu Zhe      * @throws Exception
161*e6e6073dSLiu Zhe      */
162*e6e6073dSLiu Zhe     public static XTableRows getSCRows(XSpreadsheet xSpreadsheet) throws Exception {
163*e6e6073dSLiu Zhe         XColumnRowRange xColumnRowRange =
164*e6e6073dSLiu Zhe                 (XColumnRowRange) UnoRuntime.queryInterface(XColumnRowRange.class, xSpreadsheet);
165*e6e6073dSLiu Zhe         XTableRows xTableRows = xColumnRowRange.getRows();
166*e6e6073dSLiu Zhe 
167*e6e6073dSLiu Zhe         return xTableRows;
168*e6e6073dSLiu Zhe     }
169*e6e6073dSLiu Zhe 
170*e6e6073dSLiu Zhe     /**
171*e6e6073dSLiu Zhe      * Get columns object
172*e6e6073dSLiu Zhe      * @param xSpreadsheet
173*e6e6073dSLiu Zhe      * @return
174*e6e6073dSLiu Zhe      * @throws Exception
175*e6e6073dSLiu Zhe      */
176*e6e6073dSLiu Zhe     public static XTableColumns getSCColumns(XSpreadsheet xSpreadsheet) throws Exception {
177*e6e6073dSLiu Zhe         XColumnRowRange xColumnRowRange =
178*e6e6073dSLiu Zhe                 (XColumnRowRange) UnoRuntime.queryInterface(XColumnRowRange.class, xSpreadsheet);
179*e6e6073dSLiu Zhe         XTableColumns xTableColumns = xColumnRowRange.getColumns();
180*e6e6073dSLiu Zhe 
181*e6e6073dSLiu Zhe         return xTableColumns;
182*e6e6073dSLiu Zhe     }
183*e6e6073dSLiu Zhe 
184*e6e6073dSLiu Zhe     /**
185*e6e6073dSLiu Zhe      * Set floating number into specific cell
186*e6e6073dSLiu Zhe      * @param xSpreadsheet
187*e6e6073dSLiu Zhe      * @param column
188*e6e6073dSLiu Zhe      * @param row
189*e6e6073dSLiu Zhe      * @param value
190*e6e6073dSLiu Zhe      * @throws Exception
191*e6e6073dSLiu Zhe      */
192*e6e6073dSLiu Zhe     public static void setValueToCell(XSpreadsheet xSpreadsheet, int column, int row, double value) throws Exception {
193*e6e6073dSLiu Zhe         XCell xCell = xSpreadsheet.getCellByPosition(column, row);
194*e6e6073dSLiu Zhe         xCell.setValue(value);
195*e6e6073dSLiu Zhe     }
196*e6e6073dSLiu Zhe 
197*e6e6073dSLiu Zhe     /**
198*e6e6073dSLiu Zhe      * Set text into specific cell
199*e6e6073dSLiu Zhe      * @param xSpreadsheet
200*e6e6073dSLiu Zhe      * @param column
201*e6e6073dSLiu Zhe      * @param row
202*e6e6073dSLiu Zhe      * @param text
203*e6e6073dSLiu Zhe      * @throws Exception
204*e6e6073dSLiu Zhe      */
205*e6e6073dSLiu Zhe     public static void setTextToCell(XSpreadsheet xSpreadsheet, int column, int row, String text) throws Exception {
206*e6e6073dSLiu Zhe         XCell xCell = xSpreadsheet.getCellByPosition(column, row);
207*e6e6073dSLiu Zhe         XText xText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
208*e6e6073dSLiu Zhe         xText.setString(text);
209*e6e6073dSLiu Zhe     }
210*e6e6073dSLiu Zhe 
211*e6e6073dSLiu Zhe     /**
212*e6e6073dSLiu Zhe      * Set text into specific cell
213*e6e6073dSLiu Zhe      * @param xCell
214*e6e6073dSLiu Zhe      * @param text
215*e6e6073dSLiu Zhe      * @throws Exception
216*e6e6073dSLiu Zhe      */
217*e6e6073dSLiu Zhe     public static void setTextToCell(XCell xCell, String text) throws Exception {
218*e6e6073dSLiu Zhe         XText xText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
219*e6e6073dSLiu Zhe         xText.setString(text);
220*e6e6073dSLiu Zhe     }
221*e6e6073dSLiu Zhe 
222*e6e6073dSLiu Zhe     /**
223*e6e6073dSLiu Zhe      * Set formula into specific cell
224*e6e6073dSLiu Zhe      * @param xSpreadsheet
225*e6e6073dSLiu Zhe      * @param column
226*e6e6073dSLiu Zhe      * @param row
227*e6e6073dSLiu Zhe      * @param formula
228*e6e6073dSLiu Zhe      * @throws Exception
229*e6e6073dSLiu Zhe      */
230*e6e6073dSLiu Zhe     public static void setFormulaToCell(XSpreadsheet xSpreadsheet, int column, int row, String formula) throws Exception {
231*e6e6073dSLiu Zhe         XCell xCell = xSpreadsheet.getCellByPosition(column, row);
232*e6e6073dSLiu Zhe         xCell.setFormula(formula);
233*e6e6073dSLiu Zhe     }
234*e6e6073dSLiu Zhe 
235*e6e6073dSLiu Zhe     /**
236*e6e6073dSLiu Zhe      * Get value from specific cell
237*e6e6073dSLiu Zhe      * @param xSpreadsheet
238*e6e6073dSLiu Zhe      * @param column
239*e6e6073dSLiu Zhe      * @param row
240*e6e6073dSLiu Zhe      * @return
241*e6e6073dSLiu Zhe      * @throws Exception
242*e6e6073dSLiu Zhe      */
243*e6e6073dSLiu Zhe     public static double getValueFromCell(XSpreadsheet xSpreadsheet, int column, int row) throws Exception {
244*e6e6073dSLiu Zhe         XCell xCell = xSpreadsheet.getCellByPosition(column, row);
245*e6e6073dSLiu Zhe         double cellValue = xCell.getValue();
246*e6e6073dSLiu Zhe 
247*e6e6073dSLiu Zhe         return cellValue;
248*e6e6073dSLiu Zhe     }
249*e6e6073dSLiu Zhe 
250*e6e6073dSLiu Zhe     /**
251*e6e6073dSLiu Zhe      * Get text from specific cell
252*e6e6073dSLiu Zhe      * @param xSpreadsheet
253*e6e6073dSLiu Zhe      * @param column
254*e6e6073dSLiu Zhe      * @param row
255*e6e6073dSLiu Zhe      *
256*e6e6073dSLiu Zhe      * @return
257*e6e6073dSLiu Zhe      * @throws Exception
258*e6e6073dSLiu Zhe      */
259*e6e6073dSLiu Zhe     public static String getTextFromCell(XSpreadsheet xSpreadsheet, int column, int row) throws Exception {
260*e6e6073dSLiu Zhe         XCell xCell = xSpreadsheet.getCellByPosition(column, row);
261*e6e6073dSLiu Zhe         XText xText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
262*e6e6073dSLiu Zhe 
263*e6e6073dSLiu Zhe         return xText.getString();
264*e6e6073dSLiu Zhe     }
265*e6e6073dSLiu Zhe 
266*e6e6073dSLiu Zhe     /**
267*e6e6073dSLiu Zhe      * Get formula string from specific cell
268*e6e6073dSLiu Zhe      * @param xSpreadsheet
269*e6e6073dSLiu Zhe      * @param column
270*e6e6073dSLiu Zhe      * @param row
271*e6e6073dSLiu Zhe      * @return
272*e6e6073dSLiu Zhe      * @throws Exception
273*e6e6073dSLiu Zhe      */
274*e6e6073dSLiu Zhe     public static String getFormulaFromCell(XSpreadsheet xSpreadsheet, int column, int row) throws Exception {
275*e6e6073dSLiu Zhe         XCell xCell = xSpreadsheet.getCellByPosition(column, row);
276*e6e6073dSLiu Zhe         String cellFormula = xCell.getFormula();
277*e6e6073dSLiu Zhe 
278*e6e6073dSLiu Zhe         return cellFormula;
279*e6e6073dSLiu Zhe     }
280*e6e6073dSLiu Zhe 
281*e6e6073dSLiu Zhe     /**
282*e6e6073dSLiu Zhe      * Set numbers into a cell range
283*e6e6073dSLiu Zhe      * @param xSpreadsheet
284*e6e6073dSLiu Zhe      * @param start_col
285*e6e6073dSLiu Zhe      * @param start_row
286*e6e6073dSLiu Zhe      * @param end_col
287*e6e6073dSLiu Zhe      * @param end_row
288*e6e6073dSLiu Zhe      * @param values
289*e6e6073dSLiu Zhe      * @throws Exception
290*e6e6073dSLiu Zhe      */
291*e6e6073dSLiu Zhe     public static void setValueToCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row,  double[][] values) throws Exception {
292*e6e6073dSLiu Zhe         XCellRange xCellRange = xSpreadsheet.getCellRangeByPosition(start_col, start_row, end_col, end_row);
293*e6e6073dSLiu Zhe         XCell xCell = null;
294*e6e6073dSLiu Zhe         for (int i = 0; i <= (end_row - start_row); i++ ) {
295*e6e6073dSLiu Zhe             for(int j = 0; j <= (end_col - start_col); j++) {
296*e6e6073dSLiu Zhe                 xCell = xCellRange.getCellByPosition(j, i);
297*e6e6073dSLiu Zhe                 xCell.setValue(values[i][j]);
298*e6e6073dSLiu Zhe             }
299*e6e6073dSLiu Zhe         }
300*e6e6073dSLiu Zhe     }
301*e6e6073dSLiu Zhe 
302*e6e6073dSLiu Zhe     /**
303*e6e6073dSLiu Zhe      * Set text into a cell range
304*e6e6073dSLiu Zhe      * @param xSpreadsheet
305*e6e6073dSLiu Zhe      * @param start_col
306*e6e6073dSLiu Zhe      * @param start_row
307*e6e6073dSLiu Zhe      * @param end_col
308*e6e6073dSLiu Zhe      * @param end_row
309*e6e6073dSLiu Zhe      * @param texts
310*e6e6073dSLiu Zhe      * @throws Exception
311*e6e6073dSLiu Zhe      */
312*e6e6073dSLiu Zhe     public static void setTextToCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row,  String[][] texts) throws Exception {
313*e6e6073dSLiu Zhe         XCellRange xCellRange = xSpreadsheet.getCellRangeByPosition(start_col, start_row, end_col, end_row);
314*e6e6073dSLiu Zhe         XCell xCell = null;
315*e6e6073dSLiu Zhe         XText xText = null;
316*e6e6073dSLiu Zhe         for (int i = 0; i <= (end_row - start_row); i++ ) {
317*e6e6073dSLiu Zhe             for(int j = 0; j <= (end_col - start_col); j++) {
318*e6e6073dSLiu Zhe                 xCell = xCellRange.getCellByPosition(j, i);
319*e6e6073dSLiu Zhe                 xText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
320*e6e6073dSLiu Zhe                 xText.setString(texts[i][j]);
321*e6e6073dSLiu Zhe             }
322*e6e6073dSLiu Zhe         }
323*e6e6073dSLiu Zhe     }
324*e6e6073dSLiu Zhe 
325*e6e6073dSLiu Zhe     /**
326*e6e6073dSLiu Zhe      * Get number content from a cell range
327*e6e6073dSLiu Zhe      * @param xSpreadsheet
328*e6e6073dSLiu Zhe      * @param start_col
329*e6e6073dSLiu Zhe      * @param start_row
330*e6e6073dSLiu Zhe      * @param end_col
331*e6e6073dSLiu Zhe      * @param end_row
332*e6e6073dSLiu Zhe      * @return
333*e6e6073dSLiu Zhe      * @throws Exception
334*e6e6073dSLiu Zhe      */
335*e6e6073dSLiu Zhe     public static double[][] getValueFromCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row) throws Exception {
336*e6e6073dSLiu Zhe         XCellRange xCellRange = xSpreadsheet.getCellRangeByPosition(start_col, start_row, end_col, end_row);
337*e6e6073dSLiu Zhe         XCell xCell = null;
338*e6e6073dSLiu Zhe         double[][] cellValues = new double[end_row - start_row+1][end_col - start_col +1];
339*e6e6073dSLiu Zhe 
340*e6e6073dSLiu Zhe         for (int i = 0; i <= (end_row - start_row); i++ ) {
341*e6e6073dSLiu Zhe             for(int j = 0; j <= (end_col - start_col); j++) {
342*e6e6073dSLiu Zhe                 xCell = xCellRange.getCellByPosition(j, i);
343*e6e6073dSLiu Zhe                 cellValues[i][j] = xCell.getValue();
344*e6e6073dSLiu Zhe             }
345*e6e6073dSLiu Zhe         }
346*e6e6073dSLiu Zhe 
347*e6e6073dSLiu Zhe         return cellValues;
348*e6e6073dSLiu Zhe     }
349*e6e6073dSLiu Zhe 
350*e6e6073dSLiu Zhe     /**
351*e6e6073dSLiu Zhe      * Get text content from a cell range
352*e6e6073dSLiu Zhe      * @param xSpreadsheet
353*e6e6073dSLiu Zhe      * @param start_col
354*e6e6073dSLiu Zhe      * @param start_row
355*e6e6073dSLiu Zhe      * @param end_col
356*e6e6073dSLiu Zhe      * @param end_row
357*e6e6073dSLiu Zhe      * @return
358*e6e6073dSLiu Zhe      * @throws Exception
359*e6e6073dSLiu Zhe      */
360*e6e6073dSLiu Zhe     public static String[][] getTextFromCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row) throws Exception {
361*e6e6073dSLiu Zhe         XCellRange xCellRange = xSpreadsheet.getCellRangeByPosition(start_col, start_row, end_col, end_row);
362*e6e6073dSLiu Zhe         XCell xCell = null;
363*e6e6073dSLiu Zhe         XText xText = null;
364*e6e6073dSLiu Zhe         String[][] cellTexts = new String[end_row - start_row+1][end_col - start_col +1];
365*e6e6073dSLiu Zhe 
366*e6e6073dSLiu Zhe         for (int i = 0; i <= (end_row - start_row); i++ ) {
367*e6e6073dSLiu Zhe             for (int j = 0; j <= (end_col - start_col); j++) {
368*e6e6073dSLiu Zhe                 xCell = xCellRange.getCellByPosition(j, i);
369*e6e6073dSLiu Zhe                 xText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
370*e6e6073dSLiu Zhe                 cellTexts[i][j] = xText.getString();
371*e6e6073dSLiu Zhe             }
372*e6e6073dSLiu Zhe         }
373*e6e6073dSLiu Zhe 
374*e6e6073dSLiu Zhe         return cellTexts;
375*e6e6073dSLiu Zhe     }
376*e6e6073dSLiu Zhe 
377*e6e6073dSLiu Zhe     //TODO ZS - public static String[][] getAllFromCellRange
378*e6e6073dSLiu Zhe 
379*e6e6073dSLiu Zhe     /**
380*e6e6073dSLiu Zhe      * Switch to specific sheet
381*e6e6073dSLiu Zhe      * @param xSpreadsheetDocument
382*e6e6073dSLiu Zhe      * @param xSpreadsheet
383*e6e6073dSLiu Zhe      */
384*e6e6073dSLiu Zhe     public static void setCurrentSheet(XSpreadsheetDocument xSpreadsheetDocument, XSpreadsheet xSpreadsheet) throws Exception {
385*e6e6073dSLiu Zhe         XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, xSpreadsheetDocument);
386*e6e6073dSLiu Zhe         XController xController = xModel.getCurrentController();
387*e6e6073dSLiu Zhe         XSpreadsheetView xSpreadsheetView = (XSpreadsheetView) UnoRuntime.queryInterface(XSpreadsheetView.class, xController);
388*e6e6073dSLiu Zhe         xSpreadsheetView.setActiveSheet(xSpreadsheet);
389*e6e6073dSLiu Zhe     }
390*e6e6073dSLiu Zhe 
391*e6e6073dSLiu Zhe     /**
392*e6e6073dSLiu Zhe      * Get sheet object of current active sheet
393*e6e6073dSLiu Zhe      * @param xSpreadsheetDocument
394*e6e6073dSLiu Zhe      * @return
395*e6e6073dSLiu Zhe      */
396*e6e6073dSLiu Zhe     public static XSpreadsheet getCurrentSheet(XSpreadsheetDocument xSpreadsheetDocument) throws Exception {
397*e6e6073dSLiu Zhe         XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, xSpreadsheetDocument);
398*e6e6073dSLiu Zhe         XController xController = xModel.getCurrentController();
399*e6e6073dSLiu Zhe         XSpreadsheetView xSpreadsheetView = (XSpreadsheetView) UnoRuntime.queryInterface(XSpreadsheetView.class, xController);
400*e6e6073dSLiu Zhe         XSpreadsheet xSpreadsheet = xSpreadsheetView.getActiveSheet();
401*e6e6073dSLiu Zhe 
402*e6e6073dSLiu Zhe         return xSpreadsheet;
403*e6e6073dSLiu Zhe     }
404*e6e6073dSLiu Zhe 
405*e6e6073dSLiu Zhe     /**
406*e6e6073dSLiu Zhe      * Get sheet object by sheet index
407*e6e6073dSLiu Zhe      *
408*e6e6073dSLiu Zhe      * @param xSpreadsheetDocument
409*e6e6073dSLiu Zhe      * @return
410*e6e6073dSLiu Zhe      * @throws Exception
411*e6e6073dSLiu Zhe      */
412*e6e6073dSLiu Zhe     public static String getSCActiveSheetName(
413*e6e6073dSLiu Zhe             XSpreadsheetDocument xSpreadsheetDocument) throws Exception {
414*e6e6073dSLiu Zhe         XModel xSpreadsheetModel = (XModel) UnoRuntime.queryInterface(
415*e6e6073dSLiu Zhe                 XModel.class, xSpreadsheetDocument);
416*e6e6073dSLiu Zhe         XSpreadsheetView xSpeadsheetView = (XSpreadsheetView) UnoRuntime
417*e6e6073dSLiu Zhe                 .queryInterface(XSpreadsheetView.class,
418*e6e6073dSLiu Zhe                         xSpreadsheetModel.getCurrentController());
419*e6e6073dSLiu Zhe         XSpreadsheet activesheet = xSpeadsheetView.getActiveSheet();
420*e6e6073dSLiu Zhe         XNamed activesheetName = (XNamed) UnoRuntime.queryInterface(
421*e6e6073dSLiu Zhe                 XNamed.class, activesheet);
422*e6e6073dSLiu Zhe         return activesheetName.getName();
423*e6e6073dSLiu Zhe     }
424*e6e6073dSLiu Zhe 
425*e6e6073dSLiu Zhe     /**
426*e6e6073dSLiu Zhe      * Set value of specific property from a cell
427*e6e6073dSLiu Zhe      * @param xCell
428*e6e6073dSLiu Zhe      * @param propName
429*e6e6073dSLiu Zhe      * @param value
430*e6e6073dSLiu Zhe      * @throws Exception
431*e6e6073dSLiu Zhe      */
432*e6e6073dSLiu Zhe     public static void setCellProperties(XCell xCell, String propName, Object value) throws Exception {
433*e6e6073dSLiu Zhe 
434*e6e6073dSLiu Zhe         XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xCell);
435*e6e6073dSLiu Zhe         xPropertySet.setPropertyValue(propName, value);
436*e6e6073dSLiu Zhe     }
437*e6e6073dSLiu Zhe 
438*e6e6073dSLiu Zhe     /**
439*e6e6073dSLiu Zhe      * Get value of specific property from a cell
440*e6e6073dSLiu Zhe      * @param xCell
441*e6e6073dSLiu Zhe      * @param propName
442*e6e6073dSLiu Zhe      * @return
443*e6e6073dSLiu Zhe      * @throws Exception
444*e6e6073dSLiu Zhe      */
445*e6e6073dSLiu Zhe     public static Object getCellProperties(XCell xCell, String propName) throws Exception {
446*e6e6073dSLiu Zhe         XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xCell);
447*e6e6073dSLiu Zhe         Object value = xPropertySet.getPropertyValue(propName);
448*e6e6073dSLiu Zhe 
449*e6e6073dSLiu Zhe         return value;
450*e6e6073dSLiu Zhe     }
451*e6e6073dSLiu Zhe 
452*e6e6073dSLiu Zhe     /**
453*e6e6073dSLiu Zhe      * Clear temp file directory
454*e6e6073dSLiu Zhe      */
455*e6e6073dSLiu Zhe     public static void clearTempDir() {
456*e6e6073dSLiu Zhe         FileUtil.deleteFile(Testspace.getFile(Testspace.getPath(scTempDir)));
457*e6e6073dSLiu Zhe     }
458*e6e6073dSLiu Zhe 
459*e6e6073dSLiu Zhe     /**
460*e6e6073dSLiu Zhe      * Save file as specific file format into spreadsheet temp file folder.
461*e6e6073dSLiu Zhe      * @param scComponent
462*e6e6073dSLiu Zhe      * @param fileName  File name string without extension name (e.g. "sampleFile")
463*e6e6073dSLiu Zhe      * @param extName ("ods", "ots", "xls", "xlt", "csv")
464*e6e6073dSLiu Zhe      * @throws Exception
465*e6e6073dSLiu Zhe      */
466*e6e6073dSLiu Zhe     public static void saveFileAs(XComponent scComponent, String fileName, String extName) throws Exception {
467*e6e6073dSLiu Zhe 
468*e6e6073dSLiu Zhe         initFilterName();
469*e6e6073dSLiu Zhe 
470*e6e6073dSLiu Zhe         String storeUrl = Testspace.getUrl(scTempDir + fileName + "." + extName);
471*e6e6073dSLiu Zhe 
472*e6e6073dSLiu Zhe         PropertyValue[] storeProps = new PropertyValue[2];
473*e6e6073dSLiu Zhe         storeProps[0] = new PropertyValue();
474*e6e6073dSLiu Zhe         storeProps[0].Name = "FilterName";
475*e6e6073dSLiu Zhe         storeProps[0].Value = filterName.get(extName);
476*e6e6073dSLiu Zhe         storeProps[1] = new PropertyValue();
477*e6e6073dSLiu Zhe         storeProps[1].Name = "Overwrite";
478*e6e6073dSLiu Zhe         storeProps[1].Value = new Boolean(true);
479*e6e6073dSLiu Zhe 
480*e6e6073dSLiu Zhe         XStorable scStorable =
481*e6e6073dSLiu Zhe                 (XStorable) UnoRuntime.queryInterface(XStorable.class, scComponent);
482*e6e6073dSLiu Zhe         scStorable.storeAsURL(storeUrl, storeProps);
483*e6e6073dSLiu Zhe     }
484*e6e6073dSLiu Zhe 
485*e6e6073dSLiu Zhe     /**
486*e6e6073dSLiu Zhe      * Save file after open file.
487*e6e6073dSLiu Zhe      *
488*e6e6073dSLiu Zhe      * @param xSpreadsheetDocument
489*e6e6073dSLiu Zhe      * @throws Exception
490*e6e6073dSLiu Zhe      */
491*e6e6073dSLiu Zhe     public static void save(XSpreadsheetDocument xSpreadsheetDocument)
492*e6e6073dSLiu Zhe             throws Exception {
493*e6e6073dSLiu Zhe 
494*e6e6073dSLiu Zhe         XStorable scStorable = (XStorable) UnoRuntime.queryInterface(
495*e6e6073dSLiu Zhe                 XStorable.class, xSpreadsheetDocument);
496*e6e6073dSLiu Zhe         scStorable.store();
497*e6e6073dSLiu Zhe 
498*e6e6073dSLiu Zhe     }
499*e6e6073dSLiu Zhe 
500*e6e6073dSLiu Zhe 
501*e6e6073dSLiu Zhe     /**
502*e6e6073dSLiu Zhe      * Close specific opening spreadsheet file which has been saved
503*e6e6073dSLiu Zhe      * @param xSpreadsheetDocument
504*e6e6073dSLiu Zhe      * @throws Exception
505*e6e6073dSLiu Zhe      */
506*e6e6073dSLiu Zhe     public static void closeFile(XSpreadsheetDocument xSpreadsheetDocument) throws Exception {
507*e6e6073dSLiu Zhe         XCloseable xCloseable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, xSpreadsheetDocument);
508*e6e6073dSLiu Zhe         xCloseable.close(false);
509*e6e6073dSLiu Zhe     }
510*e6e6073dSLiu Zhe 
511*e6e6073dSLiu Zhe     /**
512*e6e6073dSLiu Zhe      * Close a opening file saved in spreadsheet temp file direction and reopen it in Spreadsheet. For save&reload test scenario only.
513*e6e6073dSLiu Zhe      * @param unoApp
514*e6e6073dSLiu Zhe      * @param xSpreadsheetDocument
515*e6e6073dSLiu Zhe      * @param fullFileName   File name with the extension name. (e.g. "sc.ods")
516*e6e6073dSLiu Zhe      * @return
517*e6e6073dSLiu Zhe      * @throws Exception
518*e6e6073dSLiu Zhe      */
519*e6e6073dSLiu Zhe     public static XSpreadsheetDocument reloadFile(UnoApp unoApp, XSpreadsheetDocument xSpreadsheetDocument, String fullFileName) throws Exception {
520*e6e6073dSLiu Zhe         closeFile(xSpreadsheetDocument);
521*e6e6073dSLiu Zhe 
522*e6e6073dSLiu Zhe         String filePath = Testspace.getPath(scTempDir + fullFileName);
523*e6e6073dSLiu Zhe         XSpreadsheetDocument xScDocument = UnoRuntime.queryInterface(XSpreadsheetDocument.class, unoApp.loadDocument(filePath));
524*e6e6073dSLiu Zhe 
525*e6e6073dSLiu Zhe         return xScDocument;
526*e6e6073dSLiu Zhe     }
527*e6e6073dSLiu Zhe 
528*e6e6073dSLiu Zhe     /**
529*e6e6073dSLiu Zhe      * Initial the filter name list
530*e6e6073dSLiu Zhe      * @throws Exception
531*e6e6073dSLiu Zhe      */
532*e6e6073dSLiu Zhe     private static void initFilterName() throws Exception {
533*e6e6073dSLiu Zhe         if (filterName.size() > 0) {
534*e6e6073dSLiu Zhe             return;
535*e6e6073dSLiu Zhe         }
536*e6e6073dSLiu Zhe 
537*e6e6073dSLiu Zhe         filterName.put("ods", "calc8");
538*e6e6073dSLiu Zhe         filterName.put("ots", "calc8_template");
539*e6e6073dSLiu Zhe         filterName.put("xls", "MS Excel 97");
540*e6e6073dSLiu Zhe         filterName.put("xlt", "MS Excel 97 Vorlage/Template");
541*e6e6073dSLiu Zhe         filterName.put("csv", "Text - txt - csv (StarCalc)");
542*e6e6073dSLiu Zhe     }
543*e6e6073dSLiu Zhe 
544*e6e6073dSLiu Zhe }
545