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;
* 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;
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
*/
import java.util.Map;
import java.util.Set;
+import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.util.Internal;
/**
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);
--- /dev/null
+/* ====================================================================
+ 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();
+}
package org.apache.poi.ss.usermodel;
+import java.util.Map;
+
/**
* Evaluates formula cells.<p/>
*
* @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?
* @param value whether to perform detailed output
*/
void setDebugEvaluationOutputForNextEval(boolean value);
-
}
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;
/**
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() {}
package org.apache.poi.xssf.usermodel;
+import java.util.Map;
+
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
+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.NumberEval;
* @author Amol S. Deshmukh < amolweb at ya hoo dot com >
* @author Josh Micich
*/
-public class XSSFFormulaEvaluator implements FormulaEvaluator {
+public class XSSFFormulaEvaluator implements FormulaEvaluator, WorkbookEvaluatorProvider {
private WorkbookEvaluator _bookEvaluator;
private XSSFWorkbook _book;
throw new RuntimeException("Unexpected eval class (" + eval.getClass().getName() + ")");
}
+ public void setupReferencedWorkbooks(Map<String, FormulaEvaluator> evaluators) {
+ CollaboratingWorkbooksEnvironment.setupFormulaEvaluator(evaluators);
+ }
+
+ public WorkbookEvaluator _getWorkbookEvaluator() {
+ return _bookEvaluator;
+ }
+
/** {@inheritDoc} */
public void setDebugEvaluationOutputForNextEval(boolean value){
_bookEvaluator.setDebugEvaluationOutputForNextEval(value);
package org.apache.poi.xssf.usermodel;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.ss.usermodel.BaseTestFormulaEvaluator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
assertEquals("Test A1", cXSL_sNR.getStringCellValue());
assertEquals(142.0, cXSL_gNR.getNumericCellValue());
- // Try evaluating
- // TODO
+ // Try to evaluate without references, won't work
+ // (At least, not unit we fix bug #56752 that is)
+ try {
+ evaluator.evaluate(cXSL_cell);
+ fail("Without a fix for #56752, shouldn't be able to evaluate a " +
+ "reference to a non-provided linked workbook");
+ } catch(Exception e) {}
+
+ // Setup the environment
+ Map<String,FormulaEvaluator> evaluators = new HashMap<String, FormulaEvaluator>();
+ evaluators.put("ref2-56737.xlsx", evaluator);
+ evaluators.put("56737.xlsx",
+ _testDataProvider.openSampleWorkbook("56737.xlsx").getCreationHelper().createFormulaEvaluator());
+ evaluators.put("56737.xls",
+ HSSFTestDataSamples.openSampleWorkbook("56737.xls").getCreationHelper().createFormulaEvaluator());
+ evaluator.setupReferencedWorkbooks(evaluators);
+
+ // Try evaluating all of them, ensure we don't blow up
+ for(Row r : s) {
+ for (Cell c : r) {
+ // TODO Fix and enable
+ // evaluator.evaluate(c);
+ }
+ }
+
+ // Evaluate and check results
}
/**