1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 package ifc.sheet; 28 29 import com.sun.star.container.XIndexAccess; 30 import com.sun.star.sheet.GoalResult; 31 import com.sun.star.sheet.XGoalSeek; 32 import com.sun.star.sheet.XSpreadsheet; 33 import com.sun.star.sheet.XSpreadsheetDocument; 34 import com.sun.star.sheet.XSpreadsheets; 35 import com.sun.star.table.CellAddress; 36 import com.sun.star.uno.UnoRuntime; 37 import lib.MultiMethodTest; 38 import lib.StatusException; 39 40 /** 41 * 42 */ 43 public class _XGoalSeek extends MultiMethodTest { 44 public XGoalSeek oObj = null; 45 XSpreadsheet xSheet = null; 46 CellAddress aFormula = null; 47 CellAddress aValue = null; 48 49 public void before() { 50 Exception ex = null; 51 // get two sheets 52 try { 53 XSpreadsheetDocument xSpreadsheetDocument = (XSpreadsheetDocument) 54 UnoRuntime.queryInterface(XSpreadsheetDocument.class, oObj); 55 XSpreadsheets oSheets = xSpreadsheetDocument.getSheets(); 56 XIndexAccess oIndexSheets = (XIndexAccess) UnoRuntime.queryInterface( 57 XIndexAccess.class, oSheets); 58 xSheet = (XSpreadsheet) UnoRuntime.queryInterface( 59 XSpreadsheet.class, oIndexSheets.getByIndex(1)); 60 } 61 catch(com.sun.star.lang.IndexOutOfBoundsException e) { 62 ex = e; 63 } 64 catch(com.sun.star.lang.WrappedTargetException e) { 65 ex = e; 66 } 67 catch(java.lang.NullPointerException e) { 68 ex = e; 69 } 70 if (ex != null) { 71 throw new StatusException("Could not get a sheet.", ex); 72 } 73 74 // set value and formula 75 try { 76 xSheet.getCellByPosition(3, 4).setValue(9); 77 xSheet.getCellByPosition(3, 5).setFormula("= SQRT(D5)"); 78 aValue = new CellAddress((short)1, 3, 4); 79 aFormula = new CellAddress((short)1, 3, 5); 80 } 81 catch(Exception e) { 82 throw new StatusException("Could not get set formulas on the sheet.", e); 83 } 84 } 85 86 public void _seekGoal() { 87 boolean result = true; 88 double divergence = 0.01; 89 GoalResult goal = oObj.seekGoal(aFormula, aValue, "4"); 90 log.println("Goal Result: " + goal.Result + " Divergence: " + goal.Divergence); 91 result &= goal.Divergence < divergence; 92 result &= goal.Result > 16 - divergence || goal.Result < 16 + divergence; 93 94 goal = oObj.seekGoal(aFormula, aValue, "-4"); 95 log.println("Goal Result: " + goal.Result + " Divergence: " + goal.Divergence); 96 result &= goal.Divergence > 1/divergence; 97 result &= goal.Result < divergence || goal.Result > -divergence; 98 99 // just curious: let goal seek find a limiting value 100 try { 101 xSheet.getCellByPosition(3, 4).setValue(0.8); 102 xSheet.getCellByPosition(3, 5).setFormula("= (D5 ^ 2 - 1) / (D5 - 1)"); 103 } 104 catch(Exception e) {} 105 goal = oObj.seekGoal(aFormula, aValue, "2"); 106 log.println("Goal Result: " + goal.Result + " Divergence: " + goal.Divergence); 107 result &= goal.Divergence < divergence; 108 result &= goal.Result > 16 - divergence || goal.Result < 16 + divergence; 109 110 tRes.tested("seekGoal()", result); 111 } 112 } 113