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.

SettingExternalFunction.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.examples.ss.formula;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.util.HashMap;
  23. import java.util.Locale;
  24. import java.util.Map;
  25. import org.apache.poi.ss.formula.OperationEvaluationContext;
  26. import org.apache.poi.ss.formula.eval.ErrorEval;
  27. import org.apache.poi.ss.formula.eval.ValueEval;
  28. import org.apache.poi.ss.formula.functions.FreeRefFunction;
  29. import org.apache.poi.ss.formula.udf.UDFFinder;
  30. import org.apache.poi.ss.usermodel.Row;
  31. import org.apache.poi.ss.usermodel.Sheet;
  32. import org.apache.poi.ss.usermodel.Workbook;
  33. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  34. /**
  35. * Demonstrates how to use functions provided by third-party add-ins, e.g. Bloomberg Excel Add-in.
  36. *
  37. * There can be situations when you are not interested in formula evaluation,
  38. * you just need to set the formula and the workbook will be evaluation by the client.
  39. */
  40. public class SettingExternalFunction {
  41. /**
  42. * wrap external functions in a plugin
  43. */
  44. public static class BloombergAddIn implements UDFFinder {
  45. private final Map<String, FreeRefFunction> _functionsByName;
  46. public BloombergAddIn() {
  47. // dummy function that returns NA
  48. // don't care about the implementation, we are not interested in evaluation
  49. // and this method will never be called
  50. FreeRefFunction NA = new FreeRefFunction() {
  51. @Override
  52. public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
  53. return ErrorEval.NA;
  54. }
  55. };
  56. _functionsByName = new HashMap<>();
  57. _functionsByName.put("BDP", NA);
  58. _functionsByName.put("BDH", NA);
  59. _functionsByName.put("BDS", NA);
  60. }
  61. @Override
  62. public FreeRefFunction findFunction(String name) {
  63. return _functionsByName.get(name.toUpperCase(Locale.ROOT));
  64. }
  65. }
  66. public static void main( String[] args ) throws IOException {
  67. try (Workbook wb = new XSSFWorkbook()) { // or new HSSFWorkbook()
  68. // register the add-in
  69. wb.addToolPack(new BloombergAddIn());
  70. Sheet sheet = wb.createSheet();
  71. Row row = sheet.createRow(0);
  72. row.createCell(0).setCellFormula("BDP(\"GOOG Equity\",\"CHG_PCT_YTD\")/100");
  73. row.createCell(1).setCellFormula("BDH(\"goog us equity\",\"EBIT\",\"1/1/2005\",\"12/31/2009\",\"per=cy\",\"curr=USD\") ");
  74. row.createCell(2).setCellFormula("BDS(\"goog us equity\",\"top_20_holders_public_filings\") ");
  75. try (FileOutputStream out = new FileOutputStream("bloomberg-demo.xlsx")) {
  76. wb.write(out);
  77. }
  78. }
  79. }
  80. }