You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CheckFunctionsSupported.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.examples.ss.formula;
  16. import java.io.File;
  17. import java.util.ArrayList;
  18. import java.util.Collections;
  19. import java.util.HashMap;
  20. import java.util.HashSet;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Set;
  24. import java.util.TreeSet;
  25. import org.apache.poi.ss.formula.eval.NotImplementedException;
  26. import org.apache.poi.ss.formula.eval.NotImplementedFunctionException;
  27. import org.apache.poi.ss.usermodel.Cell;
  28. import org.apache.poi.ss.usermodel.FormulaEvaluator;
  29. import org.apache.poi.ss.usermodel.Row;
  30. import org.apache.poi.ss.usermodel.Sheet;
  31. import org.apache.poi.ss.usermodel.Workbook;
  32. import org.apache.poi.ss.usermodel.WorkbookFactory;
  33. import org.apache.poi.ss.util.CellReference;
  34. /**
  35. * Attempts to re-evaluate all the formulas in the workbook, and
  36. * reports what (if any) formula functions used are not (currently)
  37. * supported by Apache POI.
  38. *
  39. * <p>This provides examples of how to evaluate formulas in excel
  40. * files using Apache POI, along with how to handle errors whilst
  41. * doing so.
  42. */
  43. @SuppressWarnings({"java:S106","java:S4823","java:S1192"})
  44. public class CheckFunctionsSupported {
  45. public static void main(String[] args) throws Exception {
  46. if (args.length < 1) {
  47. System.err.println("Use:");
  48. System.err.println(" CheckFunctionsSupported <filename>");
  49. return;
  50. }
  51. Workbook wb = WorkbookFactory.create(new File(args[0]));
  52. CheckFunctionsSupported check = new CheckFunctionsSupported(wb);
  53. // Fetch all the problems
  54. List<FormulaEvaluationProblems> problems = new ArrayList<>();
  55. for (int sn=0; sn<wb.getNumberOfSheets(); sn++) {
  56. problems.add(check.getEvaluationProblems(sn));
  57. }
  58. // Produce an overall summary
  59. Set<String> unsupportedFunctions = new TreeSet<>();
  60. for (FormulaEvaluationProblems p : problems) {
  61. unsupportedFunctions.addAll(p.unsupportedFunctions);
  62. }
  63. if (unsupportedFunctions.isEmpty()) {
  64. System.out.println("There are no unsupported formula functions used");
  65. } else {
  66. System.out.println("Unsupported formula functions:");
  67. for (String function : unsupportedFunctions) {
  68. System.out.println(" " + function);
  69. }
  70. System.out.println("Total unsupported functions = " + unsupportedFunctions.size());
  71. }
  72. // Report sheet by sheet
  73. for (int sn=0; sn<wb.getNumberOfSheets(); sn++) {
  74. String sheetName = wb.getSheetName(sn);
  75. FormulaEvaluationProblems probs = problems.get(sn);
  76. System.out.println();
  77. System.out.println("Sheet = " + sheetName);
  78. if (probs.unevaluatableCells.isEmpty()) {
  79. System.out.println(" All cells evaluated without error");
  80. } else {
  81. for (CellReference cr : probs.unevaluatableCells.keySet()) {
  82. System.out.println(" " + cr.formatAsString() + " - " +
  83. probs.unevaluatableCells.get(cr));
  84. }
  85. }
  86. }
  87. }
  88. private final Workbook workbook;
  89. private final FormulaEvaluator evaluator;
  90. public CheckFunctionsSupported(Workbook workbook) {
  91. this.workbook = workbook;
  92. this.evaluator = workbook.getCreationHelper().createFormulaEvaluator();
  93. }
  94. public Set<String> getUnsupportedFunctions(String sheetName) {
  95. return getUnsupportedFunctions(workbook.getSheet(sheetName));
  96. }
  97. public Set<String> getUnsupportedFunctions(int sheetIndex) {
  98. return getUnsupportedFunctions(workbook.getSheetAt(sheetIndex));
  99. }
  100. public Set<String> getUnsupportedFunctions(Sheet sheet) {
  101. FormulaEvaluationProblems problems = getEvaluationProblems(sheet);
  102. return problems.unsupportedFunctions;
  103. }
  104. public FormulaEvaluationProblems getEvaluationProblems(String sheetName) {
  105. return getEvaluationProblems(workbook.getSheet(sheetName));
  106. }
  107. public FormulaEvaluationProblems getEvaluationProblems(int sheetIndex) {
  108. return getEvaluationProblems(workbook.getSheetAt(sheetIndex));
  109. }
  110. public FormulaEvaluationProblems getEvaluationProblems(Sheet sheet) {
  111. Set<String> unsupportedFunctions = new HashSet<>();
  112. Map<CellReference,Exception> unevaluatableCells = new HashMap<>();
  113. for (Row r : sheet) {
  114. for (Cell c : r) {
  115. try {
  116. evaluator.evaluate(c);
  117. } catch (Exception e) {
  118. if (e instanceof NotImplementedException && e.getCause() != null) {
  119. // Has been wrapped with cell details, but we know those
  120. e = (Exception)e.getCause();
  121. }
  122. if (e instanceof NotImplementedFunctionException) {
  123. NotImplementedFunctionException nie = (NotImplementedFunctionException)e;
  124. unsupportedFunctions.add(nie.getFunctionName());
  125. }
  126. unevaluatableCells.put(new CellReference(c), e);
  127. }
  128. }
  129. }
  130. return new FormulaEvaluationProblems(unsupportedFunctions, unevaluatableCells);
  131. }
  132. public static class FormulaEvaluationProblems {
  133. /** Which used functions are unsupported by POI at this time */
  134. private final Set<String> unsupportedFunctions;
  135. /** Which cells had unevaluatable formulas, and why? */
  136. private final Map<CellReference,Exception> unevaluatableCells;
  137. protected FormulaEvaluationProblems(Set<String> unsupportedFunctions,
  138. Map<CellReference, Exception> unevaluatableCells) {
  139. this.unsupportedFunctions = Collections.unmodifiableSet(unsupportedFunctions);
  140. this.unevaluatableCells = Collections.unmodifiableMap(unevaluatableCells);
  141. }
  142. }
  143. }