Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ArrayFunction.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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.functions;
  16. import org.apache.poi.ss.formula.CacheAreaEval;
  17. import org.apache.poi.ss.formula.FormulaParseException;
  18. import org.apache.poi.ss.formula.eval.*;
  19. import java.util.function.BiFunction;
  20. /**
  21. * Common Interface for any excel built-in function that has implemented array formula functionality.
  22. */
  23. public interface ArrayFunction {
  24. /**
  25. * @param args the evaluated function arguments. Empty values are represented with
  26. * {@link BlankEval} or {@link MissingArgEval}, never {@code null}.
  27. * @param srcRowIndex row index of the cell containing the formula under evaluation
  28. * @param srcColumnIndex column index of the cell containing the formula under evaluation
  29. * @return The evaluated result, possibly an {@link ErrorEval}, never {@code null}.
  30. * <b>Note</b> - Excel uses the error code <i>#NUM!</i> instead of IEEE <i>NaN</i>, so when
  31. * numeric functions evaluate to {@link Double#NaN} be sure to translate the result to {@link
  32. * ErrorEval#NUM_ERROR}.
  33. */
  34. ValueEval evaluateArray(ValueEval[] args, int srcRowIndex, int srcColumnIndex);
  35. /**
  36. * Evaluate an array function with two arguments.
  37. *
  38. * @param arg0 the first function argument. Empty values are represented with
  39. * {@link BlankEval} or {@link MissingArgEval}, never {@code null}
  40. * @param arg1 the first function argument. Empty values are represented with
  41. * {@link BlankEval} or {@link MissingArgEval}, never {@code null}
  42. *
  43. * @param srcRowIndex row index of the cell containing the formula under evaluation
  44. * @param srcColumnIndex column index of the cell containing the formula under evaluation
  45. * @return The evaluated result, possibly an {@link ErrorEval}, never {@code null}.
  46. * <b>Note</b> - Excel uses the error code <i>#NUM!</i> instead of IEEE <i>NaN</i>, so when
  47. * numeric functions evaluate to {@link Double#NaN} be sure to translate the result to {@link
  48. * ErrorEval#NUM_ERROR}.
  49. */
  50. default ValueEval evaluateTwoArrayArgs(ValueEval arg0, ValueEval arg1, int srcRowIndex, int srcColumnIndex,
  51. BiFunction<ValueEval, ValueEval, ValueEval> evalFunc) {
  52. return _evaluateTwoArrayArgs(arg0, arg1, srcRowIndex, srcColumnIndex, evalFunc);
  53. }
  54. default ValueEval evaluateOneArrayArg(ValueEval arg0, int srcRowIndex, int srcColumnIndex,
  55. java.util.function.Function<ValueEval, ValueEval> evalFunc) {
  56. return _evaluateOneArrayArg(arg0, srcRowIndex, srcColumnIndex, evalFunc);
  57. }
  58. static ValueEval _evaluateTwoArrayArgs(ValueEval arg0, ValueEval arg1, int srcRowIndex, int srcColumnIndex,
  59. BiFunction<ValueEval, ValueEval, ValueEval> evalFunc) {
  60. int w1, w2, h1, h2;
  61. int a1FirstCol = 0, a1FirstRow = 0;
  62. if (arg0 instanceof AreaEval) {
  63. AreaEval ae = (AreaEval)arg0;
  64. w1 = ae.getWidth();
  65. h1 = ae.getHeight();
  66. a1FirstCol = ae.getFirstColumn();
  67. a1FirstRow = ae.getFirstRow();
  68. } else if (arg0 instanceof RefEval){
  69. RefEval ref = (RefEval)arg0;
  70. w1 = 1;
  71. h1 = 1;
  72. a1FirstCol = ref.getColumn();
  73. a1FirstRow = ref.getRow();
  74. } else {
  75. w1 = 1;
  76. h1 = 1;
  77. }
  78. int a2FirstCol = 0, a2FirstRow = 0;
  79. if (arg1 instanceof AreaEval) {
  80. AreaEval ae = (AreaEval)arg1;
  81. w2 = ae.getWidth();
  82. h2 = ae.getHeight();
  83. a2FirstCol = ae.getFirstColumn();
  84. a2FirstRow = ae.getFirstRow();
  85. } else if (arg1 instanceof RefEval){
  86. RefEval ref = (RefEval)arg1;
  87. w2 = 1;
  88. h2 = 1;
  89. a2FirstCol = ref.getColumn();
  90. a2FirstRow = ref.getRow();
  91. } else {
  92. w2 = 1;
  93. h2 = 1;
  94. }
  95. int width = Math.max(w1, w2);
  96. int height = Math.max(h1, h2);
  97. ValueEval[] vals = new ValueEval[height * width];
  98. int idx = 0;
  99. for(int i = 0; i < height; i++){
  100. for(int j = 0; j < width; j++){
  101. ValueEval vA;
  102. try {
  103. vA = OperandResolver.getSingleValue(arg0, a1FirstRow + i, a1FirstCol + j);
  104. } catch (FormulaParseException e) {
  105. vA = ErrorEval.NAME_INVALID;
  106. } catch (EvaluationException e) {
  107. vA = e.getErrorEval();
  108. } catch (RuntimeException e) {
  109. if(e.getMessage().startsWith("Don't know how to evaluate name")){
  110. vA = ErrorEval.NAME_INVALID;
  111. } else {
  112. throw e;
  113. }
  114. }
  115. ValueEval vB;
  116. try {
  117. vB = OperandResolver.getSingleValue(arg1, a2FirstRow + i, a2FirstCol + j);
  118. } catch (FormulaParseException e) {
  119. vB = ErrorEval.NAME_INVALID;
  120. } catch (EvaluationException e) {
  121. vB = e.getErrorEval();
  122. } catch (RuntimeException e) {
  123. if(e.getMessage().startsWith("Don't know how to evaluate name")){
  124. vB = ErrorEval.NAME_INVALID;
  125. } else {
  126. throw e;
  127. }
  128. }
  129. if(vA instanceof ErrorEval){
  130. vals[idx++] = vA;
  131. } else if (vB instanceof ErrorEval) {
  132. vals[idx++] = vB;
  133. } else {
  134. vals[idx++] = evalFunc.apply(vA, vB);
  135. }
  136. }
  137. }
  138. if (vals.length == 1) {
  139. return vals[0];
  140. }
  141. return new CacheAreaEval(srcRowIndex, srcColumnIndex, srcRowIndex + height - 1, srcColumnIndex + width - 1, vals);
  142. }
  143. static ValueEval _evaluateOneArrayArg(ValueEval arg0, int srcRowIndex, int srcColumnIndex,
  144. java.util.function.Function<ValueEval, ValueEval> evalFunc){
  145. int w1, w2, h1, h2;
  146. int a1FirstCol = 0, a1FirstRow = 0;
  147. if (arg0 instanceof AreaEval) {
  148. AreaEval ae = (AreaEval)arg0;
  149. w1 = ae.getWidth();
  150. h1 = ae.getHeight();
  151. a1FirstCol = ae.getFirstColumn();
  152. a1FirstRow = ae.getFirstRow();
  153. } else if (arg0 instanceof RefEval){
  154. RefEval ref = (RefEval)arg0;
  155. w1 = 1;
  156. h1 = 1;
  157. a1FirstCol = ref.getColumn();
  158. a1FirstRow = ref.getRow();
  159. } else {
  160. w1 = 1;
  161. h1 = 1;
  162. }
  163. w2 = 1;
  164. h2 = 1;
  165. int width = Math.max(w1, w2);
  166. int height = Math.max(h1, h2);
  167. ValueEval[] vals = new ValueEval[height * width];
  168. int idx = 0;
  169. for(int i = 0; i < height; i++){
  170. for(int j = 0; j < width; j++){
  171. ValueEval vA;
  172. try {
  173. vA = OperandResolver.getSingleValue(arg0, a1FirstRow + i, a1FirstCol + j);
  174. } catch (FormulaParseException e) {
  175. vA = ErrorEval.NAME_INVALID;
  176. } catch (EvaluationException e) {
  177. vA = e.getErrorEval();
  178. } catch (RuntimeException e) {
  179. if(e.getMessage().startsWith("Don't know how to evaluate name")){
  180. vA = ErrorEval.NAME_INVALID;
  181. } else {
  182. throw e;
  183. }
  184. }
  185. vals[idx++] = evalFunc.apply(vA);
  186. }
  187. }
  188. if (vals.length == 1) {
  189. return vals[0];
  190. }
  191. return new CacheAreaEval(srcRowIndex, srcColumnIndex, srcRowIndex + height - 1, srcColumnIndex + width - 1, vals);
  192. }
  193. }