You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BaseTestExternalFunctions.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.formula;
  16. import junit.framework.TestCase;
  17. import org.apache.poi.ss.ITestDataProvider;
  18. import org.apache.poi.ss.formula.eval.ErrorEval;
  19. import org.apache.poi.ss.formula.eval.StringEval;
  20. import org.apache.poi.ss.formula.eval.ValueEval;
  21. import org.apache.poi.ss.formula.functions.FreeRefFunction;
  22. import org.apache.poi.ss.formula.udf.DefaultUDFFinder;
  23. import org.apache.poi.ss.formula.udf.UDFFinder;
  24. import org.apache.poi.ss.usermodel.Cell;
  25. import org.apache.poi.ss.usermodel.FormulaEvaluator;
  26. import org.apache.poi.ss.usermodel.Sheet;
  27. import org.apache.poi.ss.usermodel.Workbook;
  28. /**
  29. * Test setting / evaluating of Analysis Toolpack and user-defined functions
  30. *
  31. * @author Yegor Kozlov
  32. */
  33. public class BaseTestExternalFunctions extends TestCase {
  34. // define two custom user-defined functions
  35. private static class MyFunc implements FreeRefFunction {
  36. public MyFunc() {
  37. //
  38. }
  39. public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
  40. if (args.length != 1 || !(args[0] instanceof StringEval)) {
  41. return ErrorEval.VALUE_INVALID;
  42. }
  43. StringEval input = (StringEval) args[0];
  44. return new StringEval(input.getStringValue() + "abc");
  45. }
  46. }
  47. private static class MyFunc2 implements FreeRefFunction {
  48. public MyFunc2() {
  49. //
  50. }
  51. public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
  52. if (args.length != 1 || !(args[0] instanceof StringEval)) {
  53. return ErrorEval.VALUE_INVALID;
  54. }
  55. StringEval input = (StringEval) args[0];
  56. return new StringEval(input.getStringValue() + "abc2");
  57. }
  58. }
  59. /**
  60. * register the two test UDFs in a UDF finder, to be passed to the workbook
  61. */
  62. private static UDFFinder customToolpack = new DefaultUDFFinder(
  63. new String[] { "myFunc", "myFunc2"},
  64. new FreeRefFunction[] { new MyFunc(), new MyFunc2()}
  65. );
  66. protected final ITestDataProvider _testDataProvider;
  67. /**
  68. * @param testDataProvider an object that provides test data in HSSF / XSSF specific way
  69. */
  70. protected BaseTestExternalFunctions(ITestDataProvider testDataProvider) {
  71. _testDataProvider = testDataProvider;
  72. }
  73. public void testExternalFunctions() {
  74. Workbook wb = _testDataProvider.createWorkbook();
  75. Sheet sh = wb.createSheet();
  76. Cell cell1 = sh.createRow(0).createCell(0);
  77. cell1.setCellFormula("ISODD(1)+ISEVEN(2)"); // functions from the Excel Analysis Toolpack
  78. assertEquals("ISODD(1)+ISEVEN(2)", cell1.getCellFormula());
  79. Cell cell2 = sh.createRow(1).createCell(0);
  80. try {
  81. cell2.setCellFormula("MYFUNC(\"B1\")");
  82. fail("Should fail because MYFUNC is an unknown function");
  83. } catch (FormulaParseException e){
  84. ; //expected
  85. }
  86. wb.addToolPack(customToolpack);
  87. cell2.setCellFormula("MYFUNC(\"B1\")");
  88. assertEquals("MYFUNC(\"B1\")", cell2.getCellFormula());
  89. Cell cell3 = sh.createRow(2).createCell(0);
  90. cell3.setCellFormula("MYFUNC2(\"C1\")&\"-\"&A2"); //where A2 is defined above
  91. assertEquals("MYFUNC2(\"C1\")&\"-\"&A2", cell3.getCellFormula());
  92. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  93. assertEquals(2.0, evaluator.evaluate(cell1).getNumberValue());
  94. assertEquals("B1abc", evaluator.evaluate(cell2).getStringValue());
  95. assertEquals("C1abc2-B1abc", evaluator.evaluate(cell3).getStringValue());
  96. }
  97. /**
  98. * test invoking saved ATP functions
  99. *
  100. * @param testFile either atp.xls or atp.xlsx
  101. */
  102. public void baseTestInvokeATP(String testFile){
  103. Workbook wb = _testDataProvider.openSampleWorkbook(testFile);
  104. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  105. Sheet sh = wb.getSheetAt(0);
  106. // these two are not imlemented in r
  107. assertEquals("DELTA(1.3,1.5)", sh.getRow(0).getCell(1).getCellFormula());
  108. assertEquals("COMPLEX(2,4)", sh.getRow(1).getCell(1).getCellFormula());
  109. Cell cell2 = sh.getRow(2).getCell(1);
  110. assertEquals("ISODD(2)", cell2.getCellFormula());
  111. assertEquals(false, evaluator.evaluate(cell2).getBooleanValue());
  112. assertEquals(Cell.CELL_TYPE_BOOLEAN, evaluator.evaluateFormulaCell(cell2));
  113. Cell cell3 = sh.getRow(3).getCell(1);
  114. assertEquals("ISEVEN(2)", cell3.getCellFormula());
  115. assertEquals(true, evaluator.evaluate(cell3).getBooleanValue());
  116. assertEquals(Cell.CELL_TYPE_BOOLEAN, evaluator.evaluateFormulaCell(cell3));
  117. }
  118. }