Formula evaluation and error handling example - reports what cells can't be evaluated, and (TODO) what functions are used but not implemented

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1607583 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2014-07-03 10:56:25 +00:00
parent f33843810a
commit 7bc0767bf5

View File

@ -0,0 +1,156 @@
/* ====================================================================
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.examples.formula;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.apache.poi.ss.formula.eval.NotImplementedException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.CellReference;
/**
* Attempts to re-evaluate all the formulas in the workbook, and
* reports what (if any) formula functions used are not (currently)
* supported by Apache POI.
*
* <p>This provides examples of how to evaluate formulas in excel
* files using Apache POI, along with how to handle errors whilst
* doing so.
*/
public class CheckFunctionsSupported {
public static void main(String[] args) throws Exception {
if (args.length < 1) {
System.err.println("Use:");
System.err.println(" CheckFunctionsSupported <filename>");
return;
}
Workbook wb = WorkbookFactory.create(new File(args[0]));
CheckFunctionsSupported check = new CheckFunctionsSupported(wb);
// Fetch all the problems
List<FormulaEvaluationProblems> problems = new ArrayList<CheckFunctionsSupported.FormulaEvaluationProblems>();
for (int sn=0; sn<wb.getNumberOfSheets(); sn++) {
problems.add(check.getEvaluationProblems(sn));
}
// Produce an overall summary
Set<String> unsupportedFunctions = new TreeSet<String>();
for (FormulaEvaluationProblems p : problems) {
unsupportedFunctions.addAll(p.unsupportedFunctions);
}
if (unsupportedFunctions.isEmpty()) {
System.out.println("There are no unsupported formula functions used");
} else {
System.out.println("Unsupported formula functions:");
for (String function : unsupportedFunctions) {
System.out.println(" " + function);
}
System.out.println("Total unsupported functions = " + unsupportedFunctions.size());
}
// Report sheet by sheet
for (int sn=0; sn<wb.getNumberOfSheets(); sn++) {
String sheetName = wb.getSheetName(sn);
FormulaEvaluationProblems probs = problems.get(sn);
System.out.println();
System.out.println("Sheet = " + sheetName);
if (probs.unevaluatableCells.isEmpty()) {
System.out.println(" All cells evaluated without error");
} else {
for (CellReference cr : probs.unevaluatableCells.keySet()) {
System.out.println(" " + cr.formatAsString() + " - " +
probs.unevaluatableCells.get(cr).toString());
}
}
}
}
private Workbook workbook;
private FormulaEvaluator evaluator;
public CheckFunctionsSupported(Workbook workbook) {
this.workbook = workbook;
this.evaluator = workbook.getCreationHelper().createFormulaEvaluator();
}
public Set<String> getUnsupportedFunctions(String sheetName) {
return getUnsupportedFunctions(workbook.getSheet(sheetName));
}
public Set<String> getUnsupportedFunctions(int sheetIndex) {
return getUnsupportedFunctions(workbook.getSheetAt(sheetIndex));
}
public Set<String> getUnsupportedFunctions(Sheet sheet) {
FormulaEvaluationProblems problems = getEvaluationProblems(sheet);
return problems.unsupportedFunctions;
}
public FormulaEvaluationProblems getEvaluationProblems(String sheetName) {
return getEvaluationProblems(workbook.getSheet(sheetName));
}
public FormulaEvaluationProblems getEvaluationProblems(int sheetIndex) {
return getEvaluationProblems(workbook.getSheetAt(sheetIndex));
}
public FormulaEvaluationProblems getEvaluationProblems(Sheet sheet) {
Set<String> unsupportedFunctions = new HashSet<String>();
Map<CellReference,Exception> unevaluatableCells = new HashMap<CellReference, Exception>();
for (Row r : sheet) {
for (Cell c : r) {
try {
evaluator.evaluate(c);
} catch (Exception e) {
if (e instanceof NotImplementedException) {
NotImplementedException nie = (NotImplementedException)e;
// TODO
System.err.println("TODO - Not Implemented: " + nie);
}
unevaluatableCells.put(new CellReference(c), e);
}
}
}
return new FormulaEvaluationProblems(unsupportedFunctions, unevaluatableCells);
}
public static class FormulaEvaluationProblems {
/** Which used functions are unsupported by POI at this time */
public Set<String> unsupportedFunctions;
/** Which cells had unevaluatable formulas, and why? */
public Map<CellReference,Exception> unevaluatableCells;
protected FormulaEvaluationProblems(Set<String> unsupportedFunctions,
Map<CellReference, Exception> unevaluatableCells) {
this.unsupportedFunctions = Collections.unmodifiableSet(unsupportedFunctions);
this.unevaluatableCells = Collections.unmodifiableMap(unevaluatableCells);
}
}
}