aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/poi
diff options
context:
space:
mode:
authorNick Burch <nick@apache.org>2014-07-21 12:23:54 +0000
committerNick Burch <nick@apache.org>2014-07-21 12:23:54 +0000
commit70f40542fd5c4b246e9f3a837fb5fa9cd9b445fe (patch)
tree1cf7795219e859ab03e3837d1cc8fcd5bef173b5 /src/java/org/apache/poi
parent5dfd7ea48cae64cb017eb085daadf1b38c46c902 (diff)
downloadpoi-70f40542fd5c4b246e9f3a837fb5fa9cd9b445fe.tar.gz
poi-70f40542fd5c4b246e9f3a837fb5fa9cd9b445fe.zip
Generalise the CollaboratingWorkbooksEnvironment setup, so that XSSF can use it too
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1612254 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/poi')
-rw-r--r--src/java/org/apache/poi/hssf/usermodel/HSSFFormulaEvaluator.java15
-rw-r--r--src/java/org/apache/poi/ss/formula/CollaboratingWorkbooksEnvironment.java14
-rw-r--r--src/java/org/apache/poi/ss/formula/WorkbookEvaluatorProvider.java34
-rw-r--r--src/java/org/apache/poi/ss/usermodel/FormulaEvaluator.java13
-rw-r--r--src/java/org/apache/poi/ss/util/SheetUtil.java19
5 files changed, 88 insertions, 7 deletions
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFFormulaEvaluator.java b/src/java/org/apache/poi/hssf/usermodel/HSSFFormulaEvaluator.java
index 38f08cd2ca..1e8098b9ef 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFFormulaEvaluator.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFFormulaEvaluator.java
@@ -17,9 +17,12 @@
package org.apache.poi.hssf.usermodel;
+import java.util.Map;
+
import org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment;
import org.apache.poi.ss.formula.IStabilityClassifier;
import org.apache.poi.ss.formula.WorkbookEvaluator;
+import org.apache.poi.ss.formula.WorkbookEvaluatorProvider;
import org.apache.poi.ss.formula.eval.BoolEval;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.NumericValueEval;
@@ -40,7 +43,7 @@ import org.apache.poi.ss.usermodel.Workbook;
* cell values. Be sure to call {@link #clearAllCachedResultValues()} if any workbook cells are changed between
* calls to evaluate~ methods on this class.
*/
-public class HSSFFormulaEvaluator implements FormulaEvaluator {
+public class HSSFFormulaEvaluator implements FormulaEvaluator, WorkbookEvaluatorProvider {
private WorkbookEvaluator _bookEvaluator;
private HSSFWorkbook _book;
@@ -101,7 +104,15 @@ public class HSSFFormulaEvaluator implements FormulaEvaluator {
CollaboratingWorkbooksEnvironment.setup(workbookNames, wbEvals);
}
- /**
+ public void setupReferencedWorkbooks(Map<String, FormulaEvaluator> evaluators) {
+ CollaboratingWorkbooksEnvironment.setupFormulaEvaluator(evaluators);
+ }
+
+ public WorkbookEvaluator _getWorkbookEvaluator() {
+ return _bookEvaluator;
+ }
+
+ /**
* Does nothing
* @deprecated (Aug 2008) - not needed, since the current row can be derived from the cell
*/
diff --git a/src/java/org/apache/poi/ss/formula/CollaboratingWorkbooksEnvironment.java b/src/java/org/apache/poi/ss/formula/CollaboratingWorkbooksEnvironment.java
index 1352b716cd..00b3e8bfcb 100644
--- a/src/java/org/apache/poi/ss/formula/CollaboratingWorkbooksEnvironment.java
+++ b/src/java/org/apache/poi/ss/formula/CollaboratingWorkbooksEnvironment.java
@@ -24,6 +24,7 @@ import java.util.Iterator;
import java.util.Map;
import java.util.Set;
+import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.util.Internal;
/**
@@ -73,6 +74,19 @@ public final class CollaboratingWorkbooksEnvironment {
evaluatorsByName.values().toArray(new WorkbookEvaluator[evaluatorsByName.size()]);
new CollaboratingWorkbooksEnvironment(evaluatorsByName, evaluators);
}
+ public static void setupFormulaEvaluator(Map<String,FormulaEvaluator> evaluators) {
+ Map<String, WorkbookEvaluator> evaluatorsByName = new HashMap<String, WorkbookEvaluator>(evaluators.size());
+ for (String wbName : evaluators.keySet()) {
+ FormulaEvaluator eval = evaluators.get(wbName);
+ if (eval instanceof WorkbookEvaluatorProvider) {
+ evaluatorsByName.put(wbName, ((WorkbookEvaluatorProvider)eval)._getWorkbookEvaluator());
+ } else {
+ throw new IllegalArgumentException("Formula Evaluator " + eval +
+ " provides no WorkbookEvaluator access");
+ }
+ }
+ setup(evaluatorsByName);
+ }
private CollaboratingWorkbooksEnvironment(String[] workbookNames, WorkbookEvaluator[] evaluators, int nItems) {
this(toUniqueMap(workbookNames, evaluators, nItems), evaluators);
diff --git a/src/java/org/apache/poi/ss/formula/WorkbookEvaluatorProvider.java b/src/java/org/apache/poi/ss/formula/WorkbookEvaluatorProvider.java
new file mode 100644
index 0000000000..dd6f09bd17
--- /dev/null
+++ b/src/java/org/apache/poi/ss/formula/WorkbookEvaluatorProvider.java
@@ -0,0 +1,34 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.ss.formula;
+
+import org.apache.poi.util.Internal;
+
+/**
+ * Provides access to a {@link WorkbookEvaluator}, eg for use with
+ * {@link CollaboratingWorkbooksEnvironment}
+ *
+ * <p>For POI internal use only
+ */
+@Internal
+public interface WorkbookEvaluatorProvider {
+ /**
+ * Provide the underlying WorkbookEvaluator
+ */
+ WorkbookEvaluator _getWorkbookEvaluator();
+}
diff --git a/src/java/org/apache/poi/ss/usermodel/FormulaEvaluator.java b/src/java/org/apache/poi/ss/usermodel/FormulaEvaluator.java
index 17bb3bd579..3e8a337814 100644
--- a/src/java/org/apache/poi/ss/usermodel/FormulaEvaluator.java
+++ b/src/java/org/apache/poi/ss/usermodel/FormulaEvaluator.java
@@ -17,6 +17,8 @@
package org.apache.poi.ss.usermodel;
+import java.util.Map;
+
/**
* Evaluates formula cells.<p/>
*
@@ -114,6 +116,16 @@ public interface FormulaEvaluator {
* @param cell
*/
Cell evaluateInCell(Cell cell);
+
+ /**
+ * Sets up the Formula Evaluator to be able to reference and resolve
+ * links to other workbooks, eg [Test.xls]Sheet1!A1.
+ * <p>For a workbook referenced as [Test.xls]Sheet1!A1, you should
+ * supply a map containing the key Test.xls (no square brackets),
+ * and an open FormulaEvaluator onto that Workbook.
+ * @param otherWorkbooks Map of workbook names (no square brackets) to an evaluator on that workbook
+ */
+ void setupReferencedWorkbooks(Map<String,FormulaEvaluator> workbooks);
/**
* Perform detailed output of formula evaluation for next evaluation only?
@@ -124,5 +136,4 @@ public interface FormulaEvaluator {
* @param value whether to perform detailed output
*/
void setDebugEvaluationOutputForNextEval(boolean value);
-
}
diff --git a/src/java/org/apache/poi/ss/util/SheetUtil.java b/src/java/org/apache/poi/ss/util/SheetUtil.java
index a5c1fb736d..792ef31f69 100644
--- a/src/java/org/apache/poi/ss/util/SheetUtil.java
+++ b/src/java/org/apache/poi/ss/util/SheetUtil.java
@@ -17,13 +17,23 @@
package org.apache.poi.ss.util;
-import org.apache.poi.ss.usermodel.*;
-
-import java.text.AttributedString;
-import java.awt.font.TextLayout;
import java.awt.font.FontRenderContext;
import java.awt.font.TextAttribute;
+import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
+import java.text.AttributedString;
+import java.util.Map;
+
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.CellValue;
+import org.apache.poi.ss.usermodel.DataFormatter;
+import org.apache.poi.ss.usermodel.Font;
+import org.apache.poi.ss.usermodel.FormulaEvaluator;
+import org.apache.poi.ss.usermodel.RichTextString;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
/**
@@ -61,6 +71,7 @@ public class SheetUtil {
public void notifyUpdateCell(Cell cell) {}
public CellValue evaluate(Cell cell) {return null; }
public Cell evaluateInCell(Cell cell) { return null; }
+ public void setupReferencedWorkbooks(Map<String, FormulaEvaluator> workbooks) {}
public void setDebugEvaluationOutputForNextEval(boolean value) {}
public void evaluateAll() {}