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 6.6KB

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