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