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.

XSSFEvaluationWorkbook.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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.xssf.usermodel;
  16. import org.apache.poi.ss.SpreadsheetVersion;
  17. import org.apache.poi.ss.formula.EvaluationCell;
  18. import org.apache.poi.ss.formula.EvaluationName;
  19. import org.apache.poi.ss.formula.EvaluationSheet;
  20. import org.apache.poi.ss.formula.EvaluationWorkbook;
  21. import org.apache.poi.ss.formula.FormulaParser;
  22. import org.apache.poi.ss.formula.FormulaParsingWorkbook;
  23. import org.apache.poi.ss.formula.FormulaRenderingWorkbook;
  24. import org.apache.poi.ss.formula.FormulaType;
  25. import org.apache.poi.ss.formula.functions.FreeRefFunction;
  26. import org.apache.poi.ss.formula.ptg.NamePtg;
  27. import org.apache.poi.ss.formula.ptg.NameXPtg;
  28. import org.apache.poi.ss.formula.ptg.Ptg;
  29. import org.apache.poi.ss.formula.udf.IndexedUDFFinder;
  30. import org.apache.poi.ss.formula.udf.UDFFinder;
  31. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedName;
  32. /**
  33. * Internal POI use only
  34. *
  35. * @author Josh Micich
  36. */
  37. public final class XSSFEvaluationWorkbook implements FormulaRenderingWorkbook, EvaluationWorkbook, FormulaParsingWorkbook {
  38. private final XSSFWorkbook _uBook;
  39. public static XSSFEvaluationWorkbook create(XSSFWorkbook book) {
  40. if (book == null) {
  41. return null;
  42. }
  43. return new XSSFEvaluationWorkbook(book);
  44. }
  45. private XSSFEvaluationWorkbook(XSSFWorkbook book) {
  46. _uBook = book;
  47. }
  48. private int convertFromExternalSheetIndex(int externSheetIndex) {
  49. return externSheetIndex;
  50. }
  51. /**
  52. * @return the sheet index of the sheet with the given external index.
  53. */
  54. public int convertFromExternSheetIndex(int externSheetIndex) {
  55. return externSheetIndex;
  56. }
  57. /**
  58. * @return the external sheet index of the sheet with the given internal
  59. * index. Used by some of the more obscure formula and named range things.
  60. * Fairly easy on XSSF (we think...) since the internal and external
  61. * indices are the same
  62. */
  63. private int convertToExternalSheetIndex(int sheetIndex) {
  64. return sheetIndex;
  65. }
  66. public int getExternalSheetIndex(String sheetName) {
  67. int sheetIndex = _uBook.getSheetIndex(sheetName);
  68. return convertToExternalSheetIndex(sheetIndex);
  69. }
  70. public EvaluationName getName(String name, int sheetIndex) {
  71. for (int i = 0; i < _uBook.getNumberOfNames(); i++) {
  72. XSSFName nm = _uBook.getNameAt(i);
  73. String nameText = nm.getNameName();
  74. if (name.equalsIgnoreCase(nameText) && nm.getSheetIndex() == sheetIndex) {
  75. return new Name(_uBook.getNameAt(i), i, this);
  76. }
  77. }
  78. return sheetIndex == -1 ? null : getName(name, -1);
  79. }
  80. public int getSheetIndex(EvaluationSheet evalSheet) {
  81. XSSFSheet sheet = ((XSSFEvaluationSheet)evalSheet).getXSSFSheet();
  82. return _uBook.getSheetIndex(sheet);
  83. }
  84. public String getSheetName(int sheetIndex) {
  85. return _uBook.getSheetName(sheetIndex);
  86. }
  87. public ExternalName getExternalName(int externSheetIndex, int externNameIndex) {
  88. throw new RuntimeException("Not implemented yet");
  89. }
  90. public NameXPtg getNameXPtg(String name, int sheetRefIndex) {
  91. // First, try to find it as a User Defined Function
  92. IndexedUDFFinder udfFinder = (IndexedUDFFinder)getUDFFinder();
  93. FreeRefFunction func = udfFinder.findFunction(name);
  94. if (func != null) {
  95. return new NameXPtg(0, udfFinder.getFunctionIndex(name));
  96. }
  97. // Otherwise, try it as a named range
  98. XSSFName xname = _uBook.getName(name);
  99. if (xname != null) {
  100. int nameAt = _uBook.getNameIndex(name);
  101. return new NameXPtg(xname.getSheetIndex(), nameAt);
  102. } else {
  103. return null;
  104. }
  105. }
  106. public String resolveNameXText(NameXPtg n) {
  107. int idx = n.getNameIndex();
  108. String name = null;
  109. // First, try to find it as a User Defined Function
  110. IndexedUDFFinder udfFinder = (IndexedUDFFinder)getUDFFinder();
  111. name = udfFinder.getFunctionName(idx);
  112. if (name != null) return name;
  113. // Otherwise, try it as a named range
  114. XSSFName xname = _uBook.getNameAt(idx);
  115. if (xname != null) {
  116. name = xname.getNameName();
  117. }
  118. return name;
  119. }
  120. public EvaluationSheet getSheet(int sheetIndex) {
  121. return new XSSFEvaluationSheet(_uBook.getSheetAt(sheetIndex));
  122. }
  123. public ExternalSheet getExternalSheet(int externSheetIndex) {
  124. // TODO Auto-generated method stub
  125. return null;
  126. }
  127. public int getExternalSheetIndex(String workbookName, String sheetName) {
  128. throw new RuntimeException("not implemented yet");
  129. }
  130. public int getSheetIndex(String sheetName) {
  131. return _uBook.getSheetIndex(sheetName);
  132. }
  133. public String getSheetNameByExternSheet(int externSheetIndex) {
  134. int sheetIndex = convertFromExternalSheetIndex(externSheetIndex);
  135. return _uBook.getSheetName(sheetIndex);
  136. }
  137. public String getNameText(NamePtg namePtg) {
  138. return _uBook.getNameAt(namePtg.getIndex()).getNameName();
  139. }
  140. public EvaluationName getName(NamePtg namePtg) {
  141. int ix = namePtg.getIndex();
  142. return new Name(_uBook.getNameAt(ix), ix, this);
  143. }
  144. public Ptg[] getFormulaTokens(EvaluationCell evalCell) {
  145. XSSFCell cell = ((XSSFEvaluationCell)evalCell).getXSSFCell();
  146. XSSFEvaluationWorkbook frBook = XSSFEvaluationWorkbook.create(_uBook);
  147. return FormulaParser.parse(cell.getCellFormula(), frBook, FormulaType.CELL, _uBook.getSheetIndex(cell.getSheet()));
  148. }
  149. public UDFFinder getUDFFinder(){
  150. return _uBook.getUDFFinder();
  151. }
  152. private static final class Name implements EvaluationName {
  153. private final XSSFName _nameRecord;
  154. private final int _index;
  155. private final FormulaParsingWorkbook _fpBook;
  156. public Name(XSSFName name, int index, FormulaParsingWorkbook fpBook) {
  157. _nameRecord = name;
  158. _index = index;
  159. _fpBook = fpBook;
  160. }
  161. public Ptg[] getNameDefinition() {
  162. return FormulaParser.parse(_nameRecord.getRefersToFormula(), _fpBook, FormulaType.NAMEDRANGE, _nameRecord.getSheetIndex());
  163. }
  164. public String getNameText() {
  165. return _nameRecord.getNameName();
  166. }
  167. public boolean hasFormula() {
  168. // TODO - no idea if this is right
  169. CTDefinedName ctn = _nameRecord.getCTName();
  170. String strVal = ctn.getStringValue();
  171. return !ctn.getFunction() && strVal != null && strVal.length() > 0;
  172. }
  173. public boolean isFunctionName() {
  174. return _nameRecord.isFunctionName();
  175. }
  176. public boolean isRange() {
  177. return hasFormula(); // TODO - is this right?
  178. }
  179. public NamePtg createPtg() {
  180. return new NamePtg(_index);
  181. }
  182. }
  183. public SpreadsheetVersion getSpreadsheetVersion(){
  184. return SpreadsheetVersion.EXCEL2007;
  185. }
  186. }