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 311dfd36f5SLiu Zhe import com.sun.star.awt.Rectangle; 32e6e6073dSLiu Zhe import com.sun.star.beans.PropertyValue; 33e6e6073dSLiu Zhe import com.sun.star.beans.XPropertySet; 341dfd36f5SLiu Zhe import com.sun.star.chart.XChartDocument; 351dfd36f5SLiu Zhe import com.sun.star.chart.XDiagram; 36e6e6073dSLiu Zhe import com.sun.star.container.XIndexAccess; 371dfd36f5SLiu Zhe import com.sun.star.container.XNameAccess; 38e6e6073dSLiu Zhe import com.sun.star.container.XNamed; 391dfd36f5SLiu Zhe import com.sun.star.document.XEmbeddedObjectSupplier; 40e6e6073dSLiu Zhe import com.sun.star.frame.XController; 41e6e6073dSLiu Zhe import com.sun.star.frame.XModel; 42e6e6073dSLiu Zhe import com.sun.star.frame.XStorable; 43e6e6073dSLiu Zhe import com.sun.star.lang.XComponent; 441dfd36f5SLiu Zhe import com.sun.star.lang.XMultiServiceFactory; 451dfd36f5SLiu Zhe import com.sun.star.sheet.XCellRangeAddressable; 46e6e6073dSLiu Zhe import com.sun.star.sheet.XSpreadsheet; 47e6e6073dSLiu Zhe import com.sun.star.sheet.XSpreadsheetDocument; 48e6e6073dSLiu Zhe import com.sun.star.sheet.XSpreadsheetView; 49e6e6073dSLiu Zhe import com.sun.star.sheet.XSpreadsheets; 501dfd36f5SLiu Zhe import com.sun.star.table.CellRangeAddress; 51e6e6073dSLiu Zhe import com.sun.star.table.XCell; 52e6e6073dSLiu Zhe import com.sun.star.table.XCellRange; 53e6e6073dSLiu 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; 57e6e6073dSLiu Zhe import com.sun.star.table.XTableColumns; 58e6e6073dSLiu Zhe import com.sun.star.table.XTableRows; 59e6e6073dSLiu Zhe import com.sun.star.text.XText; 60e6e6073dSLiu Zhe import com.sun.star.uno.UnoRuntime; 61e6e6073dSLiu Zhe import com.sun.star.util.XCloseable; 62e6e6073dSLiu Zhe 63e6e6073dSLiu Zhe 64e6e6073dSLiu Zhe /** 65e6e6073dSLiu Zhe * Utilities of Spreadsheet 66e6e6073dSLiu Zhe * 67e6e6073dSLiu Zhe */ 68e6e6073dSLiu Zhe 69e6e6073dSLiu Zhe public class SCUtil { 70e6e6073dSLiu Zhe 71e6e6073dSLiu Zhe private static final String scTempDir = "output/sc/"; //Spreadsheet temp file directory 72e6e6073dSLiu Zhe private static HashMap filterName = new HashMap(); 73e6e6073dSLiu Zhe SCUtil()74e6e6073dSLiu Zhe private SCUtil() { 75e6e6073dSLiu Zhe 76e6e6073dSLiu Zhe } 77e6e6073dSLiu Zhe 78e6e6073dSLiu Zhe /** 79e6e6073dSLiu Zhe * Get spreadsheet document object 80e6e6073dSLiu Zhe * @param xSpreadsheetComponent 81e6e6073dSLiu Zhe * @return 82e6e6073dSLiu Zhe * @throws Exception 83e6e6073dSLiu Zhe */ getSCDocument(XComponent xSpreadsheetComponent)84e6e6073dSLiu Zhe public static XSpreadsheetDocument getSCDocument(XComponent xSpreadsheetComponent) throws Exception { 85e6e6073dSLiu Zhe XSpreadsheetDocument xSpreadsheetDocument = 86e6e6073dSLiu Zhe (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, xSpreadsheetComponent); 87e6e6073dSLiu Zhe 88e6e6073dSLiu Zhe return xSpreadsheetDocument; 89e6e6073dSLiu Zhe } 90e6e6073dSLiu Zhe 91e6e6073dSLiu Zhe /** 92e6e6073dSLiu Zhe * Get sheet object by sheet name 93e6e6073dSLiu Zhe * @param xSpreadsheetDocument 94e6e6073dSLiu Zhe * @param sheetName 95e6e6073dSLiu Zhe * @return 96e6e6073dSLiu Zhe * @throws Exception 97e6e6073dSLiu Zhe */ getSCSheetByName(XSpreadsheetDocument xSpreadsheetDocument, String sheetName)98e6e6073dSLiu Zhe public static XSpreadsheet getSCSheetByName(XSpreadsheetDocument xSpreadsheetDocument, String sheetName) throws Exception { 99e6e6073dSLiu Zhe XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets(); 100e6e6073dSLiu Zhe XSpreadsheet xSpreadsheet = 101e6e6073dSLiu Zhe (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, xSpreadsheets.getByName(sheetName)); 102e6e6073dSLiu Zhe 103e6e6073dSLiu Zhe return xSpreadsheet; 104e6e6073dSLiu Zhe } 105e6e6073dSLiu Zhe 106e6e6073dSLiu Zhe /** 107e6e6073dSLiu Zhe * Get sheet object by sheet index 108e6e6073dSLiu Zhe * @param xSpreadsheetDocument 109e6e6073dSLiu Zhe * @param index (Short) 0,1,2,... 110e6e6073dSLiu Zhe * @return 111e6e6073dSLiu Zhe * @throws Exception 112e6e6073dSLiu Zhe */ getSCSheetByIndex(XSpreadsheetDocument xSpreadsheetDocument, short index)113e6e6073dSLiu Zhe public static XSpreadsheet getSCSheetByIndex(XSpreadsheetDocument xSpreadsheetDocument, short index) throws Exception { 114e6e6073dSLiu Zhe XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets(); 115e6e6073dSLiu Zhe XIndexAccess xIndexAccess = 116e6e6073dSLiu Zhe (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets); 117e6e6073dSLiu Zhe XSpreadsheet xSpreadsheet = 118e6e6073dSLiu Zhe (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, xIndexAccess.getByIndex(index)); 119e6e6073dSLiu Zhe 120e6e6073dSLiu Zhe return xSpreadsheet; 121e6e6073dSLiu Zhe } 122e6e6073dSLiu Zhe 123e6e6073dSLiu Zhe /** 124e6e6073dSLiu Zhe * Get sheet name by sheet index 125e6e6073dSLiu Zhe * 126e6e6073dSLiu Zhe * @param xSpreadsheetDocument 127e6e6073dSLiu Zhe * @param index 128e6e6073dSLiu Zhe * (Short) 0,1,2,... 129e6e6073dSLiu Zhe * @return 130e6e6073dSLiu Zhe * @throws Exception 131e6e6073dSLiu Zhe */ getSCSheetNameByIndex( XSpreadsheetDocument xSpreadsheetDocument, short index)132e6e6073dSLiu Zhe public static String getSCSheetNameByIndex( 133e6e6073dSLiu Zhe XSpreadsheetDocument xSpreadsheetDocument, short index) 134e6e6073dSLiu Zhe throws Exception { 135e6e6073dSLiu Zhe XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets(); 136e6e6073dSLiu Zhe XIndexAccess xIndexAccess = (XIndexAccess) UnoRuntime.queryInterface( 137e6e6073dSLiu Zhe XIndexAccess.class, xSpreadsheets); 138e6e6073dSLiu Zhe XSpreadsheet xSpreadsheet = (XSpreadsheet) UnoRuntime.queryInterface( 139e6e6073dSLiu Zhe XSpreadsheet.class, xIndexAccess.getByIndex(index)); 140e6e6073dSLiu Zhe XNamed xsheetname = (XNamed) UnoRuntime.queryInterface(XNamed.class, 141e6e6073dSLiu Zhe xSpreadsheet); 142e6e6073dSLiu Zhe return xsheetname.getName(); 143e6e6073dSLiu Zhe } 144e6e6073dSLiu Zhe 145e6e6073dSLiu Zhe /** 146e6e6073dSLiu Zhe * Set sheet name by sheet index 147e6e6073dSLiu Zhe * 148e6e6073dSLiu Zhe * @param xSpreadsheetDocument 149e6e6073dSLiu Zhe * @param index 150e6e6073dSLiu Zhe * (Short) 0,1,2,... 151e6e6073dSLiu Zhe * @return 152e6e6073dSLiu Zhe * @throws Exception 153e6e6073dSLiu Zhe */ setSCSheetNameByIndex( XSpreadsheetDocument xSpreadsheetDocument, short index, String sheetname)154e6e6073dSLiu Zhe public static void setSCSheetNameByIndex( 155e6e6073dSLiu Zhe XSpreadsheetDocument xSpreadsheetDocument, short index, 156e6e6073dSLiu Zhe String sheetname) throws Exception { 157e6e6073dSLiu Zhe XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets(); 158e6e6073dSLiu Zhe XIndexAccess xIndexAccess = (XIndexAccess) UnoRuntime.queryInterface( 159e6e6073dSLiu Zhe XIndexAccess.class, xSpreadsheets); 160e6e6073dSLiu Zhe XSpreadsheet xSpreadsheet = (XSpreadsheet) UnoRuntime.queryInterface( 161e6e6073dSLiu Zhe XSpreadsheet.class, xIndexAccess.getByIndex(index)); 162e6e6073dSLiu Zhe XNamed xsheetname = (XNamed) UnoRuntime.queryInterface(XNamed.class, 163e6e6073dSLiu Zhe xSpreadsheet); 164e6e6073dSLiu Zhe xsheetname.setName(sheetname); 165e6e6073dSLiu Zhe } 166e6e6073dSLiu Zhe 167e6e6073dSLiu Zhe /** 168e6e6073dSLiu Zhe * Get rows object 169e6e6073dSLiu Zhe * @param xSpreadsheet 170e6e6073dSLiu Zhe * @return 171e6e6073dSLiu Zhe * @throws Exception 172e6e6073dSLiu Zhe */ getSCRows(XSpreadsheet xSpreadsheet)173e6e6073dSLiu Zhe public static XTableRows getSCRows(XSpreadsheet xSpreadsheet) throws Exception { 174e6e6073dSLiu Zhe XColumnRowRange xColumnRowRange = 175e6e6073dSLiu Zhe (XColumnRowRange) UnoRuntime.queryInterface(XColumnRowRange.class, xSpreadsheet); 176e6e6073dSLiu Zhe XTableRows xTableRows = xColumnRowRange.getRows(); 177e6e6073dSLiu Zhe 178e6e6073dSLiu Zhe return xTableRows; 179e6e6073dSLiu Zhe } 180e6e6073dSLiu Zhe 181e6e6073dSLiu Zhe /** 182e6e6073dSLiu Zhe * Get columns object 183e6e6073dSLiu Zhe * @param xSpreadsheet 184e6e6073dSLiu Zhe * @return 185e6e6073dSLiu Zhe * @throws Exception 186e6e6073dSLiu Zhe */ getSCColumns(XSpreadsheet xSpreadsheet)187e6e6073dSLiu Zhe public static XTableColumns getSCColumns(XSpreadsheet xSpreadsheet) throws Exception { 188e6e6073dSLiu Zhe XColumnRowRange xColumnRowRange = 189e6e6073dSLiu Zhe (XColumnRowRange) UnoRuntime.queryInterface(XColumnRowRange.class, xSpreadsheet); 190e6e6073dSLiu Zhe XTableColumns xTableColumns = xColumnRowRange.getColumns(); 191e6e6073dSLiu Zhe 192e6e6073dSLiu Zhe return xTableColumns; 193e6e6073dSLiu Zhe } 194e6e6073dSLiu Zhe 195e6e6073dSLiu Zhe /** 196e6e6073dSLiu Zhe * Set floating number into specific cell 197e6e6073dSLiu Zhe * @param xSpreadsheet 198e6e6073dSLiu Zhe * @param column 199e6e6073dSLiu Zhe * @param row 200e6e6073dSLiu Zhe * @param value 201e6e6073dSLiu Zhe * @throws Exception 202e6e6073dSLiu Zhe */ setValueToCell(XSpreadsheet xSpreadsheet, int column, int row, double value)203e6e6073dSLiu Zhe public static void setValueToCell(XSpreadsheet xSpreadsheet, int column, int row, double value) throws Exception { 204e6e6073dSLiu Zhe XCell xCell = xSpreadsheet.getCellByPosition(column, row); 205e6e6073dSLiu Zhe xCell.setValue(value); 206e6e6073dSLiu Zhe } 207e6e6073dSLiu Zhe 208e6e6073dSLiu Zhe /** 209e6e6073dSLiu Zhe * Set text into specific cell 210e6e6073dSLiu Zhe * @param xSpreadsheet 211e6e6073dSLiu Zhe * @param column 212e6e6073dSLiu Zhe * @param row 213e6e6073dSLiu Zhe * @param text 214e6e6073dSLiu Zhe * @throws Exception 215e6e6073dSLiu Zhe */ setTextToCell(XSpreadsheet xSpreadsheet, int column, int row, String text)216e6e6073dSLiu Zhe public static void setTextToCell(XSpreadsheet xSpreadsheet, int column, int row, String text) throws Exception { 217e6e6073dSLiu Zhe XCell xCell = xSpreadsheet.getCellByPosition(column, row); 218e6e6073dSLiu Zhe XText xText = (XText) UnoRuntime.queryInterface(XText.class, xCell); 219e6e6073dSLiu Zhe xText.setString(text); 220e6e6073dSLiu Zhe } 221e6e6073dSLiu Zhe 222e6e6073dSLiu Zhe /** 223e6e6073dSLiu Zhe * Set text into specific cell 224e6e6073dSLiu Zhe * @param xCell 225e6e6073dSLiu Zhe * @param text 226e6e6073dSLiu Zhe * @throws Exception 227e6e6073dSLiu Zhe */ setTextToCell(XCell xCell, String text)228e6e6073dSLiu Zhe public static void setTextToCell(XCell xCell, String text) throws Exception { 229e6e6073dSLiu Zhe XText xText = (XText) UnoRuntime.queryInterface(XText.class, xCell); 230e6e6073dSLiu Zhe xText.setString(text); 231e6e6073dSLiu Zhe } 232e6e6073dSLiu Zhe 233e6e6073dSLiu Zhe /** 234e6e6073dSLiu Zhe * Set formula into specific cell 235e6e6073dSLiu Zhe * @param xSpreadsheet 236e6e6073dSLiu Zhe * @param column 237e6e6073dSLiu Zhe * @param row 238e6e6073dSLiu Zhe * @param formula 239e6e6073dSLiu Zhe * @throws Exception 240e6e6073dSLiu Zhe */ setFormulaToCell(XSpreadsheet xSpreadsheet, int column, int row, String formula)241e6e6073dSLiu Zhe public static void setFormulaToCell(XSpreadsheet xSpreadsheet, int column, int row, String formula) throws Exception { 242e6e6073dSLiu Zhe XCell xCell = xSpreadsheet.getCellByPosition(column, row); 243e6e6073dSLiu Zhe xCell.setFormula(formula); 244e6e6073dSLiu Zhe } 245e6e6073dSLiu Zhe 246e6e6073dSLiu Zhe /** 247e6e6073dSLiu Zhe * Get value from specific cell 248e6e6073dSLiu Zhe * @param xSpreadsheet 249e6e6073dSLiu Zhe * @param column 250e6e6073dSLiu Zhe * @param row 251e6e6073dSLiu Zhe * @return 252e6e6073dSLiu Zhe * @throws Exception 253e6e6073dSLiu Zhe */ getValueFromCell(XSpreadsheet xSpreadsheet, int column, int row)254e6e6073dSLiu Zhe public static double getValueFromCell(XSpreadsheet xSpreadsheet, int column, int row) throws Exception { 255e6e6073dSLiu Zhe XCell xCell = xSpreadsheet.getCellByPosition(column, row); 256e6e6073dSLiu Zhe double cellValue = xCell.getValue(); 257e6e6073dSLiu Zhe 258e6e6073dSLiu Zhe return cellValue; 259e6e6073dSLiu Zhe } 260e6e6073dSLiu Zhe 261e6e6073dSLiu Zhe /** 262e6e6073dSLiu Zhe * Get text from specific cell 263e6e6073dSLiu Zhe * @param xSpreadsheet 264e6e6073dSLiu Zhe * @param column 265e6e6073dSLiu Zhe * @param row 266e6e6073dSLiu Zhe * 267e6e6073dSLiu Zhe * @return 268e6e6073dSLiu Zhe * @throws Exception 269e6e6073dSLiu Zhe */ getTextFromCell(XSpreadsheet xSpreadsheet, int column, int row)270e6e6073dSLiu Zhe public static String getTextFromCell(XSpreadsheet xSpreadsheet, int column, int row) throws Exception { 271e6e6073dSLiu Zhe XCell xCell = xSpreadsheet.getCellByPosition(column, row); 272e6e6073dSLiu Zhe XText xText = (XText) UnoRuntime.queryInterface(XText.class, xCell); 273e6e6073dSLiu Zhe 274e6e6073dSLiu Zhe return xText.getString(); 275e6e6073dSLiu Zhe } 276e6e6073dSLiu Zhe 277e6e6073dSLiu Zhe /** 278e6e6073dSLiu Zhe * Get formula string from specific cell 279e6e6073dSLiu Zhe * @param xSpreadsheet 280e6e6073dSLiu Zhe * @param column 281e6e6073dSLiu Zhe * @param row 282e6e6073dSLiu Zhe * @return 283e6e6073dSLiu Zhe * @throws Exception 284e6e6073dSLiu Zhe */ getFormulaFromCell(XSpreadsheet xSpreadsheet, int column, int row)285e6e6073dSLiu Zhe public static String getFormulaFromCell(XSpreadsheet xSpreadsheet, int column, int row) throws Exception { 286e6e6073dSLiu Zhe XCell xCell = xSpreadsheet.getCellByPosition(column, row); 287e6e6073dSLiu Zhe String cellFormula = xCell.getFormula(); 288e6e6073dSLiu Zhe 289e6e6073dSLiu Zhe return cellFormula; 290e6e6073dSLiu Zhe } 291e6e6073dSLiu Zhe 292e6e6073dSLiu Zhe /** 293e6e6073dSLiu Zhe * Set numbers into a cell range 294e6e6073dSLiu Zhe * @param xSpreadsheet 295e6e6073dSLiu Zhe * @param start_col 296e6e6073dSLiu Zhe * @param start_row 297e6e6073dSLiu Zhe * @param end_col 298e6e6073dSLiu Zhe * @param end_row 299e6e6073dSLiu Zhe * @param values 300e6e6073dSLiu Zhe * @throws Exception 301e6e6073dSLiu Zhe */ 3021dfd36f5SLiu Zhe @Deprecated setValueToCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row, double[][] values)303e6e6073dSLiu Zhe public static void setValueToCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row, double[][] values) throws Exception { 304e6e6073dSLiu Zhe XCellRange xCellRange = xSpreadsheet.getCellRangeByPosition(start_col, start_row, end_col, end_row); 305e6e6073dSLiu Zhe XCell xCell = null; 306e6e6073dSLiu Zhe for (int i = 0; i <= (end_row - start_row); i++ ) { 307e6e6073dSLiu Zhe for(int j = 0; j <= (end_col - start_col); j++) { 308e6e6073dSLiu Zhe xCell = xCellRange.getCellByPosition(j, i); 309e6e6073dSLiu Zhe xCell.setValue(values[i][j]); 310e6e6073dSLiu Zhe } 311e6e6073dSLiu Zhe } 312e6e6073dSLiu Zhe } 313e6e6073dSLiu 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 325e6e6073dSLiu Zhe /** 326e6e6073dSLiu Zhe * Set text into 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 * @param texts 333e6e6073dSLiu Zhe * @throws Exception 334e6e6073dSLiu Zhe */ 3351dfd36f5SLiu Zhe @Deprecated setTextToCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row, String[][] texts)336e6e6073dSLiu Zhe public static void setTextToCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row, String[][] texts) throws Exception { 337e6e6073dSLiu Zhe XCellRange xCellRange = xSpreadsheet.getCellRangeByPosition(start_col, start_row, end_col, end_row); 338e6e6073dSLiu Zhe XCell xCell = null; 339e6e6073dSLiu Zhe XText xText = null; 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 xText = (XText) UnoRuntime.queryInterface(XText.class, xCell); 344e6e6073dSLiu Zhe xText.setString(texts[i][j]); 345e6e6073dSLiu Zhe } 346e6e6073dSLiu Zhe } 347e6e6073dSLiu Zhe } 348e6e6073dSLiu 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 362e6e6073dSLiu Zhe /** 363e6e6073dSLiu Zhe * Get number content from a cell range 364e6e6073dSLiu Zhe * @param xSpreadsheet 365e6e6073dSLiu Zhe * @param start_col 366e6e6073dSLiu Zhe * @param start_row 367e6e6073dSLiu Zhe * @param end_col 368e6e6073dSLiu Zhe * @param end_row 369e6e6073dSLiu Zhe * @return 370e6e6073dSLiu Zhe * @throws Exception 371e6e6073dSLiu Zhe */ getValueFromCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row)372e6e6073dSLiu Zhe public static double[][] getValueFromCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row) throws Exception { 373e6e6073dSLiu Zhe XCellRange xCellRange = xSpreadsheet.getCellRangeByPosition(start_col, start_row, end_col, end_row); 374e6e6073dSLiu Zhe XCell xCell = null; 375e6e6073dSLiu Zhe double[][] cellValues = new double[end_row - start_row+1][end_col - start_col +1]; 376e6e6073dSLiu Zhe 377e6e6073dSLiu Zhe for (int i = 0; i <= (end_row - start_row); i++ ) { 378e6e6073dSLiu Zhe for(int j = 0; j <= (end_col - start_col); j++) { 379e6e6073dSLiu Zhe xCell = xCellRange.getCellByPosition(j, i); 380e6e6073dSLiu Zhe cellValues[i][j] = xCell.getValue(); 381e6e6073dSLiu Zhe } 382e6e6073dSLiu Zhe } 383e6e6073dSLiu Zhe 384e6e6073dSLiu Zhe return cellValues; 385e6e6073dSLiu Zhe } 386e6e6073dSLiu Zhe 387e6e6073dSLiu Zhe /** 388e6e6073dSLiu Zhe * Get text content from a cell range 389e6e6073dSLiu Zhe * @param xSpreadsheet 390e6e6073dSLiu Zhe * @param start_col 391e6e6073dSLiu Zhe * @param start_row 392e6e6073dSLiu Zhe * @param end_col 393e6e6073dSLiu Zhe * @param end_row 394e6e6073dSLiu Zhe * @return 395e6e6073dSLiu Zhe * @throws Exception 396e6e6073dSLiu Zhe */ getTextFromCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row)397e6e6073dSLiu Zhe public static String[][] getTextFromCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row) throws Exception { 398e6e6073dSLiu Zhe XCellRange xCellRange = xSpreadsheet.getCellRangeByPosition(start_col, start_row, end_col, end_row); 399e6e6073dSLiu Zhe XCell xCell = null; 400e6e6073dSLiu Zhe XText xText = null; 401e6e6073dSLiu Zhe String[][] cellTexts = new String[end_row - start_row+1][end_col - start_col +1]; 402e6e6073dSLiu Zhe 403e6e6073dSLiu Zhe for (int i = 0; i <= (end_row - start_row); i++ ) { 404e6e6073dSLiu Zhe for (int j = 0; j <= (end_col - start_col); j++) { 405e6e6073dSLiu Zhe xCell = xCellRange.getCellByPosition(j, i); 406e6e6073dSLiu Zhe xText = (XText) UnoRuntime.queryInterface(XText.class, xCell); 407e6e6073dSLiu Zhe cellTexts[i][j] = xText.getString(); 408e6e6073dSLiu Zhe } 409e6e6073dSLiu Zhe } 410e6e6073dSLiu Zhe 411e6e6073dSLiu Zhe return cellTexts; 412e6e6073dSLiu Zhe } 413e6e6073dSLiu Zhe 414e6e6073dSLiu Zhe //TODO ZS - public static String[][] getAllFromCellRange 415e6e6073dSLiu Zhe 416e6e6073dSLiu Zhe /** 417e6e6073dSLiu Zhe * Switch to specific sheet 418e6e6073dSLiu Zhe * @param xSpreadsheetDocument 419e6e6073dSLiu Zhe * @param xSpreadsheet 420e6e6073dSLiu Zhe */ setCurrentSheet(XSpreadsheetDocument xSpreadsheetDocument, XSpreadsheet xSpreadsheet)421e6e6073dSLiu Zhe public static void setCurrentSheet(XSpreadsheetDocument xSpreadsheetDocument, XSpreadsheet xSpreadsheet) throws Exception { 422e6e6073dSLiu Zhe XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, xSpreadsheetDocument); 423e6e6073dSLiu Zhe XController xController = xModel.getCurrentController(); 424e6e6073dSLiu Zhe XSpreadsheetView xSpreadsheetView = (XSpreadsheetView) UnoRuntime.queryInterface(XSpreadsheetView.class, xController); 425e6e6073dSLiu Zhe xSpreadsheetView.setActiveSheet(xSpreadsheet); 426e6e6073dSLiu Zhe } 427e6e6073dSLiu Zhe 428e6e6073dSLiu Zhe /** 429e6e6073dSLiu Zhe * Get sheet object of current active sheet 430e6e6073dSLiu Zhe * @param xSpreadsheetDocument 431e6e6073dSLiu Zhe * @return 432e6e6073dSLiu Zhe */ getCurrentSheet(XSpreadsheetDocument xSpreadsheetDocument)433e6e6073dSLiu Zhe public static XSpreadsheet getCurrentSheet(XSpreadsheetDocument xSpreadsheetDocument) throws Exception { 434e6e6073dSLiu Zhe XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, xSpreadsheetDocument); 435e6e6073dSLiu Zhe XController xController = xModel.getCurrentController(); 436e6e6073dSLiu Zhe XSpreadsheetView xSpreadsheetView = (XSpreadsheetView) UnoRuntime.queryInterface(XSpreadsheetView.class, xController); 437e6e6073dSLiu Zhe XSpreadsheet xSpreadsheet = xSpreadsheetView.getActiveSheet(); 438e6e6073dSLiu Zhe 439e6e6073dSLiu Zhe return xSpreadsheet; 440e6e6073dSLiu Zhe } 441e6e6073dSLiu Zhe 442e6e6073dSLiu Zhe /** 443e6e6073dSLiu Zhe * Get sheet object by sheet index 444e6e6073dSLiu Zhe * 445e6e6073dSLiu Zhe * @param xSpreadsheetDocument 446e6e6073dSLiu Zhe * @return 447e6e6073dSLiu Zhe * @throws Exception 448e6e6073dSLiu Zhe */ getSCActiveSheetName( XSpreadsheetDocument xSpreadsheetDocument)449e6e6073dSLiu Zhe public static String getSCActiveSheetName( 450e6e6073dSLiu Zhe XSpreadsheetDocument xSpreadsheetDocument) throws Exception { 451e6e6073dSLiu Zhe XModel xSpreadsheetModel = (XModel) UnoRuntime.queryInterface( 452e6e6073dSLiu Zhe XModel.class, xSpreadsheetDocument); 453e6e6073dSLiu Zhe XSpreadsheetView xSpeadsheetView = (XSpreadsheetView) UnoRuntime 454e6e6073dSLiu Zhe .queryInterface(XSpreadsheetView.class, 455e6e6073dSLiu Zhe xSpreadsheetModel.getCurrentController()); 456e6e6073dSLiu Zhe XSpreadsheet activesheet = xSpeadsheetView.getActiveSheet(); 457e6e6073dSLiu Zhe XNamed activesheetName = (XNamed) UnoRuntime.queryInterface( 458e6e6073dSLiu Zhe XNamed.class, activesheet); 459e6e6073dSLiu Zhe return activesheetName.getName(); 460e6e6073dSLiu Zhe } 461e6e6073dSLiu Zhe 462e6e6073dSLiu 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 4901dfd36f5SLiu Zhe /** 491e6e6073dSLiu Zhe * Set value of specific property from a cell 492e6e6073dSLiu Zhe * @param xCell 493e6e6073dSLiu Zhe * @param propName 494e6e6073dSLiu Zhe * @param value 495e6e6073dSLiu Zhe * @throws Exception 496e6e6073dSLiu Zhe */ setCellProperties(XCell xCell, String propName, Object value)497e6e6073dSLiu Zhe public static void setCellProperties(XCell xCell, String propName, Object value) throws Exception { 498e6e6073dSLiu Zhe 4991dfd36f5SLiu Zhe setProperties(xCell, propName, value); 500e6e6073dSLiu Zhe } 501e6e6073dSLiu Zhe 502e6e6073dSLiu Zhe /** 503e6e6073dSLiu Zhe * Get value of specific property from a cell 504e6e6073dSLiu Zhe * @param xCell 505e6e6073dSLiu Zhe * @param propName 506e6e6073dSLiu Zhe * @return 507e6e6073dSLiu Zhe * @throws Exception 508e6e6073dSLiu Zhe */ getCellProperties(XCell xCell, String propName)509e6e6073dSLiu Zhe public static Object getCellProperties(XCell xCell, String propName) throws Exception { 5101dfd36f5SLiu Zhe return getProperties(xCell, propName); 511e6e6073dSLiu Zhe } 512e6e6073dSLiu Zhe 513e6e6073dSLiu Zhe /** 514e6e6073dSLiu Zhe * Clear temp file directory 515e6e6073dSLiu Zhe */ clearTempDir()516e6e6073dSLiu Zhe public static void clearTempDir() { 517e6e6073dSLiu Zhe FileUtil.deleteFile(Testspace.getFile(Testspace.getPath(scTempDir))); 518e6e6073dSLiu Zhe } 519e6e6073dSLiu Zhe 520e6e6073dSLiu Zhe /** 521e6e6073dSLiu Zhe * Save file as specific file format into spreadsheet temp file folder. 522e6e6073dSLiu Zhe * @param scComponent 523e6e6073dSLiu Zhe * @param fileName File name string without extension name (e.g. "sampleFile") 524e6e6073dSLiu Zhe * @param extName ("ods", "ots", "xls", "xlt", "csv") 525e6e6073dSLiu Zhe * @throws Exception 526e6e6073dSLiu Zhe */ saveFileAs(XComponent scComponent, String fileName, String extName)527e6e6073dSLiu Zhe public static void saveFileAs(XComponent scComponent, String fileName, String extName) throws Exception { 528e6e6073dSLiu Zhe 529e6e6073dSLiu Zhe initFilterName(); 530e6e6073dSLiu Zhe 531e6e6073dSLiu Zhe String storeUrl = Testspace.getUrl(scTempDir + fileName + "." + extName); 532e6e6073dSLiu Zhe 533e6e6073dSLiu Zhe PropertyValue[] storeProps = new PropertyValue[2]; 534e6e6073dSLiu Zhe storeProps[0] = new PropertyValue(); 535e6e6073dSLiu Zhe storeProps[0].Name = "FilterName"; 536e6e6073dSLiu Zhe storeProps[0].Value = filterName.get(extName); 537e6e6073dSLiu Zhe storeProps[1] = new PropertyValue(); 538e6e6073dSLiu Zhe storeProps[1].Name = "Overwrite"; 539e6e6073dSLiu Zhe storeProps[1].Value = new Boolean(true); 540e6e6073dSLiu Zhe 541e6e6073dSLiu Zhe XStorable scStorable = 542e6e6073dSLiu Zhe (XStorable) UnoRuntime.queryInterface(XStorable.class, scComponent); 543e6e6073dSLiu Zhe scStorable.storeAsURL(storeUrl, storeProps); 544e6e6073dSLiu Zhe } 545e6e6073dSLiu Zhe 546e6e6073dSLiu Zhe /** 547e6e6073dSLiu Zhe * Save file after open file. 548e6e6073dSLiu Zhe * @param xSpreadsheetDocument 549e6e6073dSLiu Zhe * @throws Exception 550e6e6073dSLiu Zhe */ save(XSpreadsheetDocument xSpreadsheetDocument)551e6e6073dSLiu Zhe public static void save(XSpreadsheetDocument xSpreadsheetDocument) 552e6e6073dSLiu Zhe throws Exception { 553e6e6073dSLiu Zhe XStorable scStorable = (XStorable) UnoRuntime.queryInterface( 554e6e6073dSLiu Zhe XStorable.class, xSpreadsheetDocument); 555e6e6073dSLiu Zhe scStorable.store(); 556e6e6073dSLiu Zhe } 557e6e6073dSLiu Zhe 558e6e6073dSLiu Zhe 559e6e6073dSLiu Zhe /** 560e6e6073dSLiu Zhe * Close specific opening spreadsheet file which has been saved 561e6e6073dSLiu Zhe * @param xSpreadsheetDocument 562e6e6073dSLiu Zhe * @throws Exception 563e6e6073dSLiu Zhe */ closeFile(XSpreadsheetDocument xSpreadsheetDocument)564e6e6073dSLiu Zhe public static void closeFile(XSpreadsheetDocument xSpreadsheetDocument) throws Exception { 565e6e6073dSLiu Zhe XCloseable xCloseable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, xSpreadsheetDocument); 566e6e6073dSLiu Zhe xCloseable.close(false); 567e6e6073dSLiu Zhe } 568e6e6073dSLiu Zhe 569e6e6073dSLiu Zhe /** 570e6e6073dSLiu Zhe * Close a opening file saved in spreadsheet temp file direction and reopen it in Spreadsheet. For save&reload test scenario only. 571e6e6073dSLiu Zhe * @param unoApp 572e6e6073dSLiu Zhe * @param xSpreadsheetDocument 573e6e6073dSLiu Zhe * @param fullFileName File name with the extension name. (e.g. "sc.ods") 574e6e6073dSLiu Zhe * @return 575e6e6073dSLiu Zhe * @throws Exception 576e6e6073dSLiu Zhe */ reloadFile(UnoApp unoApp, XSpreadsheetDocument xSpreadsheetDocument, String fullFileName)577e6e6073dSLiu Zhe public static XSpreadsheetDocument reloadFile(UnoApp unoApp, XSpreadsheetDocument xSpreadsheetDocument, String fullFileName) throws Exception { 578e6e6073dSLiu Zhe closeFile(xSpreadsheetDocument); 579e6e6073dSLiu Zhe 580e6e6073dSLiu Zhe String filePath = Testspace.getPath(scTempDir + fullFileName); 581cebb507aSLiu Zhe XSpreadsheetDocument xScDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, unoApp.loadDocument(filePath)); 582e6e6073dSLiu Zhe 583e6e6073dSLiu Zhe return xScDocument; 584e6e6073dSLiu Zhe } 585e6e6073dSLiu Zhe 586e6e6073dSLiu 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 5979fdcf9fdSLiu Zhe /** 598e6e6073dSLiu Zhe * Initial the filter name list 599e6e6073dSLiu Zhe * @throws Exception 600e6e6073dSLiu Zhe */ initFilterName()601e6e6073dSLiu Zhe private static void initFilterName() throws Exception { 602e6e6073dSLiu Zhe if (filterName.size() > 0) { 603e6e6073dSLiu Zhe return; 604e6e6073dSLiu Zhe } 605e6e6073dSLiu Zhe 606e6e6073dSLiu Zhe filterName.put("ods", "calc8"); 607e6e6073dSLiu Zhe filterName.put("ots", "calc8_template"); 608e6e6073dSLiu Zhe filterName.put("xls", "MS Excel 97"); 609e6e6073dSLiu Zhe filterName.put("xlt", "MS Excel 97 Vorlage/Template"); 610e6e6073dSLiu Zhe filterName.put("csv", "Text - txt - csv (StarCalc)"); 611e6e6073dSLiu Zhe } 612e6e6073dSLiu 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 7451dfd36f5SLiu Zhe 746e6e6073dSLiu Zhe } 747