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.

HSSFEvaluationWorkbook.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.usermodel;
  16. import org.apache.poi.hssf.model.HSSFFormulaParser;
  17. import org.apache.poi.hssf.model.InternalWorkbook;
  18. import org.apache.poi.hssf.record.NameRecord;
  19. import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
  20. import org.apache.poi.ss.formula.ptg.NamePtg;
  21. import org.apache.poi.ss.formula.ptg.NameXPtg;
  22. import org.apache.poi.ss.formula.ptg.Ptg;
  23. import org.apache.poi.ss.SpreadsheetVersion;
  24. import org.apache.poi.ss.formula.EvaluationCell;
  25. import org.apache.poi.ss.formula.EvaluationName;
  26. import org.apache.poi.ss.formula.EvaluationSheet;
  27. import org.apache.poi.ss.formula.EvaluationWorkbook;
  28. import org.apache.poi.ss.formula.FormulaParseException;
  29. import org.apache.poi.ss.formula.FormulaParsingWorkbook;
  30. import org.apache.poi.ss.formula.FormulaRenderingWorkbook;
  31. import org.apache.poi.ss.formula.FormulaType;
  32. import org.apache.poi.ss.formula.udf.UDFFinder;
  33. /**
  34. * Internal POI use only
  35. *
  36. * @author Josh Micich
  37. */
  38. public final class HSSFEvaluationWorkbook implements FormulaRenderingWorkbook, EvaluationWorkbook, FormulaParsingWorkbook {
  39. private final HSSFWorkbook _uBook;
  40. private final InternalWorkbook _iBook;
  41. public static HSSFEvaluationWorkbook create(HSSFWorkbook book) {
  42. if (book == null) {
  43. return null;
  44. }
  45. return new HSSFEvaluationWorkbook(book);
  46. }
  47. private HSSFEvaluationWorkbook(HSSFWorkbook book) {
  48. _uBook = book;
  49. _iBook = book.getWorkbook();
  50. }
  51. public int getExternalSheetIndex(String sheetName) {
  52. int sheetIndex = _uBook.getSheetIndex(sheetName);
  53. return _iBook.checkExternSheet(sheetIndex);
  54. }
  55. public int getExternalSheetIndex(String workbookName, String sheetName) {
  56. return _iBook.getExternalSheetIndex(workbookName, sheetName);
  57. }
  58. public NameXPtg getNameXPtg(String name) {
  59. return _iBook.getNameXPtg(name, _uBook.getUDFFinder());
  60. }
  61. /**
  62. * Lookup a named range by its name.
  63. *
  64. * @param name the name to search
  65. * @param sheetIndex the 0-based index of the sheet this formula belongs to.
  66. * The sheet index is required to resolve sheet-level names. <code>-1</code> means workbook-global names
  67. */
  68. public EvaluationName getName(String name, int sheetIndex) {
  69. for(int i=0; i < _iBook.getNumNames(); i++) {
  70. NameRecord nr = _iBook.getNameRecord(i);
  71. if (nr.getSheetNumber() == sheetIndex+1 && name.equalsIgnoreCase(nr.getNameText())) {
  72. return new Name(nr, i);
  73. }
  74. }
  75. return sheetIndex == -1 ? null : getName(name, -1);
  76. }
  77. public int getSheetIndex(EvaluationSheet evalSheet) {
  78. HSSFSheet sheet = ((HSSFEvaluationSheet)evalSheet).getHSSFSheet();
  79. return _uBook.getSheetIndex(sheet);
  80. }
  81. public int getSheetIndex(String sheetName) {
  82. return _uBook.getSheetIndex(sheetName);
  83. }
  84. public String getSheetName(int sheetIndex) {
  85. return _uBook.getSheetName(sheetIndex);
  86. }
  87. public EvaluationSheet getSheet(int sheetIndex) {
  88. return new HSSFEvaluationSheet(_uBook.getSheetAt(sheetIndex));
  89. }
  90. public int convertFromExternSheetIndex(int externSheetIndex) {
  91. return _iBook.getSheetIndexFromExternSheetIndex(externSheetIndex);
  92. }
  93. public ExternalSheet getExternalSheet(int externSheetIndex) {
  94. return _iBook.getExternalSheet(externSheetIndex);
  95. }
  96. public ExternalName getExternalName(int externSheetIndex, int externNameIndex) {
  97. return _iBook.getExternalName(externSheetIndex, externNameIndex);
  98. }
  99. public String resolveNameXText(NameXPtg n) {
  100. return _iBook.resolveNameXText(n.getSheetRefIndex(), n.getNameIndex());
  101. }
  102. public String getSheetNameByExternSheet(int externSheetIndex) {
  103. return _iBook.findSheetNameFromExternSheet(externSheetIndex);
  104. }
  105. public String getNameText(NamePtg namePtg) {
  106. return _iBook.getNameRecord(namePtg.getIndex()).getNameText();
  107. }
  108. public EvaluationName getName(NamePtg namePtg) {
  109. int ix = namePtg.getIndex();
  110. return new Name(_iBook.getNameRecord(ix), ix);
  111. }
  112. public Ptg[] getFormulaTokens(EvaluationCell evalCell) {
  113. HSSFCell cell = ((HSSFEvaluationCell)evalCell).getHSSFCell();
  114. if (false) {
  115. // re-parsing the formula text also works, but is a waste of time
  116. // It is useful from time to time to run all unit tests with this code
  117. // to make sure that all formulas POI can evaluate can also be parsed.
  118. try {
  119. return HSSFFormulaParser.parse(cell.getCellFormula(), _uBook, FormulaType.CELL, _uBook.getSheetIndex(cell.getSheet()));
  120. } catch (FormulaParseException e) {
  121. // Note - as of Bugzilla 48036 (svn r828244, r828247) POI is capable of evaluating
  122. // IntesectionPtg. However it is still not capable of parsing it.
  123. // So FormulaEvalTestData.xls now contains a few formulas that produce errors here.
  124. System.err.println(e.getMessage());
  125. }
  126. }
  127. FormulaRecordAggregate fra = (FormulaRecordAggregate) cell.getCellValueRecord();
  128. return fra.getFormulaTokens();
  129. }
  130. public UDFFinder getUDFFinder(){
  131. return _uBook.getUDFFinder();
  132. }
  133. private static final class Name implements EvaluationName {
  134. private final NameRecord _nameRecord;
  135. private final int _index;
  136. public Name(NameRecord nameRecord, int index) {
  137. _nameRecord = nameRecord;
  138. _index = index;
  139. }
  140. public Ptg[] getNameDefinition() {
  141. return _nameRecord.getNameDefinition();
  142. }
  143. public String getNameText() {
  144. return _nameRecord.getNameText();
  145. }
  146. public boolean hasFormula() {
  147. return _nameRecord.hasFormula();
  148. }
  149. public boolean isFunctionName() {
  150. return _nameRecord.isFunctionName();
  151. }
  152. public boolean isRange() {
  153. return _nameRecord.hasFormula(); // TODO - is this right?
  154. }
  155. public NamePtg createPtg() {
  156. return new NamePtg(_index);
  157. }
  158. }
  159. public SpreadsheetVersion getSpreadsheetVersion(){
  160. return SpreadsheetVersion.EXCEL97;
  161. }
  162. }