From: Nick Burch Date: Thu, 3 Jul 2014 10:56:25 +0000 (+0000) Subject: Formula evaluation and error handling example - reports what cells can't be evaluated... X-Git-Tag: REL_3_11_BETA1~107 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ac3de152a582218d9a5698d7c122b5c26c6d4619;p=poi.git 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 --- diff --git a/src/examples/src/org/apache/poi/ss/examples/formula/CheckFunctionsSupported.java b/src/examples/src/org/apache/poi/ss/examples/formula/CheckFunctionsSupported.java new file mode 100644 index 0000000000..0477e47327 --- /dev/null +++ b/src/examples/src/org/apache/poi/ss/examples/formula/CheckFunctionsSupported.java @@ -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. + * + *

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 "); + return; + } + + Workbook wb = WorkbookFactory.create(new File(args[0])); + CheckFunctionsSupported check = new CheckFunctionsSupported(wb); + + // Fetch all the problems + List problems = new ArrayList(); + for (int sn=0; sn unsupportedFunctions = new TreeSet(); + 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 getUnsupportedFunctions(String sheetName) { + return getUnsupportedFunctions(workbook.getSheet(sheetName)); + } + public Set getUnsupportedFunctions(int sheetIndex) { + return getUnsupportedFunctions(workbook.getSheetAt(sheetIndex)); + } + public Set 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 unsupportedFunctions = new HashSet(); + Map unevaluatableCells = new HashMap(); + + 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 unsupportedFunctions; + /** Which cells had unevaluatable formulas, and why? */ + public Map unevaluatableCells; + + protected FormulaEvaluationProblems(Set unsupportedFunctions, + Map unevaluatableCells) { + this.unsupportedFunctions = Collections.unmodifiableSet(unsupportedFunctions); + this.unevaluatableCells = Collections.unmodifiableMap(unevaluatableCells); + } + } +}