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.

TestExternalFunction.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.hssf.record.formula.eval;
  16. import java.io.IOException;
  17. import junit.framework.TestCase;
  18. import org.apache.poi.hssf.HSSFTestDataSamples;
  19. import org.apache.poi.hssf.record.formula.functions.FreeRefFunction;
  20. import org.apache.poi.hssf.record.formula.toolpack.DefaultToolPack;
  21. import org.apache.poi.hssf.record.formula.toolpack.MainToolPacksHandler;
  22. import org.apache.poi.hssf.record.formula.toolpack.ToolPack;
  23. import org.apache.poi.hssf.usermodel.HSSFCell;
  24. import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
  25. import org.apache.poi.hssf.usermodel.HSSFRow;
  26. import org.apache.poi.hssf.usermodel.HSSFSheet;
  27. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  28. import org.apache.poi.ss.formula.OperationEvaluationContext;
  29. import org.apache.poi.ss.usermodel.Workbook;
  30. /**
  31. *
  32. * @author Josh Micich
  33. *
  34. * Modified 09/14/09 by Petr Udalau - Test of registering UDFs in workbook and
  35. * using ToolPacks.
  36. */
  37. public final class TestExternalFunction extends TestCase {
  38. private static class MyFunc implements FreeRefFunction {
  39. public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
  40. if (args.length != 1 || !(args[0] instanceof StringEval)) {
  41. return ErrorEval.VALUE_INVALID;
  42. } else {
  43. StringEval input = (StringEval) args[0];
  44. return new StringEval(input.getStringValue() + "abc");
  45. }
  46. }
  47. }
  48. private static class MyFunc2 implements FreeRefFunction {
  49. public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
  50. if (args.length != 1 || !(args[0] instanceof StringEval)) {
  51. return ErrorEval.VALUE_INVALID;
  52. } else {
  53. StringEval input = (StringEval) args[0];
  54. return new StringEval(input.getStringValue() + "abc2");
  55. }
  56. }
  57. }
  58. /**
  59. * Creates and registers user-defined function "MyFunc()" directly with POI.
  60. * This is VB function defined in "testNames.xls". In future there must be
  61. * some parser of VBA scripts which will register UDFs.
  62. */
  63. private void registerMyFunc(Workbook workbook) {
  64. workbook.registerUserDefinedFunction("myFunc", new MyFunc());
  65. }
  66. /**
  67. * Creates example ToolPack which contains function "MyFunc2()".
  68. */
  69. private void createExampleToolPack() {
  70. ToolPack exampleToolPack = new DefaultToolPack();
  71. exampleToolPack.addFunction("myFunc2", new MyFunc2());
  72. MainToolPacksHandler.instance().addToolPack(exampleToolPack);
  73. }
  74. /**
  75. * Checks that an external function can get invoked from the formula
  76. * evaluator.
  77. *
  78. * @throws IOException
  79. *
  80. */
  81. public void testInvoke() {
  82. HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("testNames.xls");
  83. HSSFSheet sheet = wb.getSheetAt(0);
  84. registerMyFunc(wb);
  85. createExampleToolPack();
  86. HSSFRow row = sheet.getRow(0);
  87. HSSFCell myFuncCell = row.getCell(1); //=myFunc("_")
  88. HSSFCell myFunc2Cell = row.getCell(2); //=myFunc2("_")
  89. HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
  90. try {
  91. assertEquals("_abc", fe.evaluate(myFuncCell).getStringValue());
  92. assertEquals("_abc2", fe.evaluate(myFunc2Cell).getStringValue());
  93. } catch (Exception e) {
  94. assertFalse(true);
  95. }
  96. }
  97. }