2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 import com
.sun
.star
.container
.XIndexAccess
;
21 import com
.sun
.star
.sheet
.GoalResult
;
22 import com
.sun
.star
.sheet
.XGoalSeek
;
23 import com
.sun
.star
.sheet
.XSpreadsheet
;
24 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
25 import com
.sun
.star
.sheet
.XSpreadsheets
;
26 import com
.sun
.star
.table
.CellAddress
;
27 import com
.sun
.star
.uno
.UnoRuntime
;
28 import lib
.MultiMethodTest
;
29 import lib
.StatusException
;
34 public class _XGoalSeek
extends MultiMethodTest
{
35 public XGoalSeek oObj
= null;
36 XSpreadsheet xSheet
= null;
37 CellAddress aFormula
= null;
38 CellAddress aValue
= null;
40 public void before() {
44 XSpreadsheetDocument xSpreadsheetDocument
= UnoRuntime
.queryInterface(XSpreadsheetDocument
.class, oObj
);
45 XSpreadsheets oSheets
= xSpreadsheetDocument
.getSheets();
46 XIndexAccess oIndexSheets
= UnoRuntime
.queryInterface(
47 XIndexAccess
.class, oSheets
);
48 xSheet
= UnoRuntime
.queryInterface(
49 XSpreadsheet
.class, oIndexSheets
.getByIndex(1));
51 catch(com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
54 catch(com
.sun
.star
.lang
.WrappedTargetException e
) {
57 catch(java
.lang
.NullPointerException e
) {
61 throw new StatusException("Could not get a sheet.", ex
);
64 // set value and formula
66 xSheet
.getCellByPosition(3, 4).setValue(9);
67 xSheet
.getCellByPosition(3, 5).setFormula("= SQRT(D5)");
68 aValue
= new CellAddress((short)1, 3, 4);
69 aFormula
= new CellAddress((short)1, 3, 5);
72 throw new StatusException("Could not get set formulas on the sheet.", e
);
76 public void _seekGoal() {
77 boolean result
= true;
78 double divergence
= 0.01;
79 GoalResult goal
= oObj
.seekGoal(aFormula
, aValue
, "4");
80 log
.println("Goal Result: " + goal
.Result
+ " Divergence: " + goal
.Divergence
);
81 result
&= goal
.Divergence
< divergence
;
82 result
&= goal
.Result
> 16 - divergence
|| goal
.Result
< 16 + divergence
;
84 goal
= oObj
.seekGoal(aFormula
, aValue
, "-4");
85 log
.println("Goal Result: " + goal
.Result
+ " Divergence: " + goal
.Divergence
);
86 result
&= goal
.Divergence
> 1/divergence
;
87 result
&= goal
.Result
< divergence
|| goal
.Result
> -divergence
;
89 // just curious: let goal seek find a limiting value
91 xSheet
.getCellByPosition(3, 4).setValue(0.8);
92 xSheet
.getCellByPosition(3, 5).setFormula("= (D5 ^ 2 - 1) / (D5 - 1)");
95 goal
= oObj
.seekGoal(aFormula
, aValue
, "2");
96 log
.println("Goal Result: " + goal
.Result
+ " Divergence: " + goal
.Divergence
);
97 result
&= goal
.Divergence
< divergence
;
98 result
&= goal
.Result
> 16 - divergence
|| goal
.Result
< 16 + divergence
;
100 tRes
.tested("seekGoal()", result
);