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 
311dfd36f5SLiu Zhe import com.sun.star.awt.Rectangle;
322fa2a90fSLiu Zhe import com.sun.star.beans.PropertyValue;
331ea6643fSLiu Zhe import com.sun.star.beans.XPropertySet;
341dfd36f5SLiu Zhe import com.sun.star.chart.XChartDocument;
351dfd36f5SLiu Zhe import com.sun.star.chart.XDiagram;
362fa2a90fSLiu Zhe import com.sun.star.container.XIndexAccess;
371dfd36f5SLiu Zhe import com.sun.star.container.XNameAccess;
38d01630b6SLiu Zhe import com.sun.star.container.XNamed;
391dfd36f5SLiu Zhe import com.sun.star.document.XEmbeddedObjectSupplier;
402fa2a90fSLiu Zhe import com.sun.star.frame.XController;
412fa2a90fSLiu Zhe import com.sun.star.frame.XModel;
422fa2a90fSLiu Zhe import com.sun.star.frame.XStorable;
432fa2a90fSLiu Zhe import com.sun.star.lang.XComponent;
441dfd36f5SLiu Zhe import com.sun.star.lang.XMultiServiceFactory;
451dfd36f5SLiu Zhe import com.sun.star.sheet.XCellRangeAddressable;
462fa2a90fSLiu Zhe import com.sun.star.sheet.XSpreadsheet;
472fa2a90fSLiu Zhe import com.sun.star.sheet.XSpreadsheetDocument;
482fa2a90fSLiu Zhe import com.sun.star.sheet.XSpreadsheetView;
492fa2a90fSLiu Zhe import com.sun.star.sheet.XSpreadsheets;
501dfd36f5SLiu Zhe import com.sun.star.table.CellRangeAddress;
512fa2a90fSLiu Zhe import com.sun.star.table.XCell;
522fa2a90fSLiu Zhe import com.sun.star.table.XCellRange;
532fa2a90fSLiu Zhe import com.sun.star.table.XColumnRowRange;
541dfd36f5SLiu Zhe import com.sun.star.table.XTableChart;
551dfd36f5SLiu Zhe import com.sun.star.table.XTableCharts;
561dfd36f5SLiu Zhe import com.sun.star.table.XTableChartsSupplier;
572fa2a90fSLiu Zhe import com.sun.star.table.XTableColumns;
582fa2a90fSLiu Zhe import com.sun.star.table.XTableRows;
592fa2a90fSLiu Zhe import com.sun.star.text.XText;
602fa2a90fSLiu Zhe import com.sun.star.uno.UnoRuntime;
612fa2a90fSLiu Zhe import com.sun.star.util.XCloseable;
622fa2a90fSLiu Zhe 
632fa2a90fSLiu Zhe 
642fa2a90fSLiu Zhe /**
652fa2a90fSLiu Zhe  * Utilities of Spreadsheet
662fa2a90fSLiu Zhe  *
672fa2a90fSLiu Zhe  */
682fa2a90fSLiu Zhe 
692fa2a90fSLiu Zhe public class SCUtil {
702fa2a90fSLiu Zhe 
711ea6643fSLiu Zhe 	private static final String scTempDir = "output/sc/"; //Spreadsheet temp file directory
722fa2a90fSLiu Zhe 	private static HashMap filterName = new HashMap();
731ea6643fSLiu Zhe 
SCUtil()743908dc91SLiu Zhe 	private SCUtil() {
752fa2a90fSLiu Zhe 
762fa2a90fSLiu Zhe 	}
772fa2a90fSLiu Zhe 
782fa2a90fSLiu Zhe 	/**
792fa2a90fSLiu Zhe 	 * Get spreadsheet document object
802fa2a90fSLiu Zhe 	 * @param xSpreadsheetComponent
812fa2a90fSLiu Zhe 	 * @return
822fa2a90fSLiu Zhe 	 * @throws Exception
832fa2a90fSLiu Zhe 	 */
getSCDocument(XComponent xSpreadsheetComponent)842fa2a90fSLiu Zhe     public static XSpreadsheetDocument getSCDocument(XComponent xSpreadsheetComponent) throws Exception {
852fa2a90fSLiu Zhe     	XSpreadsheetDocument xSpreadsheetDocument =
862fa2a90fSLiu Zhe         		(XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, xSpreadsheetComponent);
872fa2a90fSLiu Zhe 
882fa2a90fSLiu Zhe         return xSpreadsheetDocument;
892fa2a90fSLiu Zhe     }
902fa2a90fSLiu Zhe 
912fa2a90fSLiu Zhe     /**
922fa2a90fSLiu Zhe      * Get sheet object by sheet name
932fa2a90fSLiu Zhe      * @param xSpreadsheetDocument
942fa2a90fSLiu Zhe      * @param sheetName
952fa2a90fSLiu Zhe      * @return
962fa2a90fSLiu Zhe      * @throws Exception
972fa2a90fSLiu Zhe      */
getSCSheetByName(XSpreadsheetDocument xSpreadsheetDocument, String sheetName)982fa2a90fSLiu Zhe 	public static XSpreadsheet getSCSheetByName(XSpreadsheetDocument xSpreadsheetDocument, String sheetName) throws Exception {
992fa2a90fSLiu Zhe 		XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
1002fa2a90fSLiu Zhe 		XSpreadsheet xSpreadsheet =
1012fa2a90fSLiu Zhe 				(XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, xSpreadsheets.getByName(sheetName));
1022fa2a90fSLiu Zhe 
1032fa2a90fSLiu Zhe 		return xSpreadsheet;
1042fa2a90fSLiu Zhe 	}
1052fa2a90fSLiu Zhe 
1062fa2a90fSLiu Zhe 	/**
1072fa2a90fSLiu Zhe 	 * Get sheet object by sheet index
1082fa2a90fSLiu Zhe 	 * @param xSpreadsheetDocument
1092fa2a90fSLiu Zhe 	 * @param index   (Short) 0,1,2,...
1102fa2a90fSLiu Zhe 	 * @return
1112fa2a90fSLiu Zhe 	 * @throws Exception
1122fa2a90fSLiu Zhe 	 */
getSCSheetByIndex(XSpreadsheetDocument xSpreadsheetDocument, short index)1132fa2a90fSLiu Zhe 	public static XSpreadsheet getSCSheetByIndex(XSpreadsheetDocument xSpreadsheetDocument, short index) throws Exception {
1142fa2a90fSLiu Zhe 		XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
1152fa2a90fSLiu Zhe 		XIndexAccess xIndexAccess =
1162fa2a90fSLiu Zhe 				(XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
1172fa2a90fSLiu Zhe 		XSpreadsheet xSpreadsheet =
1182fa2a90fSLiu Zhe 				(XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, xIndexAccess.getByIndex(index));
1192fa2a90fSLiu Zhe 
1202fa2a90fSLiu Zhe 		return xSpreadsheet;
1212fa2a90fSLiu Zhe 	}
1222fa2a90fSLiu Zhe 
123d01630b6SLiu Zhe 	/**
124d01630b6SLiu Zhe 	 * Get sheet name by sheet index
125d01630b6SLiu Zhe 	 *
126d01630b6SLiu Zhe 	 * @param xSpreadsheetDocument
127d01630b6SLiu Zhe 	 * @param index
128d01630b6SLiu Zhe 	 *            (Short) 0,1,2,...
129d01630b6SLiu Zhe 	 * @return
130d01630b6SLiu Zhe 	 * @throws Exception
131d01630b6SLiu Zhe 	 */
getSCSheetNameByIndex( XSpreadsheetDocument xSpreadsheetDocument, short index)132d01630b6SLiu Zhe 	public static String getSCSheetNameByIndex(
133d01630b6SLiu Zhe 			XSpreadsheetDocument xSpreadsheetDocument, short index)
134d01630b6SLiu Zhe 			throws Exception {
135d01630b6SLiu Zhe 		XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
136d01630b6SLiu Zhe 		XIndexAccess xIndexAccess = (XIndexAccess) UnoRuntime.queryInterface(
137d01630b6SLiu Zhe 				XIndexAccess.class, xSpreadsheets);
138d01630b6SLiu Zhe 		XSpreadsheet xSpreadsheet = (XSpreadsheet) UnoRuntime.queryInterface(
139d01630b6SLiu Zhe 				XSpreadsheet.class, xIndexAccess.getByIndex(index));
140d01630b6SLiu Zhe 		XNamed xsheetname = (XNamed) UnoRuntime.queryInterface(XNamed.class,
141d01630b6SLiu Zhe 				xSpreadsheet);
142d01630b6SLiu Zhe 		return xsheetname.getName();
143d01630b6SLiu Zhe 	}
144d01630b6SLiu Zhe 
145d01630b6SLiu Zhe 	/**
146d01630b6SLiu Zhe 	 * Set sheet name by sheet index
147d01630b6SLiu Zhe 	 *
148d01630b6SLiu Zhe 	 * @param xSpreadsheetDocument
149d01630b6SLiu Zhe 	 * @param index
150d01630b6SLiu Zhe 	 *            (Short) 0,1,2,...
151d01630b6SLiu Zhe 	 * @return
152d01630b6SLiu Zhe 	 * @throws Exception
153d01630b6SLiu Zhe 	 */
setSCSheetNameByIndex( XSpreadsheetDocument xSpreadsheetDocument, short index, String sheetname)154d01630b6SLiu Zhe 	public static void setSCSheetNameByIndex(
155d01630b6SLiu Zhe 			XSpreadsheetDocument xSpreadsheetDocument, short index,
156d01630b6SLiu Zhe 			String sheetname) throws Exception {
157d01630b6SLiu Zhe 		XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
158d01630b6SLiu Zhe 		XIndexAccess xIndexAccess = (XIndexAccess) UnoRuntime.queryInterface(
159d01630b6SLiu Zhe 				XIndexAccess.class, xSpreadsheets);
160d01630b6SLiu Zhe 		XSpreadsheet xSpreadsheet = (XSpreadsheet) UnoRuntime.queryInterface(
161d01630b6SLiu Zhe 				XSpreadsheet.class, xIndexAccess.getByIndex(index));
162d01630b6SLiu Zhe 		XNamed xsheetname = (XNamed) UnoRuntime.queryInterface(XNamed.class,
163d01630b6SLiu Zhe 				xSpreadsheet);
164d01630b6SLiu Zhe 		xsheetname.setName(sheetname);
165d01630b6SLiu Zhe 	}
166d01630b6SLiu Zhe 
1672fa2a90fSLiu Zhe 	/**
1682fa2a90fSLiu Zhe 	 * Get rows object
1692fa2a90fSLiu Zhe 	 * @param xSpreadsheet
1702fa2a90fSLiu Zhe 	 * @return
1712fa2a90fSLiu Zhe 	 * @throws Exception
1722fa2a90fSLiu Zhe 	 */
getSCRows(XSpreadsheet xSpreadsheet)1732fa2a90fSLiu Zhe 	public static XTableRows getSCRows(XSpreadsheet xSpreadsheet) throws Exception {
1742fa2a90fSLiu Zhe 		XColumnRowRange xColumnRowRange =
1752fa2a90fSLiu Zhe 				(XColumnRowRange) UnoRuntime.queryInterface(XColumnRowRange.class, xSpreadsheet);
1762fa2a90fSLiu Zhe 		XTableRows xTableRows = xColumnRowRange.getRows();
1772fa2a90fSLiu Zhe 
1782fa2a90fSLiu Zhe 		return xTableRows;
1792fa2a90fSLiu Zhe 	}
1802fa2a90fSLiu Zhe 
1812fa2a90fSLiu Zhe 	/**
1822fa2a90fSLiu Zhe 	 * Get columns object
1832fa2a90fSLiu Zhe 	 * @param xSpreadsheet
1842fa2a90fSLiu Zhe 	 * @return
1852fa2a90fSLiu Zhe 	 * @throws Exception
1862fa2a90fSLiu Zhe 	 */
getSCColumns(XSpreadsheet xSpreadsheet)1872fa2a90fSLiu Zhe 	public static XTableColumns getSCColumns(XSpreadsheet xSpreadsheet) throws Exception {
1882fa2a90fSLiu Zhe 		XColumnRowRange xColumnRowRange =
1892fa2a90fSLiu Zhe 				(XColumnRowRange) UnoRuntime.queryInterface(XColumnRowRange.class, xSpreadsheet);
1902fa2a90fSLiu Zhe 		XTableColumns xTableColumns = xColumnRowRange.getColumns();
1912fa2a90fSLiu Zhe 
1922fa2a90fSLiu Zhe 		return xTableColumns;
1932fa2a90fSLiu Zhe 	}
1942fa2a90fSLiu Zhe 
1952fa2a90fSLiu Zhe 	/**
1962fa2a90fSLiu Zhe 	 * Set floating number into specific cell
1972fa2a90fSLiu Zhe 	 * @param xSpreadsheet
1982fa2a90fSLiu Zhe 	 * @param column
1992fa2a90fSLiu Zhe 	 * @param row
2002fa2a90fSLiu Zhe 	 * @param value
2012fa2a90fSLiu Zhe 	 * @throws Exception
2022fa2a90fSLiu Zhe 	 */
setValueToCell(XSpreadsheet xSpreadsheet, int column, int row, double value)2032fa2a90fSLiu Zhe 	public static void setValueToCell(XSpreadsheet xSpreadsheet, int column, int row, double value) throws Exception {
2042fa2a90fSLiu Zhe 		XCell xCell = xSpreadsheet.getCellByPosition(column, row);
2052fa2a90fSLiu Zhe 		xCell.setValue(value);
2062fa2a90fSLiu Zhe 	}
2072fa2a90fSLiu Zhe 
2082fa2a90fSLiu Zhe 	/**
2092fa2a90fSLiu Zhe 	 * Set text into specific cell
2102fa2a90fSLiu Zhe 	 * @param xSpreadsheet
2112fa2a90fSLiu Zhe 	 * @param column
2122fa2a90fSLiu Zhe 	 * @param row
2132fa2a90fSLiu Zhe 	 * @param text
2142fa2a90fSLiu Zhe 	 * @throws Exception
2152fa2a90fSLiu Zhe 	 */
setTextToCell(XSpreadsheet xSpreadsheet, int column, int row, String text)2162fa2a90fSLiu Zhe 	public static void setTextToCell(XSpreadsheet xSpreadsheet, int column, int row, String text) throws Exception {
2172fa2a90fSLiu Zhe 		XCell xCell = xSpreadsheet.getCellByPosition(column, row);
2182fa2a90fSLiu Zhe 		XText xText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
2192fa2a90fSLiu Zhe 		xText.setString(text);
2202fa2a90fSLiu Zhe 	}
2212fa2a90fSLiu Zhe 
2221ea6643fSLiu Zhe 	/**
2231ea6643fSLiu Zhe 	 * Set text into specific cell
2241ea6643fSLiu Zhe 	 * @param xCell
2251ea6643fSLiu Zhe 	 * @param text
2261ea6643fSLiu Zhe 	 * @throws Exception
2271ea6643fSLiu Zhe 	 */
setTextToCell(XCell xCell, String text)2281ea6643fSLiu Zhe 	public static void setTextToCell(XCell xCell, String text) throws Exception {
2291ea6643fSLiu Zhe 		XText xText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
2301ea6643fSLiu Zhe 		xText.setString(text);
2311ea6643fSLiu Zhe 	}
2321ea6643fSLiu Zhe 
2332fa2a90fSLiu Zhe 	/**
2342fa2a90fSLiu Zhe 	 * Set formula into specific cell
2352fa2a90fSLiu Zhe 	 * @param xSpreadsheet
2362fa2a90fSLiu Zhe 	 * @param column
2372fa2a90fSLiu Zhe 	 * @param row
2382fa2a90fSLiu Zhe 	 * @param formula
2392fa2a90fSLiu Zhe 	 * @throws Exception
2402fa2a90fSLiu Zhe 	 */
setFormulaToCell(XSpreadsheet xSpreadsheet, int column, int row, String formula)2412fa2a90fSLiu Zhe 	public static void setFormulaToCell(XSpreadsheet xSpreadsheet, int column, int row, String formula) throws Exception {
2422fa2a90fSLiu Zhe 		XCell xCell = xSpreadsheet.getCellByPosition(column, row);
2432fa2a90fSLiu Zhe 		xCell.setFormula(formula);
2442fa2a90fSLiu Zhe 	}
2452fa2a90fSLiu Zhe 
2462fa2a90fSLiu Zhe 	/**
2472fa2a90fSLiu Zhe 	 * Get value from specific cell
2482fa2a90fSLiu Zhe 	 * @param xSpreadsheet
2492fa2a90fSLiu Zhe 	 * @param column
2502fa2a90fSLiu Zhe 	 * @param row
2512fa2a90fSLiu Zhe 	 * @return
2522fa2a90fSLiu Zhe 	 * @throws Exception
2532fa2a90fSLiu Zhe 	 */
getValueFromCell(XSpreadsheet xSpreadsheet, int column, int row)2542fa2a90fSLiu Zhe 	public static double getValueFromCell(XSpreadsheet xSpreadsheet, int column, int row) throws Exception {
2552fa2a90fSLiu Zhe 		XCell xCell = xSpreadsheet.getCellByPosition(column, row);
2562fa2a90fSLiu Zhe 		double cellValue = xCell.getValue();
2572fa2a90fSLiu Zhe 
2582fa2a90fSLiu Zhe 		return cellValue;
2592fa2a90fSLiu Zhe 	}
2602fa2a90fSLiu Zhe 
2612fa2a90fSLiu Zhe 	/**
2622fa2a90fSLiu Zhe 	 * Get text from specific cell
2632fa2a90fSLiu Zhe 	 * @param xSpreadsheet
2642fa2a90fSLiu Zhe 	 * @param column
2652fa2a90fSLiu Zhe 	 * @param row
2662fa2a90fSLiu Zhe 	 *
2672fa2a90fSLiu Zhe 	 * @return
2682fa2a90fSLiu Zhe 	 * @throws Exception
2692fa2a90fSLiu Zhe 	 */
getTextFromCell(XSpreadsheet xSpreadsheet, int column, int row)2702fa2a90fSLiu Zhe 	public static String getTextFromCell(XSpreadsheet xSpreadsheet, int column, int row) throws Exception {
2712fa2a90fSLiu Zhe 		XCell xCell = xSpreadsheet.getCellByPosition(column, row);
2722fa2a90fSLiu Zhe 		XText xText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
2732fa2a90fSLiu Zhe 
2742fa2a90fSLiu Zhe 		return xText.getString();
2752fa2a90fSLiu Zhe 	}
2762fa2a90fSLiu Zhe 
2772fa2a90fSLiu Zhe 	/**
2782fa2a90fSLiu Zhe 	 * Get formula string from specific cell
2792fa2a90fSLiu Zhe 	 * @param xSpreadsheet
2802fa2a90fSLiu Zhe 	 * @param column
2812fa2a90fSLiu Zhe 	 * @param row
2822fa2a90fSLiu Zhe 	 * @return
2832fa2a90fSLiu Zhe 	 * @throws Exception
2842fa2a90fSLiu Zhe 	 */
getFormulaFromCell(XSpreadsheet xSpreadsheet, int column, int row)2852fa2a90fSLiu Zhe 	public static String getFormulaFromCell(XSpreadsheet xSpreadsheet, int column, int row) throws Exception {
2862fa2a90fSLiu Zhe 		XCell xCell = xSpreadsheet.getCellByPosition(column, row);
2872fa2a90fSLiu Zhe 		String cellFormula = xCell.getFormula();
2882fa2a90fSLiu Zhe 
2892fa2a90fSLiu Zhe 		return cellFormula;
2902fa2a90fSLiu Zhe 	}
2912fa2a90fSLiu Zhe 
2922fa2a90fSLiu Zhe 	/**
2932fa2a90fSLiu Zhe 	 * Set numbers into a cell range
2942fa2a90fSLiu Zhe 	 * @param xSpreadsheet
2952fa2a90fSLiu Zhe 	 * @param start_col
2962fa2a90fSLiu Zhe 	 * @param start_row
2972fa2a90fSLiu Zhe 	 * @param end_col
2982fa2a90fSLiu Zhe 	 * @param end_row
2992fa2a90fSLiu Zhe 	 * @param values
3002fa2a90fSLiu Zhe 	 * @throws Exception
3012fa2a90fSLiu Zhe 	 */
3021dfd36f5SLiu Zhe 	@Deprecated
setValueToCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row, double[][] values)3032fa2a90fSLiu Zhe 	public static void setValueToCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row,  double[][] values) throws Exception {
3042fa2a90fSLiu Zhe 		XCellRange xCellRange = xSpreadsheet.getCellRangeByPosition(start_col, start_row, end_col, end_row);
3052fa2a90fSLiu Zhe 		XCell xCell = null;
3062fa2a90fSLiu Zhe 		for (int i = 0; i <= (end_row - start_row); i++ ) {
3072fa2a90fSLiu Zhe 			for(int j = 0; j <= (end_col - start_col); j++) {
3082fa2a90fSLiu Zhe 				xCell = xCellRange.getCellByPosition(j, i);
3092fa2a90fSLiu Zhe 				xCell.setValue(values[i][j]);
3102fa2a90fSLiu Zhe 			}
3112fa2a90fSLiu Zhe 		}
3122fa2a90fSLiu Zhe 	}
3132fa2a90fSLiu Zhe 
setValueToCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, double[][] values)3141dfd36f5SLiu Zhe 	public static void setValueToCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, double[][] values) throws Exception {
3151dfd36f5SLiu Zhe 		XCellRange xCellRange = xSpreadsheet.getCellRangeByPosition(start_col, start_row, start_col + values[0].length - 1, start_row + values.length - 1);
3161dfd36f5SLiu Zhe 		XCell xCell = null;
3171dfd36f5SLiu Zhe 		for (int i = 0; i < values.length; i++ ) {
3181dfd36f5SLiu Zhe 			for(int j = 0; j < values[0].length; j++) {
3191dfd36f5SLiu Zhe 				xCell = xCellRange.getCellByPosition(j, i);
3201dfd36f5SLiu Zhe 				xCell.setValue(values[i][j]);
3211dfd36f5SLiu Zhe 			}
3221dfd36f5SLiu Zhe 		}
3231dfd36f5SLiu Zhe 	}
3241dfd36f5SLiu Zhe 
3252fa2a90fSLiu Zhe 	/**
3262fa2a90fSLiu Zhe 	 * Set text into 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 	 * @param texts
3332fa2a90fSLiu Zhe 	 * @throws Exception
3342fa2a90fSLiu Zhe 	 */
3351dfd36f5SLiu Zhe 	@Deprecated
setTextToCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row, String[][] texts)3362fa2a90fSLiu Zhe 	public static void setTextToCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row,  String[][] texts) throws Exception {
3372fa2a90fSLiu Zhe 		XCellRange xCellRange = xSpreadsheet.getCellRangeByPosition(start_col, start_row, end_col, end_row);
3382fa2a90fSLiu Zhe 		XCell xCell = null;
3392fa2a90fSLiu Zhe 		XText xText = null;
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 				xText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
3442fa2a90fSLiu Zhe 				xText.setString(texts[i][j]);
3452fa2a90fSLiu Zhe 			}
3462fa2a90fSLiu Zhe 		}
3472fa2a90fSLiu Zhe 	}
3482fa2a90fSLiu Zhe 
setTextToCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, String[][] texts)3491dfd36f5SLiu Zhe 	public static void setTextToCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, String[][] texts) throws Exception {
3501dfd36f5SLiu Zhe 		XCellRange xCellRange = xSpreadsheet.getCellRangeByPosition(start_col, start_row, start_col + texts[0].length - 1, start_row + texts.length - 1);
3511dfd36f5SLiu Zhe 		XCell xCell = null;
3521dfd36f5SLiu Zhe 		XText xText = null;
3531dfd36f5SLiu Zhe 		for (int i = 0; i < texts.length; i++ ) {
3541dfd36f5SLiu Zhe 			for(int j = 0; j < texts[0].length; j++) {
3551dfd36f5SLiu Zhe 				xCell = xCellRange.getCellByPosition(j, i);
3561dfd36f5SLiu Zhe 				xText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
3571dfd36f5SLiu Zhe 				xText.setString(texts[i][j]);
3581dfd36f5SLiu Zhe 			}
3591dfd36f5SLiu Zhe 		}
3601dfd36f5SLiu Zhe 	}
3611dfd36f5SLiu Zhe 
3622fa2a90fSLiu Zhe 	/**
3632fa2a90fSLiu Zhe 	 * Get number content from a cell range
3642fa2a90fSLiu Zhe 	 * @param xSpreadsheet
3652fa2a90fSLiu Zhe 	 * @param start_col
3662fa2a90fSLiu Zhe 	 * @param start_row
3672fa2a90fSLiu Zhe 	 * @param end_col
3682fa2a90fSLiu Zhe 	 * @param end_row
3692fa2a90fSLiu Zhe 	 * @return
3702fa2a90fSLiu Zhe 	 * @throws Exception
3712fa2a90fSLiu Zhe 	 */
getValueFromCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row)3722fa2a90fSLiu Zhe 	public static double[][] getValueFromCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row) throws Exception {
3732fa2a90fSLiu Zhe 		XCellRange xCellRange = xSpreadsheet.getCellRangeByPosition(start_col, start_row, end_col, end_row);
3742fa2a90fSLiu Zhe 		XCell xCell = null;
3752fa2a90fSLiu Zhe 		double[][] cellValues = new double[end_row - start_row+1][end_col - start_col +1];
3762fa2a90fSLiu Zhe 
3772fa2a90fSLiu Zhe 		for (int i = 0; i <= (end_row - start_row); i++ ) {
3782fa2a90fSLiu Zhe 			for(int j = 0; j <= (end_col - start_col); j++) {
3792fa2a90fSLiu Zhe 				xCell = xCellRange.getCellByPosition(j, i);
3802fa2a90fSLiu Zhe 				cellValues[i][j] = xCell.getValue();
3812fa2a90fSLiu Zhe 			}
3822fa2a90fSLiu Zhe 		}
3832fa2a90fSLiu Zhe 
3842fa2a90fSLiu Zhe 		return cellValues;
3852fa2a90fSLiu Zhe 	}
3862fa2a90fSLiu Zhe 
3872fa2a90fSLiu Zhe 	/**
3882fa2a90fSLiu Zhe 	 * Get text content from a cell range
3892fa2a90fSLiu Zhe 	 * @param xSpreadsheet
3902fa2a90fSLiu Zhe 	 * @param start_col
3912fa2a90fSLiu Zhe 	 * @param start_row
3922fa2a90fSLiu Zhe 	 * @param end_col
3932fa2a90fSLiu Zhe 	 * @param end_row
3942fa2a90fSLiu Zhe 	 * @return
3952fa2a90fSLiu Zhe 	 * @throws Exception
3962fa2a90fSLiu Zhe 	 */
getTextFromCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row)3972fa2a90fSLiu Zhe 	public static String[][] getTextFromCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row) throws Exception {
3982fa2a90fSLiu Zhe 		XCellRange xCellRange = xSpreadsheet.getCellRangeByPosition(start_col, start_row, end_col, end_row);
3992fa2a90fSLiu Zhe 		XCell xCell = null;
4002fa2a90fSLiu Zhe 		XText xText = null;
4012fa2a90fSLiu Zhe 		String[][] cellTexts = new String[end_row - start_row+1][end_col - start_col +1];
4022fa2a90fSLiu Zhe 
4032fa2a90fSLiu Zhe 		for (int i = 0; i <= (end_row - start_row); i++ ) {
4041ea6643fSLiu Zhe 			for (int j = 0; j <= (end_col - start_col); j++) {
4052fa2a90fSLiu Zhe 				xCell = xCellRange.getCellByPosition(j, i);
4062fa2a90fSLiu Zhe 				xText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
4072fa2a90fSLiu Zhe 				cellTexts[i][j] = xText.getString();
4082fa2a90fSLiu Zhe 			}
4092fa2a90fSLiu Zhe 		}
4102fa2a90fSLiu Zhe 
4112fa2a90fSLiu Zhe 		return cellTexts;
4122fa2a90fSLiu Zhe 	}
4132fa2a90fSLiu Zhe 
4142fa2a90fSLiu Zhe 	//TODO ZS - public static String[][] getAllFromCellRange
4152fa2a90fSLiu Zhe 
4162fa2a90fSLiu Zhe 	/**
4172fa2a90fSLiu Zhe 	 * Switch to specific sheet
4182fa2a90fSLiu Zhe 	 * @param xSpreadsheetDocument
4192fa2a90fSLiu Zhe 	 * @param xSpreadsheet
4202fa2a90fSLiu Zhe 	 */
setCurrentSheet(XSpreadsheetDocument xSpreadsheetDocument, XSpreadsheet xSpreadsheet)4212fa2a90fSLiu Zhe 	public static void setCurrentSheet(XSpreadsheetDocument xSpreadsheetDocument, XSpreadsheet xSpreadsheet) throws Exception {
4222fa2a90fSLiu Zhe 		XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, xSpreadsheetDocument);
4232fa2a90fSLiu Zhe 		XController xController = xModel.getCurrentController();
4242fa2a90fSLiu Zhe 		XSpreadsheetView xSpreadsheetView = (XSpreadsheetView) UnoRuntime.queryInterface(XSpreadsheetView.class, xController);
4252fa2a90fSLiu Zhe 		xSpreadsheetView.setActiveSheet(xSpreadsheet);
4262fa2a90fSLiu Zhe 	}
4272fa2a90fSLiu Zhe 
4282fa2a90fSLiu Zhe 	/**
4292fa2a90fSLiu Zhe 	 * Get sheet object of current active sheet
4302fa2a90fSLiu Zhe 	 * @param xSpreadsheetDocument
4312fa2a90fSLiu Zhe 	 * @return
4322fa2a90fSLiu Zhe 	 */
getCurrentSheet(XSpreadsheetDocument xSpreadsheetDocument)4332fa2a90fSLiu Zhe 	public static XSpreadsheet getCurrentSheet(XSpreadsheetDocument xSpreadsheetDocument) throws Exception {
4342fa2a90fSLiu Zhe 		XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, xSpreadsheetDocument);
4352fa2a90fSLiu Zhe 		XController xController = xModel.getCurrentController();
4362fa2a90fSLiu Zhe 		XSpreadsheetView xSpreadsheetView = (XSpreadsheetView) UnoRuntime.queryInterface(XSpreadsheetView.class, xController);
4372fa2a90fSLiu Zhe 		XSpreadsheet xSpreadsheet = xSpreadsheetView.getActiveSheet();
4382fa2a90fSLiu Zhe 
4392fa2a90fSLiu Zhe 		return xSpreadsheet;
4402fa2a90fSLiu Zhe 	}
4412fa2a90fSLiu Zhe 
442d01630b6SLiu Zhe 	/**
443d01630b6SLiu Zhe 	 * Get sheet object by sheet index
444d01630b6SLiu Zhe 	 *
445d01630b6SLiu Zhe 	 * @param xSpreadsheetDocument
446d01630b6SLiu Zhe 	 * @return
447d01630b6SLiu Zhe 	 * @throws Exception
448d01630b6SLiu Zhe 	 */
getSCActiveSheetName( XSpreadsheetDocument xSpreadsheetDocument)449d01630b6SLiu Zhe 	public static String getSCActiveSheetName(
450d01630b6SLiu Zhe 			XSpreadsheetDocument xSpreadsheetDocument) throws Exception {
451d01630b6SLiu Zhe 		XModel xSpreadsheetModel = (XModel) UnoRuntime.queryInterface(
452d01630b6SLiu Zhe 				XModel.class, xSpreadsheetDocument);
453d01630b6SLiu Zhe 		XSpreadsheetView xSpeadsheetView = (XSpreadsheetView) UnoRuntime
454d01630b6SLiu Zhe 				.queryInterface(XSpreadsheetView.class,
455d01630b6SLiu Zhe 						xSpreadsheetModel.getCurrentController());
456d01630b6SLiu Zhe 		XSpreadsheet activesheet = xSpeadsheetView.getActiveSheet();
457d01630b6SLiu Zhe 		XNamed activesheetName = (XNamed) UnoRuntime.queryInterface(
458d01630b6SLiu Zhe 				XNamed.class, activesheet);
459d01630b6SLiu Zhe 		return activesheetName.getName();
460d01630b6SLiu Zhe 	}
461d01630b6SLiu Zhe 
4621dfd36f5SLiu Zhe 	/**
4631dfd36f5SLiu Zhe 	 * Set specific property's value for an object
4641dfd36f5SLiu Zhe 	 * @param obj
4651dfd36f5SLiu Zhe 	 * @param propName
4661dfd36f5SLiu Zhe 	 * @param value
4671dfd36f5SLiu Zhe 	 * @throws Exception
4681dfd36f5SLiu Zhe 	 */
setProperties(Object obj, String propName, Object value)4691dfd36f5SLiu Zhe 	public static void setProperties(Object obj, String propName, Object value) throws Exception {
4701dfd36f5SLiu Zhe 		XPropertySet xPropertySet =
4711dfd36f5SLiu Zhe 				(XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, obj);
4721dfd36f5SLiu Zhe 		xPropertySet.setPropertyValue(propName, value);
4731dfd36f5SLiu Zhe 	}
4741dfd36f5SLiu Zhe 
4751dfd36f5SLiu Zhe 	/**
4761dfd36f5SLiu Zhe 	 * Get specific property's value of an object
4771dfd36f5SLiu Zhe 	 * @param obj
4781dfd36f5SLiu Zhe 	 * @param propName
4791dfd36f5SLiu Zhe 	 * @return
4801dfd36f5SLiu Zhe 	 * @throws Exception
4811dfd36f5SLiu Zhe 	 */
getProperties(Object obj, String propName)4821dfd36f5SLiu Zhe 	public static Object getProperties(Object obj, String propName) throws Exception {
4831dfd36f5SLiu Zhe 		XPropertySet xPropertySet =
4841dfd36f5SLiu Zhe 				(XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, obj);
4851dfd36f5SLiu Zhe 		Object value = xPropertySet.getPropertyValue(propName);
4861dfd36f5SLiu Zhe 
4871dfd36f5SLiu Zhe 		return value;
4881dfd36f5SLiu Zhe 	}
4891dfd36f5SLiu Zhe 
4902fa2a90fSLiu Zhe 	/**
4911ea6643fSLiu Zhe 	 * Set value of specific property from a cell
4921ea6643fSLiu Zhe 	 * @param xCell
4931ea6643fSLiu Zhe 	 * @param propName
4941ea6643fSLiu Zhe 	 * @param value
4951ea6643fSLiu Zhe 	 * @throws Exception
4961ea6643fSLiu Zhe 	 */
setCellProperties(XCell xCell, String propName, Object value)4971ea6643fSLiu Zhe 	public static void setCellProperties(XCell xCell, String propName, Object value) throws Exception {
4981ea6643fSLiu Zhe 
4991dfd36f5SLiu Zhe 		setProperties(xCell, propName, value);
5001ea6643fSLiu Zhe 	}
5011ea6643fSLiu Zhe 
5021ea6643fSLiu Zhe 	/**
5031ea6643fSLiu Zhe 	 * Get value of specific property from a cell
5041ea6643fSLiu Zhe 	 * @param xCell
5051ea6643fSLiu Zhe 	 * @param propName
5061ea6643fSLiu Zhe 	 * @return
5071ea6643fSLiu Zhe 	 * @throws Exception
5081ea6643fSLiu Zhe 	 */
getCellProperties(XCell xCell, String propName)5091ea6643fSLiu Zhe 	public static Object getCellProperties(XCell xCell, String propName) throws Exception {
5101dfd36f5SLiu Zhe 			return getProperties(xCell, propName);
5111ea6643fSLiu Zhe 	}
5121ea6643fSLiu Zhe 
5131ea6643fSLiu Zhe 	/**
5141ea6643fSLiu Zhe 	 * Clear temp file directory
5151ea6643fSLiu Zhe 	 */
clearTempDir()5161ea6643fSLiu Zhe 	public static void clearTempDir() {
5171ea6643fSLiu Zhe 		FileUtil.deleteFile(Testspace.getFile(Testspace.getPath(scTempDir)));
5181ea6643fSLiu Zhe 	}
5191ea6643fSLiu Zhe 
5201ea6643fSLiu Zhe 	/**
5211ea6643fSLiu Zhe 	 * Save file as specific file format into spreadsheet temp file folder.
5222fa2a90fSLiu Zhe 	 * @param scComponent
5232fa2a90fSLiu Zhe 	 * @param fileName  File name string without extension name (e.g. "sampleFile")
5242fa2a90fSLiu Zhe 	 * @param extName ("ods", "ots", "xls", "xlt", "csv")
5252fa2a90fSLiu Zhe 	 * @throws Exception
5262fa2a90fSLiu Zhe 	 */
saveFileAs(XComponent scComponent, String fileName, String extName)5272fa2a90fSLiu Zhe 	public static void saveFileAs(XComponent scComponent, String fileName, String extName) throws Exception {
5282fa2a90fSLiu Zhe 
5292fa2a90fSLiu Zhe 		initFilterName();
5302fa2a90fSLiu Zhe 
5311ea6643fSLiu Zhe 		String storeUrl = Testspace.getUrl(scTempDir + fileName + "." + extName);
5322fa2a90fSLiu Zhe 
5332fa2a90fSLiu Zhe 		PropertyValue[] storeProps = new PropertyValue[2];
5342fa2a90fSLiu Zhe 		storeProps[0] = new PropertyValue();
5352fa2a90fSLiu Zhe 		storeProps[0].Name = "FilterName";
5362fa2a90fSLiu Zhe 		storeProps[0].Value = filterName.get(extName);
5372fa2a90fSLiu Zhe 		storeProps[1] = new PropertyValue();
5382fa2a90fSLiu Zhe 		storeProps[1].Name = "Overwrite";
5392fa2a90fSLiu Zhe 		storeProps[1].Value = new Boolean(true);
5402fa2a90fSLiu Zhe 
5412fa2a90fSLiu Zhe 		XStorable scStorable =
5422fa2a90fSLiu Zhe 				(XStorable) UnoRuntime.queryInterface(XStorable.class, scComponent);
5432fa2a90fSLiu Zhe 		scStorable.storeAsURL(storeUrl, storeProps);
5442fa2a90fSLiu Zhe 	}
5452fa2a90fSLiu Zhe 
546d01630b6SLiu Zhe 	/**
5471dfd36f5SLiu Zhe 	 * Save file after open file.
548d01630b6SLiu Zhe 	 * @param xSpreadsheetDocument
549d01630b6SLiu Zhe 	 * @throws Exception
550d01630b6SLiu Zhe 	 */
save(XSpreadsheetDocument xSpreadsheetDocument)551d01630b6SLiu Zhe 	public static void save(XSpreadsheetDocument xSpreadsheetDocument)
552d01630b6SLiu Zhe 			throws Exception {
553d01630b6SLiu Zhe 		XStorable scStorable = (XStorable) UnoRuntime.queryInterface(
554d01630b6SLiu Zhe 				XStorable.class, xSpreadsheetDocument);
555d01630b6SLiu Zhe 		scStorable.store();
556d01630b6SLiu Zhe 	}
557d01630b6SLiu Zhe 
558d01630b6SLiu Zhe 
5592fa2a90fSLiu Zhe 	/**
5602fa2a90fSLiu Zhe 	 * Close specific opening spreadsheet file which has been saved
5612fa2a90fSLiu Zhe 	 * @param xSpreadsheetDocument
5622fa2a90fSLiu Zhe 	 * @throws Exception
5632fa2a90fSLiu Zhe 	 */
closeFile(XSpreadsheetDocument xSpreadsheetDocument)5642fa2a90fSLiu Zhe 	public static void closeFile(XSpreadsheetDocument xSpreadsheetDocument) throws Exception {
5652fa2a90fSLiu Zhe 		XCloseable xCloseable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, xSpreadsheetDocument);
5662fa2a90fSLiu Zhe 		xCloseable.close(false);
5672fa2a90fSLiu Zhe 	}
5682fa2a90fSLiu Zhe 
5692fa2a90fSLiu Zhe 	/**
5701ea6643fSLiu Zhe 	 * Close a opening file saved in spreadsheet temp file direction and reopen it in Spreadsheet. For save&reload test scenario only.
5712fa2a90fSLiu Zhe 	 * @param unoApp
5722fa2a90fSLiu Zhe 	 * @param xSpreadsheetDocument
5732fa2a90fSLiu Zhe 	 * @param fullFileName   File name with the extension name. (e.g. "sc.ods")
5742fa2a90fSLiu Zhe 	 * @return
5752fa2a90fSLiu Zhe 	 * @throws Exception
5762fa2a90fSLiu Zhe 	 */
reloadFile(UnoApp unoApp, XSpreadsheetDocument xSpreadsheetDocument, String fullFileName)5772fa2a90fSLiu Zhe 	public static XSpreadsheetDocument reloadFile(UnoApp unoApp, XSpreadsheetDocument xSpreadsheetDocument, String fullFileName) throws Exception {
5782fa2a90fSLiu Zhe 		closeFile(xSpreadsheetDocument);
5792fa2a90fSLiu Zhe 
5801ea6643fSLiu Zhe 		String filePath = Testspace.getPath(scTempDir + fullFileName);
581cebb507aSLiu Zhe 		XSpreadsheetDocument xScDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, unoApp.loadDocument(filePath));
5822fa2a90fSLiu Zhe 
5832fa2a90fSLiu Zhe 		return xScDocument;
5842fa2a90fSLiu Zhe 	}
5852fa2a90fSLiu Zhe 
5869fdcf9fdSLiu Zhe 	/**
5879fdcf9fdSLiu Zhe 	 * open file in Spreadsheet.
5881dfd36f5SLiu Zhe 	 * @param app
5891dfd36f5SLiu Zhe 	 * @param filePath   File path with the extension name. (e.g. "testcase/uno/sc/data/sample.xls")
5909fdcf9fdSLiu Zhe 	 * @return
5919fdcf9fdSLiu Zhe 	 * @throws Exception
5929fdcf9fdSLiu Zhe 	 */
openFile(String filePath, UnoApp app)5939fdcf9fdSLiu Zhe 	public static XSpreadsheetDocument openFile(String filePath, UnoApp app) throws Exception {
5949fdcf9fdSLiu Zhe 		return (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, app.loadDocument(filePath));
5959fdcf9fdSLiu Zhe 	}
5969fdcf9fdSLiu Zhe 
5972fa2a90fSLiu Zhe 	/**
5982fa2a90fSLiu Zhe 	 * Initial the filter name list
5992fa2a90fSLiu Zhe 	 * @throws Exception
6002fa2a90fSLiu Zhe 	 */
initFilterName()6012fa2a90fSLiu Zhe 	private static void initFilterName() throws Exception {
6022fa2a90fSLiu Zhe 		if (filterName.size() > 0) {
6032fa2a90fSLiu Zhe 			return;
6042fa2a90fSLiu Zhe 		}
6052fa2a90fSLiu Zhe 
6062fa2a90fSLiu Zhe 		filterName.put("ods", "calc8");
6072fa2a90fSLiu Zhe 		filterName.put("ots", "calc8_template");
6082fa2a90fSLiu Zhe 		filterName.put("xls", "MS Excel 97");
6092fa2a90fSLiu Zhe 		filterName.put("xlt", "MS Excel 97 Vorlage/Template");
6102fa2a90fSLiu Zhe 		filterName.put("csv", "Text - txt - csv (StarCalc)");
6112fa2a90fSLiu Zhe 	}
6121dfd36f5SLiu Zhe 
6131dfd36f5SLiu Zhe 
6141dfd36f5SLiu Zhe 	/***************************************************************
6151dfd36f5SLiu Zhe 	 *      Chart Utility method - using chart interface           *
6161dfd36f5SLiu Zhe 	****************************************************************/
6171dfd36f5SLiu Zhe 
6181dfd36f5SLiu Zhe 	/**
6191dfd36f5SLiu Zhe 	 * Get a CellRangeAddress by cell range reference name
6201dfd36f5SLiu Zhe 	 * @param xSpreadsheet
6211dfd36f5SLiu Zhe 	 * @param rangeName    a cell range reference name(e.g. "A1:B2")
6221dfd36f5SLiu Zhe 	 * @return
6231dfd36f5SLiu Zhe 	 */
getChartDataRangeByName(XSpreadsheet xSpreadsheet, String rangeName)6241dfd36f5SLiu Zhe 	public static CellRangeAddress getChartDataRangeByName(XSpreadsheet xSpreadsheet, String rangeName) {
6251dfd36f5SLiu Zhe 		XCellRange cellRange = xSpreadsheet.getCellRangeByName(rangeName);
6261dfd36f5SLiu Zhe 		XCellRangeAddressable xCellRangeAddressable =
6271dfd36f5SLiu Zhe 			(XCellRangeAddressable) UnoRuntime.queryInterface(XCellRangeAddressable.class, cellRange);
6281dfd36f5SLiu Zhe 
6291dfd36f5SLiu Zhe 		CellRangeAddress cellRangeAddress = xCellRangeAddressable.getRangeAddress();
6301dfd36f5SLiu Zhe 		return cellRangeAddress;
6311dfd36f5SLiu Zhe 	}
6321dfd36f5SLiu Zhe 
6331dfd36f5SLiu Zhe 	/**
6341dfd36f5SLiu Zhe 	 * Create a spreadsheet chart with data in a specific cell range.
6351dfd36f5SLiu Zhe 	 * @param xSpreadsheet
6361dfd36f5SLiu Zhe 	 * @param rec   a rectangle shape object
6371dfd36f5SLiu Zhe 	 * @param dataRangeAddress   the CellRangeAddress array of chart data source
6381dfd36f5SLiu Zhe 	 * @param chartName
6391dfd36f5SLiu Zhe 	 * @return
6401dfd36f5SLiu Zhe 	 * @throws Exception
6411dfd36f5SLiu Zhe 	 */
createChart(XSpreadsheet xSpreadsheet, Rectangle rec, CellRangeAddress[] dataRangeAddress, String chartName)6421dfd36f5SLiu Zhe 	public static XChartDocument createChart(XSpreadsheet xSpreadsheet, Rectangle rec, CellRangeAddress[] dataRangeAddress, String chartName) throws Exception {
643*5e5a8699SLiu Zhe 
644*5e5a8699SLiu Zhe 		return createChart(xSpreadsheet, rec, dataRangeAddress, chartName, true, false);
645*5e5a8699SLiu Zhe 	}
646*5e5a8699SLiu Zhe 
647*5e5a8699SLiu Zhe 	/**
648*5e5a8699SLiu Zhe 	 * Create a spreadsheet chart with data in a specific cell range with column/row label enable/not.
649*5e5a8699SLiu Zhe 	 * @param xSpreadsheet
650*5e5a8699SLiu Zhe 	 * @param rec    a rectangle shape object
651*5e5a8699SLiu Zhe 	 * @param dataRangeAddress    the CellRangeAddress array of chart data source
652*5e5a8699SLiu Zhe 	 * @param chartName
653*5e5a8699SLiu Zhe 	 * @param hasColumnLabel
654*5e5a8699SLiu Zhe 	 * @param hasRowLabel
655*5e5a8699SLiu Zhe 	 * @return
656*5e5a8699SLiu Zhe 	 * @throws Exception
657*5e5a8699SLiu Zhe 	 */
createChart(XSpreadsheet xSpreadsheet, Rectangle rec, CellRangeAddress[] dataRangeAddress, String chartName, Boolean hasColumnLabel, Boolean hasRowLabel)658*5e5a8699SLiu Zhe 	public static XChartDocument createChart(XSpreadsheet xSpreadsheet, Rectangle rec, CellRangeAddress[] dataRangeAddress, String chartName, Boolean hasColumnLabel, Boolean hasRowLabel) throws Exception {
6591dfd36f5SLiu Zhe 		XChartDocument xChartDocument = null;
6601dfd36f5SLiu Zhe 		XTableChartsSupplier xTChartSupplier =
6611dfd36f5SLiu Zhe 				(XTableChartsSupplier) UnoRuntime.queryInterface(XTableChartsSupplier.class, xSpreadsheet);
6621dfd36f5SLiu Zhe 		XTableCharts xTableCharts = xTChartSupplier.getCharts();
6631dfd36f5SLiu Zhe 		XNameAccess xNameAccess =
6641dfd36f5SLiu Zhe 				(XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, xTableCharts);
6651dfd36f5SLiu Zhe 		if (xNameAccess != null && !xNameAccess.hasByName(chartName)) {
6661dfd36f5SLiu Zhe 
667*5e5a8699SLiu Zhe 			xTableCharts.addNewByName(chartName, rec, dataRangeAddress, hasColumnLabel, hasRowLabel);
6681dfd36f5SLiu Zhe 			XTableChart xTableChart = (XTableChart) UnoRuntime.queryInterface(
6691dfd36f5SLiu Zhe 					XTableChart.class, xNameAccess.getByName(chartName));
6701dfd36f5SLiu Zhe 			XEmbeddedObjectSupplier xEmbeddedObjectSupplier = (XEmbeddedObjectSupplier) UnoRuntime.queryInterface(
6711dfd36f5SLiu Zhe 					XEmbeddedObjectSupplier.class, xTableChart);
6721dfd36f5SLiu Zhe 			xChartDocument = (XChartDocument) UnoRuntime.queryInterface(
6731dfd36f5SLiu Zhe 					XChartDocument.class, xEmbeddedObjectSupplier.getEmbeddedObject());
6741dfd36f5SLiu Zhe 		}
6751dfd36f5SLiu Zhe 
6761dfd36f5SLiu Zhe 		return xChartDocument;
6771dfd36f5SLiu Zhe 	}
6781dfd36f5SLiu Zhe 
6791dfd36f5SLiu Zhe 	/**
6801dfd36f5SLiu Zhe 	 * Get XChartDocument object via the chart name.
6811dfd36f5SLiu Zhe 	 * @param xSpreadsheet
6821dfd36f5SLiu Zhe 	 * @param chartName
6831dfd36f5SLiu Zhe 	 * @return
6841dfd36f5SLiu Zhe 	 * @throws Exception
6851dfd36f5SLiu Zhe 	 */
getChartByName(XSpreadsheet xSpreadsheet, String chartName)6861dfd36f5SLiu Zhe 	public static XChartDocument getChartByName(XSpreadsheet xSpreadsheet, String chartName) throws Exception {
6871dfd36f5SLiu Zhe 		XChartDocument xChartDocument = null;
6881dfd36f5SLiu Zhe 		XTableChartsSupplier xTChartSupplier =
6891dfd36f5SLiu Zhe 				(XTableChartsSupplier) UnoRuntime.queryInterface(XTableChartsSupplier.class, xSpreadsheet);
6901dfd36f5SLiu Zhe 		XTableCharts xTableCharts = xTChartSupplier.getCharts();
6911dfd36f5SLiu Zhe 		XNameAccess xNameAccess =
6921dfd36f5SLiu Zhe 				(XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, xTableCharts);
6931dfd36f5SLiu Zhe 
6941dfd36f5SLiu Zhe 		if (xNameAccess != null && xNameAccess.hasByName(chartName)) {
6951dfd36f5SLiu Zhe 			XTableChart xTableChart = (XTableChart) UnoRuntime.queryInterface(
6961dfd36f5SLiu Zhe 					XTableChart.class, xNameAccess.getByName(chartName));
6971dfd36f5SLiu Zhe 			XEmbeddedObjectSupplier xEmbeddedObjectSupplier = (XEmbeddedObjectSupplier) UnoRuntime.queryInterface(
6981dfd36f5SLiu Zhe 					XEmbeddedObjectSupplier.class, xTableChart);
6991dfd36f5SLiu Zhe 			xChartDocument = (XChartDocument) UnoRuntime.queryInterface(
7001dfd36f5SLiu Zhe 					XChartDocument.class, xEmbeddedObjectSupplier.getEmbeddedObject());
7011dfd36f5SLiu Zhe 		}
7021dfd36f5SLiu Zhe 
7031dfd36f5SLiu Zhe 		return xChartDocument;
7041dfd36f5SLiu Zhe 	}
7051dfd36f5SLiu Zhe 
7061dfd36f5SLiu Zhe 	/**
7071dfd36f5SLiu Zhe 	 * Set specific basic type to chart
7081dfd36f5SLiu Zhe 	 * @param xChartDocument
7091dfd36f5SLiu Zhe 	 * @param chartType
7101dfd36f5SLiu Zhe 	 * @throws Exception
7111dfd36f5SLiu Zhe 	 */
setChartType(XChartDocument xChartDocument, String chartType)7121dfd36f5SLiu Zhe 	public static void setChartType(XChartDocument xChartDocument, String chartType) throws Exception {
7131dfd36f5SLiu Zhe 		XMultiServiceFactory xMultiServiceFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(
7141dfd36f5SLiu Zhe 			XMultiServiceFactory.class, xChartDocument);
7151dfd36f5SLiu Zhe 		XDiagram xDiagram = (XDiagram) UnoRuntime.queryInterface(
7161dfd36f5SLiu Zhe 			XDiagram.class, xMultiServiceFactory.createInstance(chartType));
7171dfd36f5SLiu Zhe 		xChartDocument.setDiagram(xDiagram);
7181dfd36f5SLiu Zhe 	}
7191dfd36f5SLiu Zhe 
7201dfd36f5SLiu Zhe 	/**
7211dfd36f5SLiu Zhe 	 * Get the type string of a chart
7221dfd36f5SLiu Zhe 	 * @param xChartDocument
7231dfd36f5SLiu Zhe 	 * @return
7241dfd36f5SLiu Zhe 	 * @throws Exception
7251dfd36f5SLiu Zhe 	 */
getChartType(XChartDocument xChartDocument)7261dfd36f5SLiu Zhe 	public static String getChartType(XChartDocument xChartDocument) throws Exception {
7271dfd36f5SLiu Zhe 		return xChartDocument.getDiagram().getDiagramType();
7281dfd36f5SLiu Zhe 	}
7291dfd36f5SLiu Zhe 
7301dfd36f5SLiu Zhe 	/**
7311dfd36f5SLiu Zhe 	 * Get the names of charts in specific sheet
7321dfd36f5SLiu Zhe 	 * @param xSpreadsheet
7331dfd36f5SLiu Zhe 	 * @return
7341dfd36f5SLiu Zhe 	 * @throws Exception
7351dfd36f5SLiu Zhe 	 */
getChartNameList(XSpreadsheet xSpreadsheet)7361dfd36f5SLiu Zhe 	public static String[] getChartNameList(XSpreadsheet xSpreadsheet) throws Exception {
7371dfd36f5SLiu Zhe 		XTableChartsSupplier xTChartSupplier =
7381dfd36f5SLiu Zhe 				(XTableChartsSupplier) UnoRuntime.queryInterface(XTableChartsSupplier.class, xSpreadsheet);
7391dfd36f5SLiu Zhe 		XTableCharts xTableCharts = xTChartSupplier.getCharts();
7401dfd36f5SLiu Zhe 		String[] chartNames = xTableCharts.getElementNames();
7411dfd36f5SLiu Zhe 		return chartNames;
7421dfd36f5SLiu Zhe 	}
7431dfd36f5SLiu Zhe 
7441dfd36f5SLiu Zhe 
7452fa2a90fSLiu Zhe 
7462fa2a90fSLiu Zhe }
747