1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
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
;
43 public class _XGoalSeek
extends MultiMethodTest
{
44 public XGoalSeek oObj
= null;
45 XSpreadsheet xSheet
= null;
46 CellAddress aFormula
= null;
47 CellAddress aValue
= null;
49 public void before() {
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));
61 catch(com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
64 catch(com
.sun
.star
.lang
.WrappedTargetException e
) {
67 catch(java
.lang
.NullPointerException e
) {
71 throw new StatusException("Could not get a sheet.", ex
);
74 // set value and formula
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);
82 throw new StatusException("Could not get set formulas on the sheet.", e
);
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
;
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
;
99 // just curious: let goal seek find a limiting value
101 xSheet
.getCellByPosition(3, 4).setValue(0.8);
102 xSheet
.getCellByPosition(3, 5).setFormula("= (D5 ^ 2 - 1) / (D5 - 1)");
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
;
110 tRes
.tested("seekGoal()", result
);