import org.apache.poi.ss.formula.eval.StringEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.formula.udf.UDFFinder;
+
+import java.lang.reflect.Method;
+
import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment;
* This class enables a 'master workbook' to be loaded just once and shared between many evaluation
* clients. Each evaluation client creates its own {@link ForkedEvaluator} and can set cell values
* that will be used for local evaluations (and don't disturb evaluations on other evaluators).
- *
- * @author Josh Micich
*/
public final class ForkedEvaluator {
private static EvaluationWorkbook createEvaluationWorkbook(Workbook wb) {
if (wb instanceof HSSFWorkbook) {
return HSSFEvaluationWorkbook.create((HSSFWorkbook) wb);
+ } else {
+ try {
+ // TODO: check if this is Java 9 compatible ...
+ Class<?> evalWB = Class.forName("org.apache.poi.xssf.usermodel.XSSFEvaluationWorkbook");
+ Class<?> xssfWB = Class.forName("org.apache.poi.xssf.usermodel.XSSFWorkbook");
+ Method createM = evalWB.getDeclaredMethod("create", xssfWB);
+ return (EvaluationWorkbook)createM.invoke(null, wb);
+ } catch (Exception e) {
+ throw new IllegalArgumentException("Unexpected workbook type (" + wb.getClass().getName() + ") - check for poi-ooxml and poi-ooxml schemas jar in the classpath", e);
+ }
}
-// TODO rearrange POI build to allow this
-// if (wb instanceof XSSFWorkbook) {
-// return XSSFEvaluationWorkbook.create((XSSFWorkbook) wb);
-// }
- throw new IllegalArgumentException("Unexpected workbook type (" + wb.getClass().getName() + ")");
- }
- /**
- * @deprecated (Sep 2009) (reduce overloading) use {@link #create(Workbook, IStabilityClassifier, UDFFinder)}
- */
- public static ForkedEvaluator create(Workbook wb, IStabilityClassifier stabilityClassifier) {
- return create(wb, stabilityClassifier, null);
}
+
/**
* @param udfFinder pass <code>null</code> for default (AnalysisToolPak only)
*/
package org.apache.poi.xssf.usermodel;
+import org.apache.poi.ss.formula.eval.forked.TestForkedEvaluator;
import org.apache.poi.xssf.usermodel.extensions.TestXSSFBorder;
import org.apache.poi.xssf.usermodel.extensions.TestXSSFCellFill;
import org.apache.poi.xssf.usermodel.extensions.TestXSSFSheetComments;
TestXSSFSheetComments.class,
TestColumnHelper.class,
TestHeaderFooterHelper.class,
- TestXSSFPivotTable.class
+ TestXSSFPivotTable.class,
+ TestForkedEvaluator.class
})
public final class AllXSSFUsermodelTests {
}
--- /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.xssf.usermodel;
+
+import org.apache.poi.ss.formula.eval.forked.TestForkedEvaluator;
+import org.apache.poi.ss.usermodel.Workbook;
+
+public class TestXSSFForkedEvaluator extends TestForkedEvaluator {
+
+ protected Workbook newWorkbook() {
+ return new XSSFWorkbook();
+ }
+}
import java.io.IOException;
-import org.apache.poi.hssf.usermodel.HSSFRow;
-import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.IStabilityClassifier;
import org.apache.poi.ss.formula.eval.NumberEval;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
-public final class TestForkedEvaluator {
+public class TestForkedEvaluator {
@Rule
public ExpectedException expectedEx = ExpectedException.none();
+ protected Workbook newWorkbook() {
+ return new HSSFWorkbook();
+ }
+
/**
* set up a calculation workbook with input cells nicely segregated on a
* sheet called "Inputs"
*/
- private static HSSFWorkbook createWorkbook() {
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sheet1 = wb.createSheet("Inputs");
- HSSFSheet sheet2 = wb.createSheet("Calculations");
- HSSFRow row;
+ protected Workbook createWorkbook() {
+ Workbook wb = newWorkbook();
+ Sheet sheet1 = wb.createSheet("Inputs");
+ Sheet sheet2 = wb.createSheet("Calculations");
+ Row row;
row = sheet2.createRow(0);
row.createCell(0).setCellFormula("B1*Inputs!A1-Inputs!B1");
row.createCell(1).setCellValue(5.0); // Calculations!B1
*/
@Test
public void testBasic() throws IOException {
- HSSFWorkbook wb = createWorkbook();
+ Workbook wb = createWorkbook();
// The stability classifier is useful to reduce memory consumption of caching logic
IStabilityClassifier stabilityClassifier = new IStabilityClassifier() {
* <i>read-only</i> with respect to the ForkedEvaluator.
*/
@Test
- public void testMissingInputCell() throws IOException {
+ public void testMissingInputCellH() throws IOException {
expectedEx.expect(UnsupportedOperationException.class);
expectedEx.expectMessage("Underlying cell 'A2' is missing in master sheet.");
- HSSFWorkbook wb = createWorkbook();
+ Workbook wb = createWorkbook();
try {
ForkedEvaluator fe = ForkedEvaluator.create(wb, null, null);