1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************ 27*cdf0e10cSrcweir */ 28*cdf0e10cSrcweir 29*cdf0e10cSrcweir package util; 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir import com.sun.star.container.XIndexAccess; 32*cdf0e10cSrcweir import com.sun.star.lang.IllegalArgumentException; 33*cdf0e10cSrcweir import com.sun.star.lang.IndexOutOfBoundsException; 34*cdf0e10cSrcweir import com.sun.star.lang.WrappedTargetException; 35*cdf0e10cSrcweir import com.sun.star.lang.XComponent; 36*cdf0e10cSrcweir import com.sun.star.sheet.XCellRangeData; 37*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheet; 38*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument; 39*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheets; 40*cdf0e10cSrcweir import com.sun.star.table.XCellRange; 41*cdf0e10cSrcweir import com.sun.star.uno.AnyConverter; 42*cdf0e10cSrcweir import com.sun.star.uno.Exception; 43*cdf0e10cSrcweir import com.sun.star.uno.Type; 44*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir /** 47*cdf0e10cSrcweir * This class contains some usefull mathods to handle Calc documents 48*cdf0e10cSrcweir * and its sheets. 49*cdf0e10cSrcweir */ 50*cdf0e10cSrcweir public class CalcTools { 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir /** 53*cdf0e10cSrcweir * fills a range of a calc sheet with computed data of type 54*cdf0e10cSrcweir * <CODE>Double</CODE>. 55*cdf0e10cSrcweir * @param xSheetDoc the Clac documents wich should be filled 56*cdf0e10cSrcweir * @param sheetNumber the number of the sheet of <CODE>xSheetDoc</CODE> 57*cdf0e10cSrcweir * @param startCellX the cell number of the X start point (row) of the range to fill 58*cdf0e10cSrcweir * @param startCellY the cell number of the Y start point (column) of the range to fill 59*cdf0e10cSrcweir * @param rangeLengthX the size of the range expansion in X-direction 60*cdf0e10cSrcweir * @param rangeLengthY the size of the range expansion in Y-direction 61*cdf0e10cSrcweir * @throws java.lang.Exception on any error an <CODE>java.lang.Exception</CODE> was thrown 62*cdf0e10cSrcweir */ 63*cdf0e10cSrcweir public static void fillCalcSheetWithContent(XComponent xSheetDoc, int sheetNumber, 64*cdf0e10cSrcweir int startCellX, int startCellY, int rangeLengthX, int rangeLengthY) 65*cdf0e10cSrcweir throws java.lang.Exception { 66*cdf0e10cSrcweir try{ 67*cdf0e10cSrcweir XSpreadsheet xSheet = getSpreadSheetFromSheetDoc(xSheetDoc, sheetNumber); 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir fillCalcSheetWithContent(xSheet, startCellX, startCellY, rangeLengthX, rangeLengthY); 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir } catch (Exception e){ 72*cdf0e10cSrcweir throw new Exception( 73*cdf0e10cSrcweir "Couldn't fill CalcSheet with content: " + e.toString()); 74*cdf0e10cSrcweir } 75*cdf0e10cSrcweir } 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir /** 78*cdf0e10cSrcweir * fills a range of a calc sheet with computed data of type 79*cdf0e10cSrcweir * <CODE>Double</CODE>. 80*cdf0e10cSrcweir * @param xSheet the sheet to fill with content 81*cdf0e10cSrcweir * @param startCellX the cell number of the X start point (row) of the range to fill 82*cdf0e10cSrcweir * @param startCellY the cell number of the Y start point (column) of the range to fill 83*cdf0e10cSrcweir * @param rangeLengthX the size of the range expansion in X-direction 84*cdf0e10cSrcweir * @param rangeLengthY the size of the range expansion in Y-direction 85*cdf0e10cSrcweir * @throws java.lang.Exception on any error an <CODE>java.lang.Exception</CODE> was thrown 86*cdf0e10cSrcweir */ 87*cdf0e10cSrcweir public static void fillCalcSheetWithContent(XSpreadsheet xSheet, 88*cdf0e10cSrcweir int startCellX, int startCellY, int rangeLengthX, int rangeLengthY) 89*cdf0e10cSrcweir throws java.lang.Exception { 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir try{ 92*cdf0e10cSrcweir // create a range with content 93*cdf0e10cSrcweir Object[][] newData = new Object[rangeLengthY][rangeLengthX]; 94*cdf0e10cSrcweir for (int i=0; i<rangeLengthY; i++) { 95*cdf0e10cSrcweir for (int j=0; j<rangeLengthX; j++) { 96*cdf0e10cSrcweir newData[i][j] = new Double(10*i +j); 97*cdf0e10cSrcweir } 98*cdf0e10cSrcweir } 99*cdf0e10cSrcweir XCellRange xRange = null; 100*cdf0e10cSrcweir try { 101*cdf0e10cSrcweir xRange = xSheet.getCellRangeByPosition(startCellX, startCellY, 102*cdf0e10cSrcweir startCellX+rangeLengthX-1, startCellY+rangeLengthY-1); 103*cdf0e10cSrcweir } catch ( IndexOutOfBoundsException e){ 104*cdf0e10cSrcweir throw new Exception( 105*cdf0e10cSrcweir "Couldn't get CellRange from sheett: " + e.toString()); 106*cdf0e10cSrcweir } 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir XCellRangeData xRangeData = (XCellRangeData) UnoRuntime.queryInterface(XCellRangeData.class, xRange); 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir xRangeData.setDataArray(newData); 111*cdf0e10cSrcweir } catch (Exception e){ 112*cdf0e10cSrcweir throw new Exception( 113*cdf0e10cSrcweir "Couldn't fill CalcSheet with content: " + e.toString()); 114*cdf0e10cSrcweir } 115*cdf0e10cSrcweir } 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir /** 118*cdf0e10cSrcweir * 119*cdf0e10cSrcweir * returns an <CODE>XSpreadsheet</CODE> from a Calc document. 120*cdf0e10cSrcweir * @param xSheetDoc the Calc docuent which containes the sheet 121*cdf0e10cSrcweir * @param sheetNumber the number of the sheet to return 122*cdf0e10cSrcweir * @throws java.lang.Exception on any error an <CODE>java.lang.Exception</CODE> was thrown 123*cdf0e10cSrcweir * @return calc sheet 124*cdf0e10cSrcweir * @see com.sun.star.sheet.XSpreadsheet 125*cdf0e10cSrcweir */ 126*cdf0e10cSrcweir public static XSpreadsheet getSpreadSheetFromSheetDoc(XComponent xSheetDoc, int sheetNumber) 127*cdf0e10cSrcweir throws java.lang.Exception { 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir XSpreadsheet xSheet = null; 130*cdf0e10cSrcweir 131*cdf0e10cSrcweir try{ 132*cdf0e10cSrcweir XSpreadsheetDocument xSpreadsheetDoc = (XSpreadsheetDocument) 133*cdf0e10cSrcweir UnoRuntime.queryInterface(XSpreadsheetDocument.class, xSheetDoc); 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir XSpreadsheets xSpreadsheets = xSpreadsheetDoc.getSheets(); 136*cdf0e10cSrcweir 137*cdf0e10cSrcweir XIndexAccess xSheetsIndexArray = (XIndexAccess) 138*cdf0e10cSrcweir UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets); 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir try{ 141*cdf0e10cSrcweir xSheet = (XSpreadsheet) AnyConverter.toObject( 142*cdf0e10cSrcweir new Type(XSpreadsheet.class),xSheetsIndexArray.getByIndex(sheetNumber)); 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir } catch (IllegalArgumentException e){ 145*cdf0e10cSrcweir throw new Exception( 146*cdf0e10cSrcweir "Couldn't get sheet '" +sheetNumber + "' : " + e.toString()); 147*cdf0e10cSrcweir } catch (IndexOutOfBoundsException e){ 148*cdf0e10cSrcweir throw new Exception( 149*cdf0e10cSrcweir "Couldn't get sheet '" +sheetNumber + "' : " + e.toString()); 150*cdf0e10cSrcweir } catch (WrappedTargetException e){ 151*cdf0e10cSrcweir throw new Exception( 152*cdf0e10cSrcweir "Couldn't get sheet '" +sheetNumber + "' : " + e.toString()); 153*cdf0e10cSrcweir } 154*cdf0e10cSrcweir } catch (Exception e){ 155*cdf0e10cSrcweir throw new Exception( 156*cdf0e10cSrcweir "Couldn't get sheet '" +sheetNumber + "' : " + e.toString()); 157*cdf0e10cSrcweir } 158*cdf0e10cSrcweir return xSheet; 159*cdf0e10cSrcweir } 160*cdf0e10cSrcweir } 161