]> source.dussan.org Git - poi.git/commitdiff
added a demo how to use functions from external APIs
authorYegor Kozlov <yegor@apache.org>
Mon, 12 Nov 2012 09:19:37 +0000 (09:19 +0000)
committerYegor Kozlov <yegor@apache.org>
Mon, 12 Nov 2012 09:19:37 +0000 (09:19 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1408205 13f79535-47bb-0310-9956-ffa450edef68

src/examples/src/org/apache/poi/ss/examples/formula/SettingExternalFunction.java [new file with mode: 0644]

diff --git a/src/examples/src/org/apache/poi/ss/examples/formula/SettingExternalFunction.java b/src/examples/src/org/apache/poi/ss/examples/formula/SettingExternalFunction.java
new file mode 100644 (file)
index 0000000..17f4e0d
--- /dev/null
@@ -0,0 +1,93 @@
+/*\r
+ *  ====================================================================\r
+ *    Licensed to the Apache Software Foundation (ASF) under one or more\r
+ *    contributor license agreements.  See the NOTICE file distributed with\r
+ *    this work for additional information regarding copyright ownership.\r
+ *    The ASF licenses this file to You under the Apache License, Version 2.0\r
+ *    (the "License"); you may not use this file except in compliance with\r
+ *    the License.  You may obtain a copy of the License at\r
+ *\r
+ *        http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ *    Unless required by applicable law or agreed to in writing, software\r
+ *    distributed under the License is distributed on an "AS IS" BASIS,\r
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ *    See the License for the specific language governing permissions and\r
+ *    limitations under the License.\r
+ * ====================================================================\r
+ */\r
+\r
+package org.apache.poi.ss.examples.formula;\r
+\r
+import org.apache.poi.ss.formula.OperationEvaluationContext;\r
+import org.apache.poi.ss.formula.eval.ErrorEval;\r
+import org.apache.poi.ss.formula.eval.ValueEval;\r
+import org.apache.poi.ss.formula.functions.FreeRefFunction;\r
+import org.apache.poi.ss.formula.udf.UDFFinder;\r
+import org.apache.poi.ss.usermodel.Row;\r
+import org.apache.poi.ss.usermodel.Sheet;\r
+import org.apache.poi.ss.usermodel.Workbook;\r
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;\r
+\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+/**\r
+ * Demonstrates how to use functions provided by third-party add-ins, e.g. Bloomberg Excel Add-in.\r
+ *\r
+ * There can be situations when you are not interested in formula evaluation,\r
+ * you just need to set the formula  and the workbook will be evaluation by the client.\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class SettingExternalFunction {\r
+\r
+    /**\r
+     * wrap external functions in a plugin\r
+     */\r
+    public static class BloombergAddIn implements UDFFinder {\r
+        private final Map<String, FreeRefFunction> _functionsByName;\r
+\r
+        public BloombergAddIn() {\r
+            // dummy function that returns NA\r
+            // don't care about the implementation, we are not interested in evaluation\r
+            // and this method will never be called\r
+            FreeRefFunction NA = new FreeRefFunction() {\r
+                public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {\r
+                    return ErrorEval.NA;\r
+                }\r
+            };\r
+            _functionsByName = new HashMap<String, FreeRefFunction>();\r
+            _functionsByName.put("BDP", NA);\r
+            _functionsByName.put("BDH", NA);\r
+            _functionsByName.put("BDS", NA);\r
+        }\r
+\r
+        public FreeRefFunction findFunction(String name) {\r
+            return _functionsByName.get(name.toUpperCase());\r
+        }\r
+\r
+    }\r
+\r
+    public static void main( String[] args ) throws IOException {\r
+\r
+        Workbook wb = new XSSFWorkbook();  // or new HSSFWorkbook()\r
+\r
+        // register the add-in\r
+        wb.addToolPack(new BloombergAddIn());\r
+\r
+        Sheet sheet = wb.createSheet();\r
+        Row row = sheet.createRow(0);\r
+        row.createCell(0).setCellFormula("BDP(\"GOOG Equity\",\"CHG_PCT_YTD\")/100");\r
+        row.createCell(1).setCellFormula("BDH(\"goog us equity\",\"EBIT\",\"1/1/2005\",\"12/31/2009\",\"per=cy\",\"curr=USD\") ");\r
+        row.createCell(2).setCellFormula("BDS(\"goog us equity\",\"top_20_holders_public_filings\") ");\r
+\r
+        FileOutputStream out = new FileOutputStream("bloomberg-demo.xlsx");\r
+        wb.write(out);\r
+        out.close();\r
+\r
+    }\r
+\r
+}\r