View | Details | Raw Unified | Return to bug 60924
Collapse All | Expand All

(-)src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java (+2 lines)
Lines 133-138 Link Here
133
        r(m, "HEX2DEC", Hex2Dec.instance);
133
        r(m, "HEX2DEC", Hex2Dec.instance);
134
        r(m, "HEX2OCT", null);
134
        r(m, "HEX2OCT", null);
135
        r(m, "IFERROR", IfError.instance);
135
        r(m, "IFERROR", IfError.instance);
136
        r(m, "IFS", Ifs.instance);
136
        r(m, "IMABS", null);
137
        r(m, "IMABS", null);
137
        r(m, "IMAGINARY", Imaginary.instance);
138
        r(m, "IMAGINARY", Imaginary.instance);
138
        r(m, "IMARGUMENT", null);
139
        r(m, "IMARGUMENT", null);
Lines 177-182 Link Here
177
        r(m, "SERIESSUM", null);
178
        r(m, "SERIESSUM", null);
178
        r(m, "SQRTPI", null);
179
        r(m, "SQRTPI", null);
179
        r(m, "SUMIFS", Sumifs.instance);
180
        r(m, "SUMIFS", Sumifs.instance);
181
        r(m, "SWITCH", Switch.instance);
180
        r(m, "TBILLEQ", null);
182
        r(m, "TBILLEQ", null);
181
        r(m, "TBILLPRICE", null);
183
        r(m, "TBILLPRICE", null);
182
        r(m, "TBILLYIELD", null);
184
        r(m, "TBILLYIELD", null);
(-)src/java/org/apache/poi/ss/formula/atp/Ifs.java (+60 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Licensed to the Apache Software Foundation (ASF) under one or more
3
   contributor license agreements.  See the NOTICE file distributed with
4
   this work for additional information regarding copyright ownership.
5
   The ASF licenses this file to You under the Apache License, Version 2.0
6
   (the "License"); you may not use this file except in compliance with
7
   the License.  You may obtain a copy of the License at
8
9
       http://www.apache.org/licenses/LICENSE-2.0
10
11
   Unless required by applicable law or agreed to in writing, software
12
   distributed under the License is distributed on an "AS IS" BASIS,
13
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
   See the License for the specific language governing permissions and
15
   limitations under the License.
16
==================================================================== */
17
18
package org.apache.poi.ss.formula.atp;
19
20
import org.apache.poi.ss.formula.OperationEvaluationContext;
21
import org.apache.poi.ss.formula.eval.BoolEval;
22
import org.apache.poi.ss.formula.eval.ErrorEval;
23
import org.apache.poi.ss.formula.eval.ValueEval;
24
import org.apache.poi.ss.formula.functions.FreeRefFunction;
25
26
/**
27
 * Implementation of 'Analysis Toolpak' Excel function IFS()<br>
28
 * <p>
29
 * The IFS function checks whether one or more conditions are met and returns a value that corresponds to the first TRUE condition.
30
 * IFS can take the place of multiple nested IF statements, and is much easier to read with multiple conditions.
31
 * <p>
32
 * <b>Syntax</b><br>
33
 * <b>IFS</b>(IFS([Something is True1, Value if True1, [Something is True2, Value if True2],…[Something is True127, Value if True127]))
34
 *
35
 * @author Pieter Degraeuwe
36
 */
37
final class Ifs implements FreeRefFunction {
38
39
    public static final FreeRefFunction instance = new Ifs();
40
41
    private Ifs() {
42
        // enforce singleton
43
    }
44
45
    public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
46
        if (args.length % 2 != 0) {
47
            return ErrorEval.VALUE_INVALID;
48
        }
49
50
        for (int i = 0; i < args.length; i = i + 2) {
51
            BoolEval logicalTest = (BoolEval) args[i];
52
            if (logicalTest.getBooleanValue()) {
53
                return args[i + 1];
54
            }
55
        }
56
57
        return ErrorEval.NA;
58
    }
59
60
}
(-)src/java/org/apache/poi/ss/formula/atp/Switch.java (+111 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Licensed to the Apache Software Foundation (ASF) under one or more
3
   contributor license agreements.  See the NOTICE file distributed with
4
   this work for additional information regarding copyright ownership.
5
   The ASF licenses this file to You under the Apache License, Version 2.0
6
   (the "License"); you may not use this file except in compliance with
7
   the License.  You may obtain a copy of the License at
8
9
       http://www.apache.org/licenses/LICENSE-2.0
10
11
   Unless required by applicable law or agreed to in writing, software
12
   distributed under the License is distributed on an "AS IS" BASIS,
13
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
   See the License for the specific language governing permissions and
15
   limitations under the License.
16
==================================================================== */
17
18
package org.apache.poi.ss.formula.atp;
19
20
import org.apache.poi.ss.formula.OperationEvaluationContext;
21
import org.apache.poi.ss.formula.eval.BoolEval;
22
import org.apache.poi.ss.formula.eval.ErrorEval;
23
import org.apache.poi.ss.formula.eval.EvaluationException;
24
import org.apache.poi.ss.formula.eval.OperandResolver;
25
import org.apache.poi.ss.formula.eval.ValueEval;
26
import org.apache.poi.ss.formula.functions.FreeRefFunction;
27
import org.apache.poi.ss.formula.ptg.EqualPtg;
28
29
import static org.apache.poi.ss.formula.eval.RelationalOperationEval.EqualEval;
30
31
/**
32
 * Implementation of 'Analysis Toolpak' Excel function SWITCH()<br>
33
 * <p>
34
 * The SWITCH function evaluates one value (called the expression) against a list of values, and returns the result corresponding to the first matching value.
35
 * If there is no match, an optional default value may be returned.
36
 * <p>
37
 * <b>Syntax</b><br>
38
 * <b>SWITCH</b>SWITCH(expression, value1, result1, [default or value2, result2],…[default or value3, result3])
39
 *
40
 * @author Pieter Degraeuwe
41
 */
42
public final class Switch implements FreeRefFunction {
43
44
    public static final FreeRefFunction instance = new Switch();
45
46
    private Switch() {
47
        // enforce singleton
48
    }
49
50
    public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
51
        if (args.length < 3) return ErrorEval.NA;
52
53
        final ValueEval expression;
54
        try {
55
            expression = OperandResolver.getSingleValue(args[0], ec.getRowIndex(), ec.getColumnIndex());
56
        } catch (Exception e) {
57
            return ErrorEval.NA;
58
        }
59
60
        for (int i = 1; i < args.length; i = i+2) {
61
62
            try {
63
                ValueEval value =  OperandResolver.getSingleValue(args[i], ec.getRowIndex(), ec.getColumnIndex());
64
                ValueEval result = args[i+1];
65
                //                ValueEval result = OperandResolver.getSingleValue(args[i+1],ec.getRowIndex(),ec.getColumnIndex()) ;
66
67
68
                final ValueEval evaluate = EqualEval.evaluate(new ValueEval[]{expression, value}, ec.getRowIndex(), ec.getColumnIndex());
69
                if (evaluate instanceof BoolEval) {
70
                    BoolEval boolEval = (BoolEval) evaluate;
71
                    final boolean booleanValue = boolEval.getBooleanValue();
72
                    if (booleanValue) {
73
                        return result;
74
                    }
75
76
77
                }
78
79
            } catch (EvaluationException e) {
80
               return ErrorEval.NA;
81
            }
82
83
84
85
86
87
            if (i + 2 == args.length-1) {
88
                //last value in args is the default one
89
                return args[args.length-1];
90
            }
91
92
        }
93
94
/*
95
96
        if (args.length % 2 != 0) {
97
            return ErrorEval.VALUE_INVALID;
98
        }
99
100
        for (int i = 0; i < args.length; i = i + 2) {
101
            BoolEval logicalTest = (BoolEval) args[i];
102
            if (logicalTest.getBooleanValue()) {
103
                return args[i + 1];
104
            }
105
        }
106
*/
107
108
        return ErrorEval.NA;
109
    }
110
111
}
(-)src/testcases/org/apache/poi/ss/formula/atp/TestIfs.java (+125 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Licensed to the Apache Software Foundation (ASF) under one or more
3
   contributor license agreements.  See the NOTICE file distributed with
4
   this work for additional information regarding copyright ownership.
5
   The ASF licenses this file to You under the Apache License, Version 2.0
6
   (the "License"); you may not use this file except in compliance with
7
   the License.  You may obtain a copy of the License at
8
9
       http://www.apache.org/licenses/LICENSE-2.0
10
11
   Unless required by applicable law or agreed to in writing, software
12
   distributed under the License is distributed on an "AS IS" BASIS,
13
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
   See the License for the specific language governing permissions and
15
   limitations under the License.
16
==================================================================== */
17
package org.apache.poi.ss.formula.atp;
18
19
import junit.framework.TestCase;
20
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
21
import org.apache.poi.ss.usermodel.Cell;
22
import org.apache.poi.ss.usermodel.CellType;
23
import org.apache.poi.ss.usermodel.FormulaEvaluator;
24
import org.apache.poi.ss.usermodel.Row;
25
import org.apache.poi.ss.usermodel.Sheet;
26
import org.apache.poi.ss.usermodel.Workbook;
27
import org.apache.poi.ss.util.CellAddress;
28
import org.apache.poi.ss.util.CellReference;
29
import org.junit.Test;
30
31
import static org.junit.Assert.assertEquals;
32
33
/**
34
 * Testcase for 'Analysis Toolpak' function IFS()
35
 *
36
 * @author Pieter Degraeuwe
37
 */
38
public class TestIfs  {
39
40
    /**
41
     * =IFS(A1="A", "Value for A" , A1="B", "Value for B")
42
     */
43
    @Test
44
    public void testEvaluate() {
45
        Workbook wb = new HSSFWorkbook();
46
        Sheet sh = wb.createSheet();
47
        Row row1 = sh.createRow(0);
48
49
        // Create cells
50
        row1.createCell(0, CellType.STRING);
51
52
        // Create references
53
        CellReference a1Ref = new CellReference("A1");
54
55
        // Set values
56
        final Cell cellA1 = sh.getRow(a1Ref.getRow()).getCell(a1Ref.getCol());
57
58
59
        Cell cell1 = row1.createCell(1);
60
        cell1.setCellFormula("IFS(A1=\"A\", \"Value for A\", A1=\"B\",\"Value for B\")");
61
62
        FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
63
64
        cellA1.setCellValue("A");
65
        assertEquals("Checks that the cell is numeric",
66
                CellType.STRING, evaluator.evaluate(cell1).getCellType());
67
        assertEquals("IFS should return 'Value for B'", "Value for A", evaluator.evaluate(cell1).getStringValue());
68
69
        cellA1.setCellValue("B");
70
        evaluator.clearAllCachedResultValues();
71
        assertEquals("Checks that the cell is numeric",
72
                CellType.STRING, evaluator.evaluate(cell1).getCellType());
73
        assertEquals("IFS should return 'Value for B'", "Value for B", evaluator.evaluate(cell1).getStringValue());
74
75
76
    }
77
78
79
    /**
80
     * where D1 contains a string "A"
81
     * =IFS(A1=D1, "Value for A" , A1="B", "Value for B")
82
     */
83
    @Test
84
    public  void testEvaluateForReferenced() {
85
        Workbook wb = new HSSFWorkbook();
86
        Sheet sh = wb.createSheet();
87
        Row row1 = sh.createRow(0);
88
89
        // Create cells
90
//        row1.createCell(0, CellType.STRING);
91
92
93
        // Create references
94
        CellReference a1Ref = new CellReference("A1");
95
        CellReference d1Ref = new CellReference("D1");
96
97
        // Set values
98
        final Cell cellA1 = sh.getRow(a1Ref.getRow()).createCell(a1Ref.getCol());
99
        cellA1.setCellFormula("D1");
100
101
        final Cell cellD1 = sh.getRow(d1Ref.getRow()).createCell(d1Ref.getCol());
102
        cellD1.setCellValue("A");
103
104
105
        Cell cell1 = row1.createCell(1);
106
        cell1.setCellFormula("IFS(A1=\"A\", \"Value for A\", A1=\"B\",\"Value for B\")");
107
108
        FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
109
110
        cellD1.setCellValue("A");
111
        assertEquals("Checks that the cell is numeric",
112
                CellType.STRING, evaluator.evaluate(cell1).getCellType());
113
        assertEquals("IFS should return 'Value for B'", "Value for A", evaluator.evaluate(cell1).getStringValue());
114
115
        cellD1.setCellValue("B");
116
        evaluator.clearAllCachedResultValues();
117
        assertEquals("Checks that the cell is numeric",
118
                CellType.STRING, evaluator.evaluate(cell1).getCellType());
119
        assertEquals("IFS should return 'Value for B'", "Value for B", evaluator.evaluate(cell1).getStringValue());
120
121
122
    }
123
124
125
}
(-)src/testcases/org/apache/poi/ss/formula/atp/TestSwitch.java (+83 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Licensed to the Apache Software Foundation (ASF) under one or more
3
   contributor license agreements.  See the NOTICE file distributed with
4
   this work for additional information regarding copyright ownership.
5
   The ASF licenses this file to You under the Apache License, Version 2.0
6
   (the "License"); you may not use this file except in compliance with
7
   the License.  You may obtain a copy of the License at
8
9
       http://www.apache.org/licenses/LICENSE-2.0
10
11
   Unless required by applicable law or agreed to in writing, software
12
   distributed under the License is distributed on an "AS IS" BASIS,
13
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
   See the License for the specific language governing permissions and
15
   limitations under the License.
16
==================================================================== */
17
package org.apache.poi.ss.formula.atp;
18
19
import junit.framework.TestCase;
20
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
21
import org.apache.poi.ss.usermodel.Cell;
22
import org.apache.poi.ss.usermodel.CellType;
23
import org.apache.poi.ss.usermodel.FormulaEvaluator;
24
import org.apache.poi.ss.usermodel.Row;
25
import org.apache.poi.ss.usermodel.Sheet;
26
import org.apache.poi.ss.usermodel.Workbook;
27
import org.apache.poi.ss.util.CellReference;
28
import org.junit.Test;
29
30
import static org.junit.Assert.assertEquals;
31
32
/**
33
 * Testcase for 'Analysis Toolpak' function SWITCH()
34
 *
35
 * @author Pieter Degraeuwe
36
 */
37
public class TestSwitch {
38
39
    /**
40
     * =SWITCH(A1, "A", "Value for A", "B", "Value for B" )
41
     * =SWITCH(A1, "A", "Value for A", "B", "Value for B", "Something else" )
42
     */
43
    @Test
44
    public void testEvaluate() {
45
        Workbook wb = new HSSFWorkbook();
46
        Sheet sh = wb.createSheet();
47
        Row row1 = sh.createRow(0);
48
49
        FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
50
51
        // Create cells
52
        row1.createCell(0, CellType.STRING);
53
54
        // Create references
55
        CellReference a1Ref = new CellReference("A1");
56
57
        // Set values
58
        final Cell cellA1 = sh.getRow(a1Ref.getRow()).getCell(a1Ref.getCol());
59
60
61
        Cell cell1 = row1.createCell(1);
62
        cell1.setCellFormula("SWITCH(A1, \"A\",\"Value for A\", \"B\",\"Value for B\", \"Something else\")");
63
64
65
        cellA1.setCellValue("A");
66
        assertEquals("Checks that the cell is String", CellType.STRING, evaluator.evaluate(cell1).getCellType());
67
        assertEquals("SWITCH should return 'Value for A'", "Value for A", evaluator.evaluate(cell1).getStringValue());
68
69
        cellA1.setCellValue("B");
70
        evaluator.clearAllCachedResultValues();
71
        assertEquals("Checks that the cell is String", CellType.STRING, evaluator.evaluate(cell1).getCellType());
72
        assertEquals("SWITCH should return 'Value for B'", "Value for B", evaluator.evaluate(cell1).getStringValue());
73
74
        cellA1.setCellValue("");
75
        evaluator.clearAllCachedResultValues();
76
        assertEquals("Checks that the cell is String", CellType.STRING, evaluator.evaluate(cell1).getCellType());
77
        assertEquals("SWITCH should return 'Something else'", "Something else", evaluator.evaluate(cell1).getStringValue());
78
79
80
    }
81
82
83
}

Return to bug 60924