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.

ForkedEvaluationWorkbook.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.ss.formula.eval.forked;
  16. import java.util.HashMap;
  17. import java.util.Map;
  18. import org.apache.poi.ss.formula.ptg.NamePtg;
  19. import org.apache.poi.ss.formula.ptg.NameXPtg;
  20. import org.apache.poi.ss.formula.ptg.Ptg;
  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.udf.UDFFinder;
  26. import org.apache.poi.ss.usermodel.Workbook;
  27. /**
  28. * Represents a workbook being used for forked evaluation. Most operations are delegated to the
  29. * shared master workbook, except those that potentially involve cell values that may have been
  30. * updated after a call to {@link #getOrCreateUpdatableCell(String, int, int)}.
  31. *
  32. * @author Josh Micich
  33. */
  34. final class ForkedEvaluationWorkbook implements EvaluationWorkbook {
  35. private final EvaluationWorkbook _masterBook;
  36. private final Map<String, ForkedEvaluationSheet> _sharedSheetsByName;
  37. public ForkedEvaluationWorkbook(EvaluationWorkbook master) {
  38. _masterBook = master;
  39. _sharedSheetsByName = new HashMap<String, ForkedEvaluationSheet>();
  40. }
  41. public ForkedEvaluationCell getOrCreateUpdatableCell(String sheetName, int rowIndex,
  42. int columnIndex) {
  43. ForkedEvaluationSheet sheet = getSharedSheet(sheetName);
  44. return sheet.getOrCreateUpdatableCell(rowIndex, columnIndex);
  45. }
  46. public EvaluationCell getEvaluationCell(String sheetName, int rowIndex, int columnIndex) {
  47. ForkedEvaluationSheet sheet = getSharedSheet(sheetName);
  48. return sheet.getCell(rowIndex, columnIndex);
  49. }
  50. private ForkedEvaluationSheet getSharedSheet(String sheetName) {
  51. ForkedEvaluationSheet result = _sharedSheetsByName.get(sheetName);
  52. if (result == null) {
  53. result = new ForkedEvaluationSheet(_masterBook.getSheet(_masterBook
  54. .getSheetIndex(sheetName)));
  55. _sharedSheetsByName.put(sheetName, result);
  56. }
  57. return result;
  58. }
  59. public void copyUpdatedCells(Workbook workbook) {
  60. String[] sheetNames = new String[_sharedSheetsByName.size()];
  61. _sharedSheetsByName.keySet().toArray(sheetNames);
  62. OrderedSheet[] oss = new OrderedSheet[sheetNames.length];
  63. for (int i = 0; i < sheetNames.length; i++) {
  64. String sheetName = sheetNames[i];
  65. oss[i] = new OrderedSheet(sheetName, _masterBook.getSheetIndex(sheetName));
  66. }
  67. for (int i = 0; i < oss.length; i++) {
  68. String sheetName = oss[i].getSheetName();
  69. ForkedEvaluationSheet sheet = _sharedSheetsByName.get(sheetName);
  70. sheet.copyUpdatedCells(workbook.getSheet(sheetName));
  71. }
  72. }
  73. public int convertFromExternSheetIndex(int externSheetIndex) {
  74. return _masterBook.convertFromExternSheetIndex(externSheetIndex);
  75. }
  76. public ExternalSheet getExternalSheet(int externSheetIndex) {
  77. return _masterBook.getExternalSheet(externSheetIndex);
  78. }
  79. public Ptg[] getFormulaTokens(EvaluationCell cell) {
  80. if (cell instanceof ForkedEvaluationCell) {
  81. // doesn't happen yet because formulas cannot be modified from the master workbook
  82. throw new RuntimeException("Updated formulas not supported yet");
  83. }
  84. return _masterBook.getFormulaTokens(cell);
  85. }
  86. public EvaluationName getName(NamePtg namePtg) {
  87. return _masterBook.getName(namePtg);
  88. }
  89. public EvaluationName getName(String name, int sheetIndex){
  90. return _masterBook.getName(name, sheetIndex);
  91. }
  92. public EvaluationSheet getSheet(int sheetIndex) {
  93. return getSharedSheet(getSheetName(sheetIndex));
  94. }
  95. public ExternalName getExternalName(int externSheetIndex, int externNameIndex) {
  96. return _masterBook.getExternalName(externSheetIndex, externNameIndex);
  97. }
  98. public int getSheetIndex(EvaluationSheet sheet) {
  99. if (sheet instanceof ForkedEvaluationSheet) {
  100. ForkedEvaluationSheet mes = (ForkedEvaluationSheet) sheet;
  101. return mes.getSheetIndex(_masterBook);
  102. }
  103. return _masterBook.getSheetIndex(sheet);
  104. }
  105. public int getSheetIndex(String sheetName) {
  106. return _masterBook.getSheetIndex(sheetName);
  107. }
  108. public String getSheetName(int sheetIndex) {
  109. return _masterBook.getSheetName(sheetIndex);
  110. }
  111. public String resolveNameXText(NameXPtg ptg) {
  112. return _masterBook.resolveNameXText(ptg);
  113. }
  114. public UDFFinder getUDFFinder(){
  115. return _masterBook.getUDFFinder();
  116. }
  117. private static final class OrderedSheet implements Comparable<OrderedSheet> {
  118. private final String _sheetName;
  119. private final int _index;
  120. public OrderedSheet(String sheetName, int index) {
  121. _sheetName = sheetName;
  122. _index = index;
  123. }
  124. public String getSheetName() {
  125. return _sheetName;
  126. }
  127. public int compareTo(OrderedSheet o) {
  128. return _index - o._index;
  129. }
  130. }
  131. }