12fa2a90fSLiu Zhe /**************************************************************
22fa2a90fSLiu Zhe  *
32fa2a90fSLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
42fa2a90fSLiu Zhe  * or more contributor license agreements.  See the NOTICE file
52fa2a90fSLiu Zhe  * distributed with this work for additional information
62fa2a90fSLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
72fa2a90fSLiu Zhe  * to you under the Apache License, Version 2.0 (the
82fa2a90fSLiu Zhe  * "License"); you may not use this file except in compliance
92fa2a90fSLiu Zhe  * with the License.  You may obtain a copy of the License at
102fa2a90fSLiu Zhe  *
112fa2a90fSLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
122fa2a90fSLiu Zhe  *
132fa2a90fSLiu Zhe  * Unless required by applicable law or agreed to in writing,
142fa2a90fSLiu Zhe  * software distributed under the License is distributed on an
152fa2a90fSLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
162fa2a90fSLiu Zhe  * KIND, either express or implied.  See the License for the
172fa2a90fSLiu Zhe  * specific language governing permissions and limitations
182fa2a90fSLiu Zhe  * under the License.
192fa2a90fSLiu Zhe  *
202fa2a90fSLiu Zhe  *************************************************************/
212fa2a90fSLiu Zhe 
222fa2a90fSLiu Zhe 
232fa2a90fSLiu Zhe package testlib.uno;
242fa2a90fSLiu Zhe 
252fa2a90fSLiu Zhe import java.util.HashMap;
262fa2a90fSLiu Zhe 
271ea6643fSLiu Zhe import org.openoffice.test.common.FileUtil;
282fa2a90fSLiu Zhe import org.openoffice.test.common.Testspace;
292fa2a90fSLiu Zhe import org.openoffice.test.uno.UnoApp;
302fa2a90fSLiu Zhe 
312fa2a90fSLiu Zhe import com.sun.star.beans.PropertyValue;
321ea6643fSLiu Zhe import com.sun.star.beans.XPropertySet;
332fa2a90fSLiu Zhe import com.sun.star.container.XIndexAccess;
34d01630b6SLiu Zhe import com.sun.star.container.XNamed;
352fa2a90fSLiu Zhe import com.sun.star.frame.XController;
362fa2a90fSLiu Zhe import com.sun.star.frame.XModel;
372fa2a90fSLiu Zhe import com.sun.star.frame.XStorable;
382fa2a90fSLiu Zhe import com.sun.star.lang.XComponent;
392fa2a90fSLiu Zhe import com.sun.star.sheet.XSpreadsheet;
402fa2a90fSLiu Zhe import com.sun.star.sheet.XSpreadsheetDocument;
412fa2a90fSLiu Zhe import com.sun.star.sheet.XSpreadsheetView;
422fa2a90fSLiu Zhe import com.sun.star.sheet.XSpreadsheets;
432fa2a90fSLiu Zhe import com.sun.star.table.XCell;
442fa2a90fSLiu Zhe import com.sun.star.table.XCellRange;
452fa2a90fSLiu Zhe import com.sun.star.table.XColumnRowRange;
462fa2a90fSLiu Zhe import com.sun.star.table.XTableColumns;
472fa2a90fSLiu Zhe import com.sun.star.table.XTableRows;
482fa2a90fSLiu Zhe import com.sun.star.text.XText;
492fa2a90fSLiu Zhe import com.sun.star.uno.UnoRuntime;
502fa2a90fSLiu Zhe import com.sun.star.util.XCloseable;
512fa2a90fSLiu Zhe 
522fa2a90fSLiu Zhe 
532fa2a90fSLiu Zhe /**
542fa2a90fSLiu Zhe  * Utilities of Spreadsheet
552fa2a90fSLiu Zhe  *
562fa2a90fSLiu Zhe  */
572fa2a90fSLiu Zhe 
582fa2a90fSLiu Zhe public class SCUtil {
592fa2a90fSLiu Zhe 
601ea6643fSLiu Zhe 	private static final String scTempDir = "output/sc/"; //Spreadsheet temp file directory
612fa2a90fSLiu Zhe 	private static HashMap filterName = new HashMap();
621ea6643fSLiu Zhe 
633908dc91SLiu Zhe 	private SCUtil() {
642fa2a90fSLiu Zhe 
652fa2a90fSLiu Zhe 	}
662fa2a90fSLiu Zhe 
672fa2a90fSLiu Zhe 	/**
682fa2a90fSLiu Zhe 	 * Get spreadsheet document object
692fa2a90fSLiu Zhe 	 * @param xSpreadsheetComponent
702fa2a90fSLiu Zhe 	 * @return
712fa2a90fSLiu Zhe 	 * @throws Exception
722fa2a90fSLiu Zhe 	 */
732fa2a90fSLiu Zhe     public static XSpreadsheetDocument getSCDocument(XComponent xSpreadsheetComponent) throws Exception {
742fa2a90fSLiu Zhe     	XSpreadsheetDocument xSpreadsheetDocument =
752fa2a90fSLiu Zhe         		(XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, xSpreadsheetComponent);
762fa2a90fSLiu Zhe 
772fa2a90fSLiu Zhe         return xSpreadsheetDocument;
782fa2a90fSLiu Zhe     }
792fa2a90fSLiu Zhe 
802fa2a90fSLiu Zhe     /**
812fa2a90fSLiu Zhe      * Get sheet object by sheet name
822fa2a90fSLiu Zhe      * @param xSpreadsheetDocument
832fa2a90fSLiu Zhe      * @param sheetName
842fa2a90fSLiu Zhe      * @return
852fa2a90fSLiu Zhe      * @throws Exception
862fa2a90fSLiu Zhe      */
872fa2a90fSLiu Zhe 	public static XSpreadsheet getSCSheetByName(XSpreadsheetDocument xSpreadsheetDocument, String sheetName) throws Exception {
882fa2a90fSLiu Zhe 		XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
892fa2a90fSLiu Zhe 		XSpreadsheet xSpreadsheet =
902fa2a90fSLiu Zhe 				(XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, xSpreadsheets.getByName(sheetName));
912fa2a90fSLiu Zhe 
922fa2a90fSLiu Zhe 		return xSpreadsheet;
932fa2a90fSLiu Zhe 	}
942fa2a90fSLiu Zhe 
952fa2a90fSLiu Zhe 	/**
962fa2a90fSLiu Zhe 	 * Get sheet object by sheet index
972fa2a90fSLiu Zhe 	 * @param xSpreadsheetDocument
982fa2a90fSLiu Zhe 	 * @param index   (Short) 0,1,2,...
992fa2a90fSLiu Zhe 	 * @return
1002fa2a90fSLiu Zhe 	 * @throws Exception
1012fa2a90fSLiu Zhe 	 */
1022fa2a90fSLiu Zhe 	public static XSpreadsheet getSCSheetByIndex(XSpreadsheetDocument xSpreadsheetDocument, short index) throws Exception {
1032fa2a90fSLiu Zhe 		XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
1042fa2a90fSLiu Zhe 		XIndexAccess xIndexAccess =
1052fa2a90fSLiu Zhe 				(XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
1062fa2a90fSLiu Zhe 		XSpreadsheet xSpreadsheet =
1072fa2a90fSLiu Zhe 				(XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, xIndexAccess.getByIndex(index));
1082fa2a90fSLiu Zhe 
1092fa2a90fSLiu Zhe 		return xSpreadsheet;
1102fa2a90fSLiu Zhe 	}
1112fa2a90fSLiu Zhe 
112d01630b6SLiu Zhe 	/**
113d01630b6SLiu Zhe 	 * Get sheet name by sheet index
114d01630b6SLiu Zhe 	 *
115d01630b6SLiu Zhe 	 * @param xSpreadsheetDocument
116d01630b6SLiu Zhe 	 * @param index
117d01630b6SLiu Zhe 	 *            (Short) 0,1,2,...
118d01630b6SLiu Zhe 	 * @return
119d01630b6SLiu Zhe 	 * @throws Exception
120d01630b6SLiu Zhe 	 */
121d01630b6SLiu Zhe 	public static String getSCSheetNameByIndex(
122d01630b6SLiu Zhe 			XSpreadsheetDocument xSpreadsheetDocument, short index)
123d01630b6SLiu Zhe 			throws Exception {
124d01630b6SLiu Zhe 		XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
125d01630b6SLiu Zhe 		XIndexAccess xIndexAccess = (XIndexAccess) UnoRuntime.queryInterface(
126d01630b6SLiu Zhe 				XIndexAccess.class, xSpreadsheets);
127d01630b6SLiu Zhe 		XSpreadsheet xSpreadsheet = (XSpreadsheet) UnoRuntime.queryInterface(
128d01630b6SLiu Zhe 				XSpreadsheet.class, xIndexAccess.getByIndex(index));
129d01630b6SLiu Zhe 		XNamed xsheetname = (XNamed) UnoRuntime.queryInterface(XNamed.class,
130d01630b6SLiu Zhe 				xSpreadsheet);
131d01630b6SLiu Zhe 		return xsheetname.getName();
132d01630b6SLiu Zhe 	}
133d01630b6SLiu Zhe 
134d01630b6SLiu Zhe 	/**
135d01630b6SLiu Zhe 	 * Set sheet name by sheet index
136d01630b6SLiu Zhe 	 *
137d01630b6SLiu Zhe 	 * @param xSpreadsheetDocument
138d01630b6SLiu Zhe 	 * @param index
139d01630b6SLiu Zhe 	 *            (Short) 0,1,2,...
140d01630b6SLiu Zhe 	 * @return
141d01630b6SLiu Zhe 	 * @throws Exception
142d01630b6SLiu Zhe 	 */
143d01630b6SLiu Zhe 	public static void setSCSheetNameByIndex(
144d01630b6SLiu Zhe 			XSpreadsheetDocument xSpreadsheetDocument, short index,
145d01630b6SLiu Zhe 			String sheetname) throws Exception {
146d01630b6SLiu Zhe 		XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
147d01630b6SLiu Zhe 		XIndexAccess xIndexAccess = (XIndexAccess) UnoRuntime.queryInterface(
148d01630b6SLiu Zhe 				XIndexAccess.class, xSpreadsheets);
149d01630b6SLiu Zhe 		XSpreadsheet xSpreadsheet = (XSpreadsheet) UnoRuntime.queryInterface(
150d01630b6SLiu Zhe 				XSpreadsheet.class, xIndexAccess.getByIndex(index));
151d01630b6SLiu Zhe 		XNamed xsheetname = (XNamed) UnoRuntime.queryInterface(XNamed.class,
152d01630b6SLiu Zhe 				xSpreadsheet);
153d01630b6SLiu Zhe 		xsheetname.setName(sheetname);
154d01630b6SLiu Zhe 	}
155d01630b6SLiu Zhe 
1562fa2a90fSLiu Zhe 	/**
1572fa2a90fSLiu Zhe 	 * Get rows object
1582fa2a90fSLiu Zhe 	 * @param xSpreadsheet
1592fa2a90fSLiu Zhe 	 * @return
1602fa2a90fSLiu Zhe 	 * @throws Exception
1612fa2a90fSLiu Zhe 	 */
1622fa2a90fSLiu Zhe 	public static XTableRows getSCRows(XSpreadsheet xSpreadsheet) throws Exception {
1632fa2a90fSLiu Zhe 		XColumnRowRange xColumnRowRange =
1642fa2a90fSLiu Zhe 				(XColumnRowRange) UnoRuntime.queryInterface(XColumnRowRange.class, xSpreadsheet);
1652fa2a90fSLiu Zhe 		XTableRows xTableRows = xColumnRowRange.getRows();
1662fa2a90fSLiu Zhe 
1672fa2a90fSLiu Zhe 		return xTableRows;
1682fa2a90fSLiu Zhe 	}
1692fa2a90fSLiu Zhe 
1702fa2a90fSLiu Zhe 	/**
1712fa2a90fSLiu Zhe 	 * Get columns object
1722fa2a90fSLiu Zhe 	 * @param xSpreadsheet
1732fa2a90fSLiu Zhe 	 * @return
1742fa2a90fSLiu Zhe 	 * @throws Exception
1752fa2a90fSLiu Zhe 	 */
1762fa2a90fSLiu Zhe 	public static XTableColumns getSCColumns(XSpreadsheet xSpreadsheet) throws Exception {
1772fa2a90fSLiu Zhe 		XColumnRowRange xColumnRowRange =
1782fa2a90fSLiu Zhe 				(XColumnRowRange) UnoRuntime.queryInterface(XColumnRowRange.class, xSpreadsheet);
1792fa2a90fSLiu Zhe 		XTableColumns xTableColumns = xColumnRowRange.getColumns();
1802fa2a90fSLiu Zhe 
1812fa2a90fSLiu Zhe 		return xTableColumns;
1822fa2a90fSLiu Zhe 	}
1832fa2a90fSLiu Zhe 
1842fa2a90fSLiu Zhe 	/**
1852fa2a90fSLiu Zhe 	 * Set floating number into specific cell
1862fa2a90fSLiu Zhe 	 * @param xSpreadsheet
1872fa2a90fSLiu Zhe 	 * @param column
1882fa2a90fSLiu Zhe 	 * @param row
1892fa2a90fSLiu Zhe 	 * @param value
1902fa2a90fSLiu Zhe 	 * @throws Exception
1912fa2a90fSLiu Zhe 	 */
1922fa2a90fSLiu Zhe 	public static void setValueToCell(XSpreadsheet xSpreadsheet, int column, int row, double value) throws Exception {
1932fa2a90fSLiu Zhe 		XCell xCell = xSpreadsheet.getCellByPosition(column, row);
1942fa2a90fSLiu Zhe 		xCell.setValue(value);
1952fa2a90fSLiu Zhe 	}
1962fa2a90fSLiu Zhe 
1972fa2a90fSLiu Zhe 	/**
1982fa2a90fSLiu Zhe 	 * Set text into specific cell
1992fa2a90fSLiu Zhe 	 * @param xSpreadsheet
2002fa2a90fSLiu Zhe 	 * @param column
2012fa2a90fSLiu Zhe 	 * @param row
2022fa2a90fSLiu Zhe 	 * @param text
2032fa2a90fSLiu Zhe 	 * @throws Exception
2042fa2a90fSLiu Zhe 	 */
2052fa2a90fSLiu Zhe 	public static void setTextToCell(XSpreadsheet xSpreadsheet, int column, int row, String text) throws Exception {
2062fa2a90fSLiu Zhe 		XCell xCell = xSpreadsheet.getCellByPosition(column, row);
2072fa2a90fSLiu Zhe 		XText xText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
2082fa2a90fSLiu Zhe 		xText.setString(text);
2092fa2a90fSLiu Zhe 	}
2102fa2a90fSLiu Zhe 
2111ea6643fSLiu Zhe 	/**
2121ea6643fSLiu Zhe 	 * Set text into specific cell
2131ea6643fSLiu Zhe 	 * @param xCell
2141ea6643fSLiu Zhe 	 * @param text
2151ea6643fSLiu Zhe 	 * @throws Exception
2161ea6643fSLiu Zhe 	 */
2171ea6643fSLiu Zhe 	public static void setTextToCell(XCell xCell, String text) throws Exception {
2181ea6643fSLiu Zhe 		XText xText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
2191ea6643fSLiu Zhe 		xText.setString(text);
2201ea6643fSLiu Zhe 	}
2211ea6643fSLiu Zhe 
2222fa2a90fSLiu Zhe 	/**
2232fa2a90fSLiu Zhe 	 * Set formula into specific cell
2242fa2a90fSLiu Zhe 	 * @param xSpreadsheet
2252fa2a90fSLiu Zhe 	 * @param column
2262fa2a90fSLiu Zhe 	 * @param row
2272fa2a90fSLiu Zhe 	 * @param formula
2282fa2a90fSLiu Zhe 	 * @throws Exception
2292fa2a90fSLiu Zhe 	 */
2302fa2a90fSLiu Zhe 	public static void setFormulaToCell(XSpreadsheet xSpreadsheet, int column, int row, String formula) throws Exception {
2312fa2a90fSLiu Zhe 		XCell xCell = xSpreadsheet.getCellByPosition(column, row);
2322fa2a90fSLiu Zhe 		xCell.setFormula(formula);
2332fa2a90fSLiu Zhe 	}
2342fa2a90fSLiu Zhe 
2352fa2a90fSLiu Zhe 	/**
2362fa2a90fSLiu Zhe 	 * Get value from specific cell
2372fa2a90fSLiu Zhe 	 * @param xSpreadsheet
2382fa2a90fSLiu Zhe 	 * @param column
2392fa2a90fSLiu Zhe 	 * @param row
2402fa2a90fSLiu Zhe 	 * @return
2412fa2a90fSLiu Zhe 	 * @throws Exception
2422fa2a90fSLiu Zhe 	 */
2432fa2a90fSLiu Zhe 	public static double getValueFromCell(XSpreadsheet xSpreadsheet, int column, int row) throws Exception {
2442fa2a90fSLiu Zhe 		XCell xCell = xSpreadsheet.getCellByPosition(column, row);
2452fa2a90fSLiu Zhe 		double cellValue = xCell.getValue();
2462fa2a90fSLiu Zhe 
2472fa2a90fSLiu Zhe 		return cellValue;
2482fa2a90fSLiu Zhe 	}
2492fa2a90fSLiu Zhe 
2502fa2a90fSLiu Zhe 	/**
2512fa2a90fSLiu Zhe 	 * Get text from specific cell
2522fa2a90fSLiu Zhe 	 * @param xSpreadsheet
2532fa2a90fSLiu Zhe 	 * @param column
2542fa2a90fSLiu Zhe 	 * @param row
2552fa2a90fSLiu Zhe 	 *
2562fa2a90fSLiu Zhe 	 * @return
2572fa2a90fSLiu Zhe 	 * @throws Exception
2582fa2a90fSLiu Zhe 	 */
2592fa2a90fSLiu Zhe 	public static String getTextFromCell(XSpreadsheet xSpreadsheet, int column, int row) throws Exception {
2602fa2a90fSLiu Zhe 		XCell xCell = xSpreadsheet.getCellByPosition(column, row);
2612fa2a90fSLiu Zhe 		XText xText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
2622fa2a90fSLiu Zhe 
2632fa2a90fSLiu Zhe 		return xText.getString();
2642fa2a90fSLiu Zhe 	}
2652fa2a90fSLiu Zhe 
2662fa2a90fSLiu Zhe 	/**
2672fa2a90fSLiu Zhe 	 * Get formula string from specific cell
2682fa2a90fSLiu Zhe 	 * @param xSpreadsheet
2692fa2a90fSLiu Zhe 	 * @param column
2702fa2a90fSLiu Zhe 	 * @param row
2712fa2a90fSLiu Zhe 	 * @return
2722fa2a90fSLiu Zhe 	 * @throws Exception
2732fa2a90fSLiu Zhe 	 */
2742fa2a90fSLiu Zhe 	public static String getFormulaFromCell(XSpreadsheet xSpreadsheet, int column, int row) throws Exception {
2752fa2a90fSLiu Zhe 		XCell xCell = xSpreadsheet.getCellByPosition(column, row);
2762fa2a90fSLiu Zhe 		String cellFormula = xCell.getFormula();
2772fa2a90fSLiu Zhe 
2782fa2a90fSLiu Zhe 		return cellFormula;
2792fa2a90fSLiu Zhe 	}
2802fa2a90fSLiu Zhe 
2812fa2a90fSLiu Zhe 	/**
2822fa2a90fSLiu Zhe 	 * Set numbers into a cell range
2832fa2a90fSLiu Zhe 	 * @param xSpreadsheet
2842fa2a90fSLiu Zhe 	 * @param start_col
2852fa2a90fSLiu Zhe 	 * @param start_row
2862fa2a90fSLiu Zhe 	 * @param end_col
2872fa2a90fSLiu Zhe 	 * @param end_row
2882fa2a90fSLiu Zhe 	 * @param values
2892fa2a90fSLiu Zhe 	 * @throws Exception
2902fa2a90fSLiu Zhe 	 */
2912fa2a90fSLiu Zhe 	public static void setValueToCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row,  double[][] values) throws Exception {
2922fa2a90fSLiu Zhe 		XCellRange xCellRange = xSpreadsheet.getCellRangeByPosition(start_col, start_row, end_col, end_row);
2932fa2a90fSLiu Zhe 		XCell xCell = null;
2942fa2a90fSLiu Zhe 		for (int i = 0; i <= (end_row - start_row); i++ ) {
2952fa2a90fSLiu Zhe 			for(int j = 0; j <= (end_col - start_col); j++) {
2962fa2a90fSLiu Zhe 				xCell = xCellRange.getCellByPosition(j, i);
2972fa2a90fSLiu Zhe 				xCell.setValue(values[i][j]);
2982fa2a90fSLiu Zhe 			}
2992fa2a90fSLiu Zhe 		}
3002fa2a90fSLiu Zhe 	}
3012fa2a90fSLiu Zhe 
3022fa2a90fSLiu Zhe 	/**
3032fa2a90fSLiu Zhe 	 * Set text into a cell range
3042fa2a90fSLiu Zhe 	 * @param xSpreadsheet
3052fa2a90fSLiu Zhe 	 * @param start_col
3062fa2a90fSLiu Zhe 	 * @param start_row
3072fa2a90fSLiu Zhe 	 * @param end_col
3082fa2a90fSLiu Zhe 	 * @param end_row
3092fa2a90fSLiu Zhe 	 * @param texts
3102fa2a90fSLiu Zhe 	 * @throws Exception
3112fa2a90fSLiu Zhe 	 */
3122fa2a90fSLiu Zhe 	public static void setTextToCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row,  String[][] texts) throws Exception {
3132fa2a90fSLiu Zhe 		XCellRange xCellRange = xSpreadsheet.getCellRangeByPosition(start_col, start_row, end_col, end_row);
3142fa2a90fSLiu Zhe 		XCell xCell = null;
3152fa2a90fSLiu Zhe 		XText xText = null;
3162fa2a90fSLiu Zhe 		for (int i = 0; i <= (end_row - start_row); i++ ) {
3172fa2a90fSLiu Zhe 			for(int j = 0; j <= (end_col - start_col); j++) {
3182fa2a90fSLiu Zhe 				xCell = xCellRange.getCellByPosition(j, i);
3192fa2a90fSLiu Zhe 				xText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
3202fa2a90fSLiu Zhe 				xText.setString(texts[i][j]);
3212fa2a90fSLiu Zhe 			}
3222fa2a90fSLiu Zhe 		}
3232fa2a90fSLiu Zhe 	}
3242fa2a90fSLiu Zhe 
3252fa2a90fSLiu Zhe 	/**
3262fa2a90fSLiu Zhe 	 * Get number content from a cell range
3272fa2a90fSLiu Zhe 	 * @param xSpreadsheet
3282fa2a90fSLiu Zhe 	 * @param start_col
3292fa2a90fSLiu Zhe 	 * @param start_row
3302fa2a90fSLiu Zhe 	 * @param end_col
3312fa2a90fSLiu Zhe 	 * @param end_row
3322fa2a90fSLiu Zhe 	 * @return
3332fa2a90fSLiu Zhe 	 * @throws Exception
3342fa2a90fSLiu Zhe 	 */
3352fa2a90fSLiu Zhe 	public static double[][] getValueFromCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row) throws Exception {
3362fa2a90fSLiu Zhe 		XCellRange xCellRange = xSpreadsheet.getCellRangeByPosition(start_col, start_row, end_col, end_row);
3372fa2a90fSLiu Zhe 		XCell xCell = null;
3382fa2a90fSLiu Zhe 		double[][] cellValues = new double[end_row - start_row+1][end_col - start_col +1];
3392fa2a90fSLiu Zhe 
3402fa2a90fSLiu Zhe 		for (int i = 0; i <= (end_row - start_row); i++ ) {
3412fa2a90fSLiu Zhe 			for(int j = 0; j <= (end_col - start_col); j++) {
3422fa2a90fSLiu Zhe 				xCell = xCellRange.getCellByPosition(j, i);
3432fa2a90fSLiu Zhe 				cellValues[i][j] = xCell.getValue();
3442fa2a90fSLiu Zhe 			}
3452fa2a90fSLiu Zhe 		}
3462fa2a90fSLiu Zhe 
3472fa2a90fSLiu Zhe 		return cellValues;
3482fa2a90fSLiu Zhe 	}
3492fa2a90fSLiu Zhe 
3502fa2a90fSLiu Zhe 	/**
3512fa2a90fSLiu Zhe 	 * Get text content from a cell range
3522fa2a90fSLiu Zhe 	 * @param xSpreadsheet
3532fa2a90fSLiu Zhe 	 * @param start_col
3542fa2a90fSLiu Zhe 	 * @param start_row
3552fa2a90fSLiu Zhe 	 * @param end_col
3562fa2a90fSLiu Zhe 	 * @param end_row
3572fa2a90fSLiu Zhe 	 * @return
3582fa2a90fSLiu Zhe 	 * @throws Exception
3592fa2a90fSLiu Zhe 	 */
3602fa2a90fSLiu Zhe 	public static String[][] getTextFromCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row) throws Exception {
3612fa2a90fSLiu Zhe 		XCellRange xCellRange = xSpreadsheet.getCellRangeByPosition(start_col, start_row, end_col, end_row);
3622fa2a90fSLiu Zhe 		XCell xCell = null;
3632fa2a90fSLiu Zhe 		XText xText = null;
3642fa2a90fSLiu Zhe 		String[][] cellTexts = new String[end_row - start_row+1][end_col - start_col +1];
3652fa2a90fSLiu Zhe 
3662fa2a90fSLiu Zhe 		for (int i = 0; i <= (end_row - start_row); i++ ) {
3671ea6643fSLiu Zhe 			for (int j = 0; j <= (end_col - start_col); j++) {
3682fa2a90fSLiu Zhe 				xCell = xCellRange.getCellByPosition(j, i);
3692fa2a90fSLiu Zhe 				xText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
3702fa2a90fSLiu Zhe 				cellTexts[i][j] = xText.getString();
3712fa2a90fSLiu Zhe 			}
3722fa2a90fSLiu Zhe 		}
3732fa2a90fSLiu Zhe 
3742fa2a90fSLiu Zhe 		return cellTexts;
3752fa2a90fSLiu Zhe 	}
3762fa2a90fSLiu Zhe 
3772fa2a90fSLiu Zhe 	//TODO ZS - public static String[][] getAllFromCellRange
3782fa2a90fSLiu Zhe 
3792fa2a90fSLiu Zhe 	/**
3802fa2a90fSLiu Zhe 	 * Switch to specific sheet
3812fa2a90fSLiu Zhe 	 * @param xSpreadsheetDocument
3822fa2a90fSLiu Zhe 	 * @param xSpreadsheet
3832fa2a90fSLiu Zhe 	 */
3842fa2a90fSLiu Zhe 	public static void setCurrentSheet(XSpreadsheetDocument xSpreadsheetDocument, XSpreadsheet xSpreadsheet) throws Exception {
3852fa2a90fSLiu Zhe 		XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, xSpreadsheetDocument);
3862fa2a90fSLiu Zhe 		XController xController = xModel.getCurrentController();
3872fa2a90fSLiu Zhe 		XSpreadsheetView xSpreadsheetView = (XSpreadsheetView) UnoRuntime.queryInterface(XSpreadsheetView.class, xController);
3882fa2a90fSLiu Zhe 		xSpreadsheetView.setActiveSheet(xSpreadsheet);
3892fa2a90fSLiu Zhe 	}
3902fa2a90fSLiu Zhe 
3912fa2a90fSLiu Zhe 	/**
3922fa2a90fSLiu Zhe 	 * Get sheet object of current active sheet
3932fa2a90fSLiu Zhe 	 * @param xSpreadsheetDocument
3942fa2a90fSLiu Zhe 	 * @return
3952fa2a90fSLiu Zhe 	 */
3962fa2a90fSLiu Zhe 	public static XSpreadsheet getCurrentSheet(XSpreadsheetDocument xSpreadsheetDocument) throws Exception {
3972fa2a90fSLiu Zhe 		XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, xSpreadsheetDocument);
3982fa2a90fSLiu Zhe 		XController xController = xModel.getCurrentController();
3992fa2a90fSLiu Zhe 		XSpreadsheetView xSpreadsheetView = (XSpreadsheetView) UnoRuntime.queryInterface(XSpreadsheetView.class, xController);
4002fa2a90fSLiu Zhe 		XSpreadsheet xSpreadsheet = xSpreadsheetView.getActiveSheet();
4012fa2a90fSLiu Zhe 
4022fa2a90fSLiu Zhe 		return xSpreadsheet;
4032fa2a90fSLiu Zhe 	}
4042fa2a90fSLiu Zhe 
405d01630b6SLiu Zhe 	/**
406d01630b6SLiu Zhe 	 * Get sheet object by sheet index
407d01630b6SLiu Zhe 	 *
408d01630b6SLiu Zhe 	 * @param xSpreadsheetDocument
409d01630b6SLiu Zhe 	 * @return
410d01630b6SLiu Zhe 	 * @throws Exception
411d01630b6SLiu Zhe 	 */
412d01630b6SLiu Zhe 	public static String getSCActiveSheetName(
413d01630b6SLiu Zhe 			XSpreadsheetDocument xSpreadsheetDocument) throws Exception {
414d01630b6SLiu Zhe 		XModel xSpreadsheetModel = (XModel) UnoRuntime.queryInterface(
415d01630b6SLiu Zhe 				XModel.class, xSpreadsheetDocument);
416d01630b6SLiu Zhe 		XSpreadsheetView xSpeadsheetView = (XSpreadsheetView) UnoRuntime
417d01630b6SLiu Zhe 				.queryInterface(XSpreadsheetView.class,
418d01630b6SLiu Zhe 						xSpreadsheetModel.getCurrentController());
419d01630b6SLiu Zhe 		XSpreadsheet activesheet = xSpeadsheetView.getActiveSheet();
420d01630b6SLiu Zhe 		XNamed activesheetName = (XNamed) UnoRuntime.queryInterface(
421d01630b6SLiu Zhe 				XNamed.class, activesheet);
422d01630b6SLiu Zhe 		return activesheetName.getName();
423d01630b6SLiu Zhe 	}
424d01630b6SLiu Zhe 
4252fa2a90fSLiu Zhe 	/**
4261ea6643fSLiu Zhe 	 * Set value of specific property from a cell
4271ea6643fSLiu Zhe 	 * @param xCell
4281ea6643fSLiu Zhe 	 * @param propName
4291ea6643fSLiu Zhe 	 * @param value
4301ea6643fSLiu Zhe 	 * @throws Exception
4311ea6643fSLiu Zhe 	 */
4321ea6643fSLiu Zhe 	public static void setCellProperties(XCell xCell, String propName, Object value) throws Exception {
4331ea6643fSLiu Zhe 
4341ea6643fSLiu Zhe 		XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xCell);
4351ea6643fSLiu Zhe 		xPropertySet.setPropertyValue(propName, value);
4361ea6643fSLiu Zhe 	}
4371ea6643fSLiu Zhe 
4381ea6643fSLiu Zhe 	/**
4391ea6643fSLiu Zhe 	 * Get value of specific property from a cell
4401ea6643fSLiu Zhe 	 * @param xCell
4411ea6643fSLiu Zhe 	 * @param propName
4421ea6643fSLiu Zhe 	 * @return
4431ea6643fSLiu Zhe 	 * @throws Exception
4441ea6643fSLiu Zhe 	 */
4451ea6643fSLiu Zhe 	public static Object getCellProperties(XCell xCell, String propName) throws Exception {
4461ea6643fSLiu Zhe 		XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xCell);
4471ea6643fSLiu Zhe 		Object value = xPropertySet.getPropertyValue(propName);
4481ea6643fSLiu Zhe 
4491ea6643fSLiu Zhe 		return value;
4501ea6643fSLiu Zhe 	}
4511ea6643fSLiu Zhe 
4521ea6643fSLiu Zhe 	/**
4531ea6643fSLiu Zhe 	 * Clear temp file directory
4541ea6643fSLiu Zhe 	 */
4551ea6643fSLiu Zhe 	public static void clearTempDir() {
4561ea6643fSLiu Zhe 		FileUtil.deleteFile(Testspace.getFile(Testspace.getPath(scTempDir)));
4571ea6643fSLiu Zhe 	}
4581ea6643fSLiu Zhe 
4591ea6643fSLiu Zhe 	/**
4601ea6643fSLiu Zhe 	 * Save file as specific file format into spreadsheet temp file folder.
4612fa2a90fSLiu Zhe 	 * @param scComponent
4622fa2a90fSLiu Zhe 	 * @param fileName  File name string without extension name (e.g. "sampleFile")
4632fa2a90fSLiu Zhe 	 * @param extName ("ods", "ots", "xls", "xlt", "csv")
4642fa2a90fSLiu Zhe 	 * @throws Exception
4652fa2a90fSLiu Zhe 	 */
4662fa2a90fSLiu Zhe 	public static void saveFileAs(XComponent scComponent, String fileName, String extName) throws Exception {
4672fa2a90fSLiu Zhe 
4682fa2a90fSLiu Zhe 		initFilterName();
4692fa2a90fSLiu Zhe 
4701ea6643fSLiu Zhe 		String storeUrl = Testspace.getUrl(scTempDir + fileName + "." + extName);
4712fa2a90fSLiu Zhe 
4722fa2a90fSLiu Zhe 		PropertyValue[] storeProps = new PropertyValue[2];
4732fa2a90fSLiu Zhe 		storeProps[0] = new PropertyValue();
4742fa2a90fSLiu Zhe 		storeProps[0].Name = "FilterName";
4752fa2a90fSLiu Zhe 		storeProps[0].Value = filterName.get(extName);
4762fa2a90fSLiu Zhe 		storeProps[1] = new PropertyValue();
4772fa2a90fSLiu Zhe 		storeProps[1].Name = "Overwrite";
4782fa2a90fSLiu Zhe 		storeProps[1].Value = new Boolean(true);
4792fa2a90fSLiu Zhe 
4802fa2a90fSLiu Zhe 		XStorable scStorable =
4812fa2a90fSLiu Zhe 				(XStorable) UnoRuntime.queryInterface(XStorable.class, scComponent);
4822fa2a90fSLiu Zhe 		scStorable.storeAsURL(storeUrl, storeProps);
4832fa2a90fSLiu Zhe 	}
4842fa2a90fSLiu Zhe 
485d01630b6SLiu Zhe 	/**
486d01630b6SLiu Zhe 	 * Save file after open file.
487d01630b6SLiu Zhe 	 *
488d01630b6SLiu Zhe 	 * @param xSpreadsheetDocument
489d01630b6SLiu Zhe 	 * @throws Exception
490d01630b6SLiu Zhe 	 */
491d01630b6SLiu Zhe 	public static void save(XSpreadsheetDocument xSpreadsheetDocument)
492d01630b6SLiu Zhe 			throws Exception {
493d01630b6SLiu Zhe 
494d01630b6SLiu Zhe 		XStorable scStorable = (XStorable) UnoRuntime.queryInterface(
495d01630b6SLiu Zhe 				XStorable.class, xSpreadsheetDocument);
496d01630b6SLiu Zhe 		scStorable.store();
497d01630b6SLiu Zhe 
498d01630b6SLiu Zhe 	}
499d01630b6SLiu Zhe 
500d01630b6SLiu Zhe 
5012fa2a90fSLiu Zhe 	/**
5022fa2a90fSLiu Zhe 	 * Close specific opening spreadsheet file which has been saved
5032fa2a90fSLiu Zhe 	 * @param xSpreadsheetDocument
5042fa2a90fSLiu Zhe 	 * @throws Exception
5052fa2a90fSLiu Zhe 	 */
5062fa2a90fSLiu Zhe 	public static void closeFile(XSpreadsheetDocument xSpreadsheetDocument) throws Exception {
5072fa2a90fSLiu Zhe 		XCloseable xCloseable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, xSpreadsheetDocument);
5082fa2a90fSLiu Zhe 		xCloseable.close(false);
5092fa2a90fSLiu Zhe 	}
5102fa2a90fSLiu Zhe 
5112fa2a90fSLiu Zhe 	/**
5121ea6643fSLiu Zhe 	 * Close a opening file saved in spreadsheet temp file direction and reopen it in Spreadsheet. For save&reload test scenario only.
5132fa2a90fSLiu Zhe 	 * @param unoApp
5142fa2a90fSLiu Zhe 	 * @param xSpreadsheetDocument
5152fa2a90fSLiu Zhe 	 * @param fullFileName   File name with the extension name. (e.g. "sc.ods")
5162fa2a90fSLiu Zhe 	 * @return
5172fa2a90fSLiu Zhe 	 * @throws Exception
5182fa2a90fSLiu Zhe 	 */
5192fa2a90fSLiu Zhe 	public static XSpreadsheetDocument reloadFile(UnoApp unoApp, XSpreadsheetDocument xSpreadsheetDocument, String fullFileName) throws Exception {
5202fa2a90fSLiu Zhe 		closeFile(xSpreadsheetDocument);
5212fa2a90fSLiu Zhe 
5221ea6643fSLiu Zhe 		String filePath = Testspace.getPath(scTempDir + fullFileName);
5232fa2a90fSLiu Zhe 		XSpreadsheetDocument xScDocument = UnoRuntime.queryInterface(XSpreadsheetDocument.class, unoApp.loadDocument(filePath));
5242fa2a90fSLiu Zhe 
5252fa2a90fSLiu Zhe 		return xScDocument;
5262fa2a90fSLiu Zhe 	}
5272fa2a90fSLiu Zhe 
528*9fdcf9fdSLiu Zhe 	/**
529*9fdcf9fdSLiu Zhe 	 * open file in Spreadsheet.
530*9fdcf9fdSLiu Zhe 	 * @param unoApp
531*9fdcf9fdSLiu Zhe 	 * @param filtpath   File path with the extension name. (e.g. "testcase/uno/sc/data/sample.xls")
532*9fdcf9fdSLiu Zhe 	 * @return
533*9fdcf9fdSLiu Zhe 	 * @throws Exception
534*9fdcf9fdSLiu Zhe 	 */
535*9fdcf9fdSLiu Zhe 	public static XSpreadsheetDocument openFile(String filePath, UnoApp app) throws Exception {
536*9fdcf9fdSLiu Zhe 		return (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, app.loadDocument(filePath));
537*9fdcf9fdSLiu Zhe 	}
538*9fdcf9fdSLiu Zhe 
5392fa2a90fSLiu Zhe 	/**
5402fa2a90fSLiu Zhe 	 * Initial the filter name list
5412fa2a90fSLiu Zhe 	 * @throws Exception
5422fa2a90fSLiu Zhe 	 */
5432fa2a90fSLiu Zhe 	private static void initFilterName() throws Exception {
5442fa2a90fSLiu Zhe 		if (filterName.size() > 0) {
5452fa2a90fSLiu Zhe 			return;
5462fa2a90fSLiu Zhe 		}
5472fa2a90fSLiu Zhe 
5482fa2a90fSLiu Zhe 		filterName.put("ods", "calc8");
5492fa2a90fSLiu Zhe 		filterName.put("ots", "calc8_template");
5502fa2a90fSLiu Zhe 		filterName.put("xls", "MS Excel 97");
5512fa2a90fSLiu Zhe 		filterName.put("xlt", "MS Excel 97 Vorlage/Template");
5522fa2a90fSLiu Zhe 		filterName.put("csv", "Text - txt - csv (StarCalc)");
5532fa2a90fSLiu Zhe 	}
5542fa2a90fSLiu Zhe 
5552fa2a90fSLiu Zhe }
556