All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.poi.ss.examples.formula.CheckFunctionsSupported Maven / Gradle / Ivy

/* ====================================================================
   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.formula.eval.NotImplementedFunctionException;
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 && e.getCause() != null) { // Has been wrapped with cell details, but we know those e = (Exception)e.getCause(); } if (e instanceof NotImplementedFunctionException) { NotImplementedFunctionException nie = (NotImplementedFunctionException)e; unsupportedFunctions.add(nie.getFunctionName()); } 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); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy