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.

HSSFFormulaParser.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.hssf.model;
  16. import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook;
  17. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  18. import org.apache.poi.ss.formula.FormulaParseException;
  19. import org.apache.poi.ss.formula.FormulaParser;
  20. import org.apache.poi.ss.formula.FormulaParsingWorkbook;
  21. import org.apache.poi.ss.formula.FormulaRenderer;
  22. import org.apache.poi.ss.formula.FormulaType;
  23. import org.apache.poi.ss.formula.ptg.Ptg;
  24. import org.apache.poi.util.Internal;
  25. import org.apache.poi.util.Removal;
  26. /**
  27. * HSSF wrapper for the {@link FormulaParser} and {@link FormulaRenderer}
  28. */
  29. @Internal
  30. public final class HSSFFormulaParser {
  31. private static FormulaParsingWorkbook createParsingWorkbook(HSSFWorkbook book) {
  32. return HSSFEvaluationWorkbook.create(book);
  33. }
  34. private HSSFFormulaParser() {
  35. // no instances of this class
  36. }
  37. /**
  38. * Convenience method for parsing cell formulas. see {@link #parse(String, HSSFWorkbook, FormulaType, int)}
  39. * @param formula The formula to parse, excluding the leading equals sign
  40. * @param workbook The parent workbook
  41. * @return the parsed formula tokens
  42. * @throws FormulaParseException if the formula has incorrect syntax or is otherwise invalid
  43. */
  44. public static Ptg[] parse(String formula, HSSFWorkbook workbook) throws FormulaParseException {
  45. return parse(formula, workbook, FormulaType.CELL);
  46. }
  47. /**
  48. * @param formula The formula to parse, excluding the leading equals sign
  49. * @param workbook The parent workbook
  50. * @param formulaType a constant from {@link FormulaType}
  51. * @return the parsed formula tokens
  52. * @throws FormulaParseException if the formula has incorrect syntax or is otherwise invalid
  53. *
  54. * @deprecated POI 3.15 beta 3. Use {@link #parse(String, HSSFWorkbook, FormulaType)} instead.
  55. */
  56. @Removal(version="3.17")
  57. public static Ptg[] parse(String formula, HSSFWorkbook workbook, int formulaType) throws FormulaParseException {
  58. return parse(formula, workbook, FormulaType.forInt(formulaType));
  59. }
  60. /**
  61. * @param formula The formula to parse, excluding the leading equals sign
  62. * @param workbook The parent workbook
  63. * @param formulaType The type of formula
  64. * @return The parsed formula tokens
  65. * @throws FormulaParseException if the formula has incorrect syntax or is otherwise invalid
  66. */
  67. public static Ptg[] parse(String formula, HSSFWorkbook workbook, FormulaType formulaType) throws FormulaParseException {
  68. return parse(formula, workbook, formulaType, -1);
  69. }
  70. /**
  71. * @param formula the formula to parse
  72. * @param workbook the parent workbook
  73. * @param formulaType a constant from {@link FormulaType}
  74. * @param sheetIndex the 0-based index of the sheet this formula belongs to.
  75. * The sheet index is required to resolve sheet-level names. <code>-1</code> means that
  76. * the scope of the name will be ignored and the parser will match named ranges only by name
  77. *
  78. * @return the parsed formula tokens
  79. * @throws FormulaParseException if the formula has incorrect syntax or is otherwise invalid
  80. * @deprecated POI 3.15 beta 3. Use {@link #parse(String, HSSFWorkbook, FormulaType, int)} instead.
  81. */
  82. @Removal(version="3.17")
  83. public static Ptg[] parse(String formula, HSSFWorkbook workbook, int formulaType, int sheetIndex) throws FormulaParseException {
  84. return parse(formula, workbook, FormulaType.forInt(formulaType), sheetIndex);
  85. }
  86. /**
  87. * @param formula The formula to parse
  88. * @param workbook The parent workbook
  89. * @param formulaType The type of formula
  90. * @param sheetIndex The 0-based index of the sheet this formula belongs to.
  91. * The sheet index is required to resolve sheet-level names. <code>-1</code> means that
  92. * the scope of the name will be ignored and the parser will match named ranges only by name
  93. *
  94. * @return the parsed formula tokens
  95. * @throws FormulaParseException if the formula has incorrect syntax or is otherwise invalid
  96. */
  97. public static Ptg[] parse(String formula, HSSFWorkbook workbook, FormulaType formulaType, int sheetIndex) throws FormulaParseException {
  98. return FormulaParser.parse(formula, createParsingWorkbook(workbook), formulaType, sheetIndex);
  99. }
  100. /**
  101. * Static method to convert an array of {@link Ptg}s in RPN order
  102. * to a human readable string format in infix mode.
  103. * @param book used for defined names and 3D references
  104. * @param ptgs must not be <code>null</code>
  105. * @return a human readable String
  106. */
  107. public static String toFormulaString(HSSFWorkbook book, Ptg[] ptgs) {
  108. return FormulaRenderer.toFormulaString(HSSFEvaluationWorkbook.create(book), ptgs);
  109. }
  110. }