You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by fa...@apache.org on 2021/05/22 20:56:49 UTC
svn commit: r1890120 [19/43] - in /poi/trunk/poi/src:
main/java/org/apache/poi/ main/java/org/apache/poi/ddf/
main/java/org/apache/poi/extractor/ main/java/org/apache/poi/hpsf/
main/java/org/apache/poi/hssf/ main/java/org/apache/poi/hssf/dev/
main/java...
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Choose.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Choose.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Choose.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Choose.java Sat May 22 20:56:44 2021
@@ -26,29 +26,29 @@ import org.apache.poi.ss.formula.eval.Va
public final class Choose implements Function {
- public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
- if (args.length < 2) {
- return ErrorEval.VALUE_INVALID;
- }
+ public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
+ if (args.length < 2) {
+ return ErrorEval.VALUE_INVALID;
+ }
- try {
- int ix = evaluateFirstArg(args[0], srcRowIndex, srcColumnIndex);
- if (ix < 1 || ix >= args.length) {
- return ErrorEval.VALUE_INVALID;
- }
- ValueEval result = OperandResolver.getSingleValue(args[ix], srcRowIndex, srcColumnIndex);
- if (result == MissingArgEval.instance) {
- return BlankEval.instance;
- }
- return result;
- } catch (EvaluationException e) {
- return e.getErrorEval();
- }
- }
+ try {
+ int ix = evaluateFirstArg(args[0], srcRowIndex, srcColumnIndex);
+ if (ix < 1 || ix >= args.length) {
+ return ErrorEval.VALUE_INVALID;
+ }
+ ValueEval result = OperandResolver.getSingleValue(args[ix], srcRowIndex, srcColumnIndex);
+ if (result == MissingArgEval.instance) {
+ return BlankEval.instance;
+ }
+ return result;
+ } catch (EvaluationException e) {
+ return e.getErrorEval();
+ }
+ }
- public static int evaluateFirstArg(ValueEval arg0, int srcRowIndex, int srcColumnIndex)
- throws EvaluationException {
- ValueEval ev = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex);
- return OperandResolver.coerceValueToInt(ev);
- }
+ public static int evaluateFirstArg(ValueEval arg0, int srcRowIndex, int srcColumnIndex)
+ throws EvaluationException {
+ ValueEval ev = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex);
+ return OperandResolver.coerceValueToInt(ev);
+ }
}
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Columns.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Columns.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Columns.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Columns.java Sat May 22 20:56:44 2021
@@ -28,16 +28,16 @@ import org.apache.poi.ss.formula.TwoDEva
*/
public final class Columns extends Fixed1ArgFunction {
- public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) {
+ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) {
- int result;
- if (arg0 instanceof TwoDEval) {
- result = ((TwoDEval) arg0).getWidth();
- } else if (arg0 instanceof RefEval) {
- result = 1;
- } else { // anything else is not valid argument
- return ErrorEval.VALUE_INVALID;
- }
- return new NumberEval(result);
- }
+ int result;
+ if (arg0 instanceof TwoDEval) {
+ result = ((TwoDEval) arg0).getWidth();
+ } else if (arg0 instanceof RefEval) {
+ result = 1;
+ } else { // anything else is not valid argument
+ return ErrorEval.VALUE_INVALID;
+ }
+ return new NumberEval(result);
+ }
}
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Count.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Count.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Count.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Count.java Sat May 22 20:56:44 2021
@@ -47,44 +47,44 @@ public final class Count implements Func
_predicate = criteriaPredicate;
}
- public ValueEval evaluate(ValueEval[] args, int srcCellRow, int srcCellCol) {
- int nArgs = args.length;
- if (nArgs < 1) {
- // too few arguments
- return ErrorEval.VALUE_INVALID;
- }
-
- if (nArgs > 30) {
- // too many arguments
- return ErrorEval.VALUE_INVALID;
- }
-
- int temp = 0;
-
- for(int i=0; i<nArgs; i++) {
- temp += CountUtils.countArg(args[i], _predicate);
-
- }
- return new NumberEval(temp);
- }
-
- private static final I_MatchPredicate defaultPredicate = new I_MatchPredicate() {
-
- public boolean matches(ValueEval valueEval) {
-
- if(valueEval instanceof NumberEval) {
- // only numbers are counted
- return true;
- }
- if(valueEval == MissingArgEval.instance) {
- // oh yeah, and missing arguments
- return true;
- }
-
- // error values and string values not counted
- return false;
- }
- };
+ public ValueEval evaluate(ValueEval[] args, int srcCellRow, int srcCellCol) {
+ int nArgs = args.length;
+ if (nArgs < 1) {
+ // too few arguments
+ return ErrorEval.VALUE_INVALID;
+ }
+
+ if (nArgs > 30) {
+ // too many arguments
+ return ErrorEval.VALUE_INVALID;
+ }
+
+ int temp = 0;
+
+ for(int i=0; i<nArgs; i++) {
+ temp += CountUtils.countArg(args[i], _predicate);
+
+ }
+ return new NumberEval(temp);
+ }
+
+ private static final I_MatchPredicate defaultPredicate = new I_MatchPredicate() {
+
+ public boolean matches(ValueEval valueEval) {
+
+ if(valueEval instanceof NumberEval) {
+ // only numbers are counted
+ return true;
+ }
+ if(valueEval == MissingArgEval.instance) {
+ // oh yeah, and missing arguments
+ return true;
+ }
+
+ // error values and string values not counted
+ return false;
+ }
+ };
/**
* matches hidden rows but not subtotals
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/CountUtils.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/CountUtils.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/CountUtils.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/CountUtils.java Sat May 22 20:56:44 2021
@@ -27,16 +27,16 @@ import org.apache.poi.ss.formula.eval.Va
*/
final class CountUtils {
- private CountUtils() {
- // no instances of this class
- }
-
- /**
- * Common interface for the matching criteria.
- */
- public interface I_MatchPredicate {
- boolean matches(ValueEval x);
- }
+ private CountUtils() {
+ // no instances of this class
+ }
+
+ /**
+ * Common interface for the matching criteria.
+ */
+ public interface I_MatchPredicate {
+ boolean matches(ValueEval x);
+ }
public interface I_MatchAreaPredicate extends I_MatchPredicate {
boolean matches(TwoDEval x, int rowIndex, int columnIndex);
}
@@ -69,35 +69,35 @@ final class CountUtils {
}
return result;
}
- /**
+ /**
* @return the number of evaluated cells in the range that match the specified criteria
- */
- public static int countMatchingCellsInRef(RefEval refEval, I_MatchPredicate criteriaPredicate) {
- int result = 0;
+ */
+ public static int countMatchingCellsInRef(RefEval refEval, I_MatchPredicate criteriaPredicate) {
+ int result = 0;
final int firstSheetIndex = refEval.getFirstSheetIndex();
final int lastSheetIndex = refEval.getLastSheetIndex();
for (int sIx = firstSheetIndex; sIx <= lastSheetIndex; sIx++) {
- ValueEval ve = refEval.getInnerValueEval(sIx);
+ ValueEval ve = refEval.getInnerValueEval(sIx);
if(criteriaPredicate.matches(ve)) {
result++;
}
- }
- return result;
- }
- public static int countArg(ValueEval eval, I_MatchPredicate criteriaPredicate) {
- if (eval == null) {
- throw new IllegalArgumentException("eval must not be null");
- }
+ }
+ return result;
+ }
+ public static int countArg(ValueEval eval, I_MatchPredicate criteriaPredicate) {
+ if (eval == null) {
+ throw new IllegalArgumentException("eval must not be null");
+ }
if (eval instanceof ThreeDEval) {
return countMatchingCellsInArea((ThreeDEval) eval, criteriaPredicate);
}
- if (eval instanceof TwoDEval) {
- throw new IllegalArgumentException("Count requires 3D Evals, 2D ones aren't supported");
- }
- if (eval instanceof RefEval) {
- return CountUtils.countMatchingCellsInRef((RefEval) eval, criteriaPredicate);
- }
- return criteriaPredicate.matches(eval) ? 1 : 0;
- }
+ if (eval instanceof TwoDEval) {
+ throw new IllegalArgumentException("Count requires 3D Evals, 2D ones aren't supported");
+ }
+ if (eval instanceof RefEval) {
+ return CountUtils.countMatchingCellsInRef((RefEval) eval, criteriaPredicate);
+ }
+ return criteriaPredicate.matches(eval) ? 1 : 0;
+ }
}
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Counta.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Counta.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Counta.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Counta.java Sat May 22 20:56:44 2021
@@ -43,43 +43,43 @@ public final class Counta implements Fun
_predicate = criteriaPredicate;
}
- public ValueEval evaluate(ValueEval[] args, int srcCellRow, int srcCellCol) {
- int nArgs = args.length;
- if (nArgs < 1) {
- // too few arguments
- return ErrorEval.VALUE_INVALID;
- }
-
- if (nArgs > 30) {
- // too many arguments
- return ErrorEval.VALUE_INVALID;
- }
-
- int temp = 0;
-
- for(int i=0; i<nArgs; i++) {
- temp += CountUtils.countArg(args[i], _predicate);
-
- }
- return new NumberEval(temp);
- }
-
- private static final I_MatchPredicate defaultPredicate = new I_MatchPredicate() {
-
- public boolean matches(ValueEval valueEval) {
- // Note - observed behavior of Excel:
- // Error values like #VALUE!, #REF!, #DIV/0!, #NAME? etc don't cause this COUNTA to return an error
- // in fact, they seem to get counted
-
- if(valueEval == BlankEval.instance) {
- return false;
- }
- // Note - everything but BlankEval counts
- return true;
- }
- };
+ public ValueEval evaluate(ValueEval[] args, int srcCellRow, int srcCellCol) {
+ int nArgs = args.length;
+ if (nArgs < 1) {
+ // too few arguments
+ return ErrorEval.VALUE_INVALID;
+ }
+
+ if (nArgs > 30) {
+ // too many arguments
+ return ErrorEval.VALUE_INVALID;
+ }
+
+ int temp = 0;
+
+ for(int i=0; i<nArgs; i++) {
+ temp += CountUtils.countArg(args[i], _predicate);
+
+ }
+ return new NumberEval(temp);
+ }
+
+ private static final I_MatchPredicate defaultPredicate = new I_MatchPredicate() {
+
+ public boolean matches(ValueEval valueEval) {
+ // Note - observed behavior of Excel:
+ // Error values like #VALUE!, #REF!, #DIV/0!, #NAME? etc don't cause this COUNTA to return an error
+ // in fact, they seem to get counted
+
+ if(valueEval == BlankEval.instance) {
+ return false;
+ }
+ // Note - everything but BlankEval counts
+ return true;
+ }
+ };
- private static final I_MatchPredicate subtotalPredicate = new I_MatchAreaPredicate() {
+ private static final I_MatchPredicate subtotalPredicate = new I_MatchAreaPredicate() {
public boolean matches(ValueEval valueEval) {
return defaultPredicate.matches(valueEval);
}
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Countblank.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Countblank.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Countblank.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Countblank.java Sat May 22 20:56:44 2021
@@ -32,25 +32,25 @@ import org.apache.poi.ss.formula.functio
*/
public final class Countblank extends Fixed1ArgFunction {
- @Override
- public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) {
+ @Override
+ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) {
- double result;
- if (arg0 instanceof RefEval) {
- result = CountUtils.countMatchingCellsInRef((RefEval) arg0, predicate);
- } else if (arg0 instanceof ThreeDEval) {
- result = CountUtils.countMatchingCellsInArea((ThreeDEval) arg0, predicate);
- } else {
- throw new IllegalArgumentException("Bad range arg type (" + arg0.getClass().getName() + ")");
- }
- return new NumberEval(result);
- }
+ double result;
+ if (arg0 instanceof RefEval) {
+ result = CountUtils.countMatchingCellsInRef((RefEval) arg0, predicate);
+ } else if (arg0 instanceof ThreeDEval) {
+ result = CountUtils.countMatchingCellsInArea((ThreeDEval) arg0, predicate);
+ } else {
+ throw new IllegalArgumentException("Bad range arg type (" + arg0.getClass().getName() + ")");
+ }
+ return new NumberEval(result);
+ }
- private static final I_MatchPredicate predicate = valueEval -> {
- // Note - only BlankEval counts
- return valueEval == BlankEval.instance ||
- // see https://support.office.com/en-us/article/COUNTBLANK-function-6a92d772-675c-4bee-b346-24af6bd3ac22
- // "Cells with formulas that return "" (empty text) are also counted."
- (valueEval instanceof StringEval && ((StringEval)valueEval).getStringValue().isEmpty());
- };
+ private static final I_MatchPredicate predicate = valueEval -> {
+ // Note - only BlankEval counts
+ return valueEval == BlankEval.instance ||
+ // see https://support.office.com/en-us/article/COUNTBLANK-function-6a92d772-675c-4bee-b346-24af6bd3ac22
+ // "Cells with formulas that return "" (empty text) are also counted."
+ (valueEval instanceof StringEval && ((StringEval)valueEval).getStringValue().isEmpty());
+ };
}
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/DateFunc.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/DateFunc.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/DateFunc.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/DateFunc.java Sat May 22 20:56:44 2021
@@ -31,82 +31,82 @@ import org.apache.poi.util.LocaleUtil;
* Implementation for the Excel function DATE
*/
public final class DateFunc extends Fixed3ArgFunction {
- public static final Function instance = new DateFunc();
+ public static final Function instance = new DateFunc();
- private DateFunc() {
- // no fields to initialise
- }
- public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1,
- ValueEval arg2) {
- double result;
- try {
- double d0 = NumericFunction.singleOperandEvaluate(arg0, srcRowIndex, srcColumnIndex);
- double d1 = NumericFunction.singleOperandEvaluate(arg1, srcRowIndex, srcColumnIndex);
- double d2 = NumericFunction.singleOperandEvaluate(arg2, srcRowIndex, srcColumnIndex);
- result = evaluate(getYear(d0), (int) (d1 - 1), (int) d2);
- NumericFunction.checkValue(result);
- } catch (EvaluationException e) {
- return e.getErrorEval();
- }
- return new NumberEval(result);
- }
-
- /**
- * Note - works with Java Calendar months, not Excel months
- */
- private static double evaluate(int year, int month, int pDay) throws EvaluationException {
- // We don't support negative years yet
- if (year < 0) {
- throw new EvaluationException(ErrorEval.VALUE_INVALID);
- }
- // Negative months are fairly easy
- while (month < 0) {
- year--;
- month += 12;
- }
- // Negative days are handled by the Java Calendar
-
- // Excel has bugs around leap years in 1900, handle them
- // Special case for the non-existant 1900 leap year
- if (year == 1900 && month == Calendar.FEBRUARY && pDay == 29) {
- return 60.0;
- }
-
- // If they give a date in 1900 in Jan/Feb, with the days
- // putting it past the leap year, adjust
- int day = pDay;
- if (year == 1900) {
- if ((month == Calendar.JANUARY && day >= 60) ||
- (month == Calendar.FEBRUARY && day >= 30)) {
- day--;
- }
- }
-
- // Turn this into a Java date
- Calendar c = LocaleUtil.getLocaleCalendar(year, month, day);
-
- // Handle negative days of the week, that pull us across
- // the 29th of Feb 1900
- if (pDay < 0 && c.get(Calendar.YEAR) == 1900 &&
- month > Calendar.FEBRUARY &&
- c.get(Calendar.MONTH) < Calendar.MARCH) {
- c.add(Calendar.DATE, 1);
- }
-
- // TODO Identify if we're doing 1900 or 1904 date windowing
- boolean use1904windowing = false;
-
- // Have this Java date turned back into an Excel one
- return DateUtil.getExcelDate(c.getTime(), use1904windowing);
- }
-
- private static int getYear(double d) {
- int year = (int)d;
-
- if (year < 0) {
- return -1;
- }
+ private DateFunc() {
+ // no fields to initialise
+ }
+ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1,
+ ValueEval arg2) {
+ double result;
+ try {
+ double d0 = NumericFunction.singleOperandEvaluate(arg0, srcRowIndex, srcColumnIndex);
+ double d1 = NumericFunction.singleOperandEvaluate(arg1, srcRowIndex, srcColumnIndex);
+ double d2 = NumericFunction.singleOperandEvaluate(arg2, srcRowIndex, srcColumnIndex);
+ result = evaluate(getYear(d0), (int) (d1 - 1), (int) d2);
+ NumericFunction.checkValue(result);
+ } catch (EvaluationException e) {
+ return e.getErrorEval();
+ }
+ return new NumberEval(result);
+ }
+
+ /**
+ * Note - works with Java Calendar months, not Excel months
+ */
+ private static double evaluate(int year, int month, int pDay) throws EvaluationException {
+ // We don't support negative years yet
+ if (year < 0) {
+ throw new EvaluationException(ErrorEval.VALUE_INVALID);
+ }
+ // Negative months are fairly easy
+ while (month < 0) {
+ year--;
+ month += 12;
+ }
+ // Negative days are handled by the Java Calendar
+
+ // Excel has bugs around leap years in 1900, handle them
+ // Special case for the non-existant 1900 leap year
+ if (year == 1900 && month == Calendar.FEBRUARY && pDay == 29) {
+ return 60.0;
+ }
+
+ // If they give a date in 1900 in Jan/Feb, with the days
+ // putting it past the leap year, adjust
+ int day = pDay;
+ if (year == 1900) {
+ if ((month == Calendar.JANUARY && day >= 60) ||
+ (month == Calendar.FEBRUARY && day >= 30)) {
+ day--;
+ }
+ }
+
+ // Turn this into a Java date
+ Calendar c = LocaleUtil.getLocaleCalendar(year, month, day);
+
+ // Handle negative days of the week, that pull us across
+ // the 29th of Feb 1900
+ if (pDay < 0 && c.get(Calendar.YEAR) == 1900 &&
+ month > Calendar.FEBRUARY &&
+ c.get(Calendar.MONTH) < Calendar.MARCH) {
+ c.add(Calendar.DATE, 1);
+ }
+
+ // TODO Identify if we're doing 1900 or 1904 date windowing
+ boolean use1904windowing = false;
+
+ // Have this Java date turned back into an Excel one
+ return DateUtil.getExcelDate(c.getTime(), use1904windowing);
+ }
+
+ private static int getYear(double d) {
+ int year = (int)d;
+
+ if (year < 0) {
+ return -1;
+ }
- return year < 1900 ? 1900 + year : year;
- }
+ return year < 1900 ? 1900 + year : year;
+ }
}
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Errortype.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Errortype.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Errortype.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Errortype.java Sat May 22 20:56:44 2021
@@ -49,30 +49,30 @@ import org.apache.poi.ss.usermodel.Formu
*/
public final class Errortype extends Fixed1ArgFunction {
- @Override
- public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) {
+ @Override
+ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) {
- try {
- OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex);
- return ErrorEval.NA;
- } catch (EvaluationException e) {
- int result = translateErrorCodeToErrorTypeValue(e.getErrorEval().getErrorCode());
- return new NumberEval(result);
- }
- }
+ try {
+ OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex);
+ return ErrorEval.NA;
+ } catch (EvaluationException e) {
+ int result = translateErrorCodeToErrorTypeValue(e.getErrorEval().getErrorCode());
+ return new NumberEval(result);
+ }
+ }
- private int translateErrorCodeToErrorTypeValue(int errorCode) {
- switch (FormulaError.forInt(errorCode)) {
- case NULL: return 1;
- case DIV0: return 2;
- case VALUE: return 3;
- case REF: return 4;
- case NAME: return 5;
- case NUM: return 6;
- case NA: return 7;
- default:
- throw new IllegalArgumentException("Invalid error code (" + errorCode + ")");
- }
- }
+ private int translateErrorCodeToErrorTypeValue(int errorCode) {
+ switch (FormulaError.forInt(errorCode)) {
+ case NULL: return 1;
+ case DIV0: return 2;
+ case VALUE: return 3;
+ case REF: return 4;
+ case NAME: return 5;
+ case NUM: return 6;
+ case NA: return 7;
+ default:
+ throw new IllegalArgumentException("Invalid error code (" + errorCode + ")");
+ }
+ }
}
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Finance.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Finance.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Finance.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Finance.java Sat May 22 20:56:44 2021
@@ -22,7 +22,7 @@ package org.apache.poi.ss.formula.functi
*/
public class Finance {
- /**
+ /**
* Emulates Excel/Calc's PMT(interest_rate, number_payments, PV, FV, Type)
* function, which calculates the payments for a loan or the future value of an investment
*
@@ -38,32 +38,32 @@ public class Finance {
* - when payment is made: beginning of period is 1; end, 0.
* @return <code>double</code> representing periodic payment amount.
*/
- // http://arachnoid.com/lutusp/finance.html
- static public double pmt(double r, int nper, double pv, double fv, int type) {
+ // http://arachnoid.com/lutusp/finance.html
+ static public double pmt(double r, int nper, double pv, double fv, int type) {
return -r * (pv * Math.pow(1 + r, nper) + fv) / ((1 + r*type) * (Math.pow(1 + r, nper) - 1));
- }
+ }
- /**
+ /**
* Overloaded pmt() call omitting type, which defaults to 0.
*
* @see #pmt(double, int, double, double, int)
*/
- static public double pmt(double r, int nper, double pv, double fv) {
- return pmt(r, nper, pv, fv, 0);
- }
-
- /**
+ static public double pmt(double r, int nper, double pv, double fv) {
+ return pmt(r, nper, pv, fv, 0);
+ }
+
+ /**
* Overloaded pmt() call omitting fv and type, which both default to 0.
*
* @see #pmt(double, int, double, double, int)
*/
- static public double pmt(double r, int nper, double pv) {
- return pmt(r, nper, pv, 0);
- }
-
-
- /**
+ static public double pmt(double r, int nper, double pv) {
+ return pmt(r, nper, pv, 0);
+ }
+
+
+ /**
* Emulates Excel/Calc's IPMT(interest_rate, period, number_payments, PV,
* FV, Type) function, which calculates the portion of the payment at a
* given period that is the interest on previous balance.
@@ -85,22 +85,22 @@ public class Finance {
* @see #pmt(double, int, double, double, int)
* @see #fv(double, int, double, double, int)
*/
- // http://doc.optadata.com/en/dokumentation/application/expression/functions/financial.html
- static public double ipmt(double r, int per, int nper, double pv, double fv, int type) {
- double ipmt = fv(r, per - 1, pmt(r, nper, pv, fv, type), pv, type) * r;
- if (type==1) ipmt /= (1 + r);
- return ipmt;
- }
-
- static public double ipmt(double r, int per, int nper, double pv, double fv) {
- return ipmt(r, per, nper, pv, fv, 0);
- }
-
- static public double ipmt(double r, int per, int nper, double pv) {
- return ipmt(r, per, nper, pv, 0);
- }
-
- /**
+ // http://doc.optadata.com/en/dokumentation/application/expression/functions/financial.html
+ static public double ipmt(double r, int per, int nper, double pv, double fv, int type) {
+ double ipmt = fv(r, per - 1, pmt(r, nper, pv, fv, type), pv, type) * r;
+ if (type==1) ipmt /= (1 + r);
+ return ipmt;
+ }
+
+ static public double ipmt(double r, int per, int nper, double pv, double fv) {
+ return ipmt(r, per, nper, pv, fv, 0);
+ }
+
+ static public double ipmt(double r, int per, int nper, double pv) {
+ return ipmt(r, per, nper, pv, 0);
+ }
+
+ /**
* Emulates Excel/Calc's PPMT(interest_rate, period, number_payments, PV,
* FV, Type) function, which calculates the portion of the payment at a
* given period that will apply to principal.
@@ -122,18 +122,18 @@ public class Finance {
* @see #pmt(double, int, double, double, int)
* @see #ipmt(double, int, int, double, double, int)
*/
- static public double ppmt(double r, int per, int nper, double pv, double fv, int type) {
- return pmt(r, nper, pv, fv, type) - ipmt(r, per, nper, pv, fv, type);
- }
-
- static public double ppmt(double r, int per, int nper, double pv, double fv) {
- return pmt(r, nper, pv, fv) - ipmt(r, per, nper, pv, fv);
- }
-
- static public double ppmt(double r, int per, int nper, double pv) {
- return pmt(r, nper, pv) - ipmt(r, per, nper, pv);
- }
-
+ static public double ppmt(double r, int per, int nper, double pv, double fv, int type) {
+ return pmt(r, nper, pv, fv, type) - ipmt(r, per, nper, pv, fv, type);
+ }
+
+ static public double ppmt(double r, int per, int nper, double pv, double fv) {
+ return pmt(r, nper, pv, fv) - ipmt(r, per, nper, pv, fv);
+ }
+
+ static public double ppmt(double r, int per, int nper, double pv) {
+ return pmt(r, nper, pv) - ipmt(r, per, nper, pv);
+ }
+
/**
* Emulates Excel/Calc's FV(interest_rate, number_payments, payment, PV,
* Type) function, which calculates future value or principal at period N.
@@ -150,18 +150,18 @@ public class Finance {
* - when payment is made: beginning of period is 1; end, 0.
* @return <code>double</code> representing future principal value.
*/
- //http://en.wikipedia.org/wiki/Future_value
- static public double fv(double r, int nper, double pmt, double pv, int type) {
+ //http://en.wikipedia.org/wiki/Future_value
+ static public double fv(double r, int nper, double pmt, double pv, int type) {
return -(pv * Math.pow(1 + r, nper) + pmt * (1+r*type) * (Math.pow(1 + r, nper) - 1) / r);
- }
-
- /**
+ }
+
+ /**
* Overloaded fv() call omitting type, which defaults to 0.
*
* @see #fv(double, int, double, double, int)
*/
- static public double fv(double r, int nper, double c, double pv) {
- return fv(r, nper, c, pv, 0);
- }
+ static public double fv(double r, int nper, double c, double pv) {
+ return fv(r, nper, c, pv, 0);
+ }
}
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Fixed0ArgFunction.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Fixed0ArgFunction.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Fixed0ArgFunction.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Fixed0ArgFunction.java Sat May 22 20:56:44 2021
@@ -29,10 +29,10 @@ import org.apache.poi.util.Removal;
@Deprecated
@Removal(version = "6.0.0")
public abstract class Fixed0ArgFunction implements Function0Arg {
- public final ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
- if (args.length != 0) {
- return ErrorEval.VALUE_INVALID;
- }
- return evaluate(srcRowIndex, srcColumnIndex);
- }
+ public final ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
+ if (args.length != 0) {
+ return ErrorEval.VALUE_INVALID;
+ }
+ return evaluate(srcRowIndex, srcColumnIndex);
+ }
}
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Fixed1ArgFunction.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Fixed1ArgFunction.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Fixed1ArgFunction.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Fixed1ArgFunction.java Sat May 22 20:56:44 2021
@@ -24,10 +24,10 @@ import org.apache.poi.ss.formula.eval.Va
* Convenience base class for functions that must take exactly one argument.
*/
public abstract class Fixed1ArgFunction implements Function1Arg {
- public final ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
- if (args.length != 1) {
- return ErrorEval.VALUE_INVALID;
- }
- return evaluate(srcRowIndex, srcColumnIndex, args[0]);
- }
+ public final ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
+ if (args.length != 1) {
+ return ErrorEval.VALUE_INVALID;
+ }
+ return evaluate(srcRowIndex, srcColumnIndex, args[0]);
+ }
}
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Fixed2ArgFunction.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Fixed2ArgFunction.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Fixed2ArgFunction.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Fixed2ArgFunction.java Sat May 22 20:56:44 2021
@@ -24,10 +24,10 @@ import org.apache.poi.ss.formula.eval.Va
* Convenience base class for functions that must take exactly two arguments.
*/
public abstract class Fixed2ArgFunction implements Function2Arg {
- public final ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
- if (args.length != 2) {
- return ErrorEval.VALUE_INVALID;
- }
- return evaluate(srcRowIndex, srcColumnIndex, args[0], args[1]);
- }
+ public final ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
+ if (args.length != 2) {
+ return ErrorEval.VALUE_INVALID;
+ }
+ return evaluate(srcRowIndex, srcColumnIndex, args[0], args[1]);
+ }
}
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Fixed3ArgFunction.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Fixed3ArgFunction.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Fixed3ArgFunction.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Fixed3ArgFunction.java Sat May 22 20:56:44 2021
@@ -24,10 +24,10 @@ import org.apache.poi.ss.formula.eval.Va
* Convenience base class for functions that must take exactly three arguments.
*/
public abstract class Fixed3ArgFunction implements Function3Arg {
- public final ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
- if (args.length != 3) {
- return ErrorEval.VALUE_INVALID;
- }
- return evaluate(srcRowIndex, srcColumnIndex, args[0], args[1], args[2]);
- }
+ public final ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
+ if (args.length != 3) {
+ return ErrorEval.VALUE_INVALID;
+ }
+ return evaluate(srcRowIndex, srcColumnIndex, args[0], args[1], args[2]);
+ }
}
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Fixed4ArgFunction.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Fixed4ArgFunction.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Fixed4ArgFunction.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Fixed4ArgFunction.java Sat May 22 20:56:44 2021
@@ -24,10 +24,10 @@ import org.apache.poi.ss.formula.eval.Va
* Convenience base class for functions that must take exactly four arguments.
*/
public abstract class Fixed4ArgFunction implements Function4Arg {
- public final ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
- if (args.length != 4) {
- return ErrorEval.VALUE_INVALID;
- }
- return evaluate(srcRowIndex, srcColumnIndex, args[0], args[1], args[2], args[3]);
- }
+ public final ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
+ if (args.length != 4) {
+ return ErrorEval.VALUE_INVALID;
+ }
+ return evaluate(srcRowIndex, srcColumnIndex, args[0], args[1], args[2], args[3]);
+ }
}
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/FreeRefFunction.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/FreeRefFunction.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/FreeRefFunction.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/FreeRefFunction.java Sat May 22 20:56:44 2021
@@ -37,13 +37,13 @@ import org.apache.poi.ss.formula.Operati
* arbitrary text into cell references, and evaluate them..
*/
public interface FreeRefFunction {
- /**
- * @param args the pre-evaluated arguments for this function. args is never {@code null},
- * nor are any of its elements.
- * @param ec primarily used to identify the source cell containing the formula being evaluated.
- * may also be used to dynamically create reference evals.
- * @return never {@code null}. Possibly an instance of {@code ErrorEval} in the case of
- * a specified Excel error (Exceptions are never thrown to represent Excel errors).
- */
- ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec);
+ /**
+ * @param args the pre-evaluated arguments for this function. args is never {@code null},
+ * nor are any of its elements.
+ * @param ec primarily used to identify the source cell containing the formula being evaluated.
+ * may also be used to dynamically create reference evals.
+ * @return never {@code null}. Possibly an instance of {@code ErrorEval} in the case of
+ * a specified Excel error (Exceptions are never thrown to represent Excel errors).
+ */
+ ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec);
}
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Function.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Function.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Function.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Function.java Sat May 22 20:56:44 2021
@@ -27,15 +27,15 @@ import org.apache.poi.ss.formula.eval.Va
*/
public interface Function {
- /**
- * @param args the evaluated function arguments. Empty values are represented with
- * {@link BlankEval} or {@link MissingArgEval}, never <code>null</code>.
- * @param srcRowIndex row index of the cell containing the formula under evaluation
- * @param srcColumnIndex column index of the cell containing the formula under evaluation
- * @return The evaluated result, possibly an {@link ErrorEval}, never <code>null</code>.
- * <b>Note</b> - Excel uses the error code <i>#NUM!</i> instead of IEEE <i>NaN</i>, so when
- * numeric functions evaluate to {@link Double#NaN} be sure to translate the result to {@link
- * ErrorEval#NUM_ERROR}.
- */
- ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex);
+ /**
+ * @param args the evaluated function arguments. Empty values are represented with
+ * {@link BlankEval} or {@link MissingArgEval}, never <code>null</code>.
+ * @param srcRowIndex row index of the cell containing the formula under evaluation
+ * @param srcColumnIndex column index of the cell containing the formula under evaluation
+ * @return The evaluated result, possibly an {@link ErrorEval}, never <code>null</code>.
+ * <b>Note</b> - Excel uses the error code <i>#NUM!</i> instead of IEEE <i>NaN</i>, so when
+ * numeric functions evaluate to {@link Double#NaN} be sure to translate the result to {@link
+ * ErrorEval#NUM_ERROR}.
+ */
+ ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex);
}
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Function0Arg.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Function0Arg.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Function0Arg.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Function0Arg.java Sat May 22 20:56:44 2021
@@ -28,8 +28,8 @@ import org.apache.poi.util.Removal;
@Deprecated
@Removal(version = "6.0.0")
public interface Function0Arg extends Function {
- /**
- * see {@link Function#evaluate(ValueEval[], int, int)}
- */
- ValueEval evaluate(int srcRowIndex, int srcColumnIndex);
+ /**
+ * see {@link Function#evaluate(ValueEval[], int, int)}
+ */
+ ValueEval evaluate(int srcRowIndex, int srcColumnIndex);
}
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Function1Arg.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Function1Arg.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Function1Arg.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Function1Arg.java Sat May 22 20:56:44 2021
@@ -23,8 +23,8 @@ import org.apache.poi.ss.formula.eval.Va
* Implemented by all functions that can be called with one argument
*/
public interface Function1Arg extends Function {
- /**
- * see {@link Function#evaluate(ValueEval[], int, int)}
- */
- ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0);
+ /**
+ * see {@link Function#evaluate(ValueEval[], int, int)}
+ */
+ ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0);
}
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Function2Arg.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Function2Arg.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Function2Arg.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Function2Arg.java Sat May 22 20:56:44 2021
@@ -23,8 +23,8 @@ import org.apache.poi.ss.formula.eval.Va
* Implemented by all functions that can be called with two arguments
*/
public interface Function2Arg extends Function {
- /**
- * see {@link Function#evaluate(ValueEval[], int, int)}
- */
- ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1);
+ /**
+ * see {@link Function#evaluate(ValueEval[], int, int)}
+ */
+ ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1);
}
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Function3Arg.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Function3Arg.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Function3Arg.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Function3Arg.java Sat May 22 20:56:44 2021
@@ -23,8 +23,8 @@ import org.apache.poi.ss.formula.eval.Va
* Implemented by all functions that can be called with three arguments
*/
public interface Function3Arg extends Function {
- /**
- * see {@link Function#evaluate(ValueEval[], int, int)}
- */
- ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1, ValueEval arg2);
+ /**
+ * see {@link Function#evaluate(ValueEval[], int, int)}
+ */
+ ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1, ValueEval arg2);
}
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Function4Arg.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Function4Arg.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Function4Arg.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Function4Arg.java Sat May 22 20:56:44 2021
@@ -23,8 +23,8 @@ import org.apache.poi.ss.formula.eval.Va
* Implemented by all functions that can be called with four arguments
*/
public interface Function4Arg extends Function {
- /**
- * see {@link Function#evaluate(ValueEval[], int, int)}
- */
- ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1, ValueEval arg2, ValueEval arg3);
+ /**
+ * see {@link Function#evaluate(ValueEval[], int, int)}
+ */
+ ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1, ValueEval arg2, ValueEval arg3);
}
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Hlookup.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Hlookup.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Hlookup.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Hlookup.java Sat May 22 20:56:44 2021
@@ -38,43 +38,43 @@ import org.apache.poi.ss.formula.TwoDEva
* the lookup_value. If FALSE, only exact matches will be considered<br>
*/
public final class Hlookup extends Var3or4ArgFunction {
- private static final ValueEval DEFAULT_ARG3 = BoolEval.TRUE;
+ private static final ValueEval DEFAULT_ARG3 = BoolEval.TRUE;
- @Override
- public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1,
- ValueEval arg2) {
- return evaluate(srcRowIndex, srcColumnIndex, arg0, arg1, arg2, DEFAULT_ARG3);
- }
+ @Override
+ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1,
+ ValueEval arg2) {
+ return evaluate(srcRowIndex, srcColumnIndex, arg0, arg1, arg2, DEFAULT_ARG3);
+ }
- @Override
- public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1,
- ValueEval arg2, ValueEval arg3) {
- try {
- // Evaluation order:
- // arg0 lookup_value, arg1 table_array, arg3 range_lookup, find lookup value, arg2 row_index, fetch result
- ValueEval lookupValue = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex);
- TwoDEval tableArray = LookupUtils.resolveTableArrayArg(arg1);
- boolean isRangeLookup = LookupUtils.resolveRangeLookupArg(arg3, srcRowIndex, srcColumnIndex);
- int colIndex = LookupUtils.lookupIndexOfValue(lookupValue, LookupUtils.createRowVector(tableArray, 0), isRangeLookup);
- int rowIndex = LookupUtils.resolveRowOrColIndexArg(arg2, srcRowIndex, srcColumnIndex);
- ValueVector resultCol = createResultColumnVector(tableArray, rowIndex);
- return resultCol.getItem(colIndex);
- } catch (EvaluationException e) {
- return e.getErrorEval();
- }
- }
+ @Override
+ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1,
+ ValueEval arg2, ValueEval arg3) {
+ try {
+ // Evaluation order:
+ // arg0 lookup_value, arg1 table_array, arg3 range_lookup, find lookup value, arg2 row_index, fetch result
+ ValueEval lookupValue = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex);
+ TwoDEval tableArray = LookupUtils.resolveTableArrayArg(arg1);
+ boolean isRangeLookup = LookupUtils.resolveRangeLookupArg(arg3, srcRowIndex, srcColumnIndex);
+ int colIndex = LookupUtils.lookupIndexOfValue(lookupValue, LookupUtils.createRowVector(tableArray, 0), isRangeLookup);
+ int rowIndex = LookupUtils.resolveRowOrColIndexArg(arg2, srcRowIndex, srcColumnIndex);
+ ValueVector resultCol = createResultColumnVector(tableArray, rowIndex);
+ return resultCol.getItem(colIndex);
+ } catch (EvaluationException e) {
+ return e.getErrorEval();
+ }
+ }
- /**
- * Returns one column from an {@code AreaEval}
- *
- * @param rowIndex assumed to be non-negative
- *
- * @throws EvaluationException (#REF!) if colIndex is too high
- */
- private ValueVector createResultColumnVector(TwoDEval tableArray, int rowIndex) throws EvaluationException {
- if(rowIndex >= tableArray.getHeight()) {
- throw EvaluationException.invalidRef();
- }
- return LookupUtils.createRowVector(tableArray, rowIndex);
- }
+ /**
+ * Returns one column from an {@code AreaEval}
+ *
+ * @param rowIndex assumed to be non-negative
+ *
+ * @throws EvaluationException (#REF!) if colIndex is too high
+ */
+ private ValueVector createResultColumnVector(TwoDEval tableArray, int rowIndex) throws EvaluationException {
+ if(rowIndex >= tableArray.getHeight()) {
+ throw EvaluationException.invalidRef();
+ }
+ return LookupUtils.createRowVector(tableArray, rowIndex);
+ }
}
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Hyperlink.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Hyperlink.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Hyperlink.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Hyperlink.java Sat May 22 20:56:44 2021
@@ -35,12 +35,12 @@ import org.apache.poi.ss.formula.eval.Va
*/
public final class Hyperlink extends Var1or2ArgFunction {
- public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) {
- return arg0;
- }
- public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) {
- // note - if last arg is MissingArgEval, result will be NumberEval.ZERO,
- // but WorkbookEvaluator does that translation
- return arg1;
- }
+ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) {
+ return arg0;
+ }
+ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) {
+ // note - if last arg is MissingArgEval, result will be NumberEval.ZERO,
+ // but WorkbookEvaluator does that translation
+ return arg1;
+ }
}
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/IPMT.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/IPMT.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/IPMT.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/IPMT.java Sat May 22 20:56:44 2021
@@ -26,31 +26,31 @@ import org.apache.poi.ss.formula.eval.Va
public class IPMT extends NumericFunction {
- @Override
- public double eval(ValueEval[] args, int srcCellRow, int srcCellCol) throws EvaluationException {
-
- if(args.length != 4)
- throw new EvaluationException(ErrorEval.VALUE_INVALID);
-
- double result;
-
- ValueEval v1 = OperandResolver.getSingleValue(args[0], srcCellRow, srcCellCol);
- ValueEval v2 = OperandResolver.getSingleValue(args[1], srcCellRow, srcCellCol);
- ValueEval v3 = OperandResolver.getSingleValue(args[2], srcCellRow, srcCellCol);
- ValueEval v4 = OperandResolver.getSingleValue(args[3], srcCellRow, srcCellCol);
-
- double interestRate = OperandResolver.coerceValueToDouble(v1);
- int period = OperandResolver.coerceValueToInt(v2);
- int numberPayments = OperandResolver.coerceValueToInt(v3);
- double PV = OperandResolver.coerceValueToDouble(v4);
-
- result = Finance.ipmt(interestRate, period, numberPayments, PV) ;
-
- checkValue(result);
-
- return result;
- }
+ @Override
+ public double eval(ValueEval[] args, int srcCellRow, int srcCellCol) throws EvaluationException {
+
+ if(args.length != 4)
+ throw new EvaluationException(ErrorEval.VALUE_INVALID);
+
+ double result;
+
+ ValueEval v1 = OperandResolver.getSingleValue(args[0], srcCellRow, srcCellCol);
+ ValueEval v2 = OperandResolver.getSingleValue(args[1], srcCellRow, srcCellCol);
+ ValueEval v3 = OperandResolver.getSingleValue(args[2], srcCellRow, srcCellCol);
+ ValueEval v4 = OperandResolver.getSingleValue(args[3], srcCellRow, srcCellCol);
+
+ double interestRate = OperandResolver.coerceValueToDouble(v1);
+ int period = OperandResolver.coerceValueToInt(v2);
+ int numberPayments = OperandResolver.coerceValueToInt(v3);
+ double PV = OperandResolver.coerceValueToDouble(v4);
+
+ result = Finance.ipmt(interestRate, period, numberPayments, PV) ;
+
+ checkValue(result);
+
+ return result;
+ }
-
+
}
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Index.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Index.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Index.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Index.java Sat May 22 20:56:44 2021
@@ -44,129 +44,129 @@ import org.apache.poi.ss.formula.TwoDEva
*/
public final class Index implements Function2Arg, Function3Arg, Function4Arg, ArrayMode {
- @Override
- public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) {
- TwoDEval reference = convertFirstArg(arg0);
-
- int columnIx = 0;
- try {
- int rowIx = resolveIndexArg(arg1, srcRowIndex, srcColumnIndex);
-
- if (!reference.isColumn()) {
- if (!reference.isRow()) {
- // always an error with 2-D area refs
- // Note - the type of error changes if the pRowArg is negative
- return ErrorEval.REF_INVALID;
- }
- // When the two-arg version of INDEX() has been invoked and the reference
- // is a single column ref, the row arg seems to get used as the column index
- columnIx = rowIx;
- rowIx = 0;
- }
-
- return getValueFromArea(reference, rowIx, columnIx);
- } catch (EvaluationException e) {
- return e.getErrorEval();
- }
- }
- @Override
- public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1,
- ValueEval arg2) {
- TwoDEval reference = convertFirstArg(arg0);
-
- try {
- int columnIx = resolveIndexArg(arg2, srcRowIndex, srcColumnIndex);
- int rowIx = resolveIndexArg(arg1, srcRowIndex, srcColumnIndex);
- return getValueFromArea(reference, rowIx, columnIx);
- } catch (EvaluationException e) {
- return e.getErrorEval();
- }
- }
- @Override
- public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1,
- ValueEval arg2, ValueEval arg3) {
- throw new RuntimeException("Incomplete code"
- + " - don't know how to support the 'area_num' parameter yet)");
- // Excel expression might look like this "INDEX( (A1:B4, C3:D6, D2:E5 ), 1, 2, 3)
- // In this example, the 3rd area would be used i.e. D2:E5, and the overall result would be E2
- // Token array might be encoded like this: MemAreaPtg, AreaPtg, AreaPtg, UnionPtg, UnionPtg, ParenthesesPtg
- // The formula parser doesn't seem to support this yet. Not sure if the evaluator does either
- }
+ @Override
+ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) {
+ TwoDEval reference = convertFirstArg(arg0);
+
+ int columnIx = 0;
+ try {
+ int rowIx = resolveIndexArg(arg1, srcRowIndex, srcColumnIndex);
+
+ if (!reference.isColumn()) {
+ if (!reference.isRow()) {
+ // always an error with 2-D area refs
+ // Note - the type of error changes if the pRowArg is negative
+ return ErrorEval.REF_INVALID;
+ }
+ // When the two-arg version of INDEX() has been invoked and the reference
+ // is a single column ref, the row arg seems to get used as the column index
+ columnIx = rowIx;
+ rowIx = 0;
+ }
+
+ return getValueFromArea(reference, rowIx, columnIx);
+ } catch (EvaluationException e) {
+ return e.getErrorEval();
+ }
+ }
+ @Override
+ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1,
+ ValueEval arg2) {
+ TwoDEval reference = convertFirstArg(arg0);
+
+ try {
+ int columnIx = resolveIndexArg(arg2, srcRowIndex, srcColumnIndex);
+ int rowIx = resolveIndexArg(arg1, srcRowIndex, srcColumnIndex);
+ return getValueFromArea(reference, rowIx, columnIx);
+ } catch (EvaluationException e) {
+ return e.getErrorEval();
+ }
+ }
+ @Override
+ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1,
+ ValueEval arg2, ValueEval arg3) {
+ throw new RuntimeException("Incomplete code"
+ + " - don't know how to support the 'area_num' parameter yet)");
+ // Excel expression might look like this "INDEX( (A1:B4, C3:D6, D2:E5 ), 1, 2, 3)
+ // In this example, the 3rd area would be used i.e. D2:E5, and the overall result would be E2
+ // Token array might be encoded like this: MemAreaPtg, AreaPtg, AreaPtg, UnionPtg, UnionPtg, ParenthesesPtg
+ // The formula parser doesn't seem to support this yet. Not sure if the evaluator does either
+ }
- private static TwoDEval convertFirstArg(ValueEval arg0) {
+ private static TwoDEval convertFirstArg(ValueEval arg0) {
if (arg0 instanceof RefEval) {
- // convert to area ref for simpler code in getValueFromArea()
- return ((RefEval) arg0).offset(0, 0, 0, 0);
- }
- if((arg0 instanceof TwoDEval)) {
- return (TwoDEval) arg0;
- }
- // else the other variation of this function takes an array as the first argument
- // it seems like interface 'ArrayEval' does not even exist yet
- throw new RuntimeException("Incomplete code - cannot handle first arg of type ("
- + arg0.getClass().getName() + ")");
-
- }
-
- @Override
- public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
- switch (args.length) {
- case 2:
- return evaluate(srcRowIndex, srcColumnIndex, args[0], args[1]);
- case 3:
- return evaluate(srcRowIndex, srcColumnIndex, args[0], args[1], args[2]);
- case 4:
- return evaluate(srcRowIndex, srcColumnIndex, args[0], args[1], args[2], args[3]);
- }
- return ErrorEval.VALUE_INVALID;
- }
-
- private static ValueEval getValueFromArea(TwoDEval ae, int pRowIx, int pColumnIx)
- throws EvaluationException {
- assert pRowIx >= 0;
- assert pColumnIx >= 0;
-
- TwoDEval result = ae;
-
- if (pRowIx != 0) {
- // Slightly irregular logic for bounds checking errors
- if (pRowIx > ae.getHeight()) {
- // high bounds check fail gives #REF! if arg was explicitly passed
- throw new EvaluationException(ErrorEval.REF_INVALID);
- }
- result = result.getRow(pRowIx-1);
- }
-
- if (pColumnIx != 0) {
- // Slightly irregular logic for bounds checking errors
- if (pColumnIx > ae.getWidth()) {
- // high bounds check fail gives #REF! if arg was explicitly passed
- throw new EvaluationException(ErrorEval.REF_INVALID);
- }
- result = result.getColumn(pColumnIx-1);
- }
- return result;
- }
-
-
- /**
- * @param arg a 1-based index.
- * @return the resolved 1-based index. Zero if the arg was missing or blank
- * @throws EvaluationException if the arg is an error value evaluates to a negative numeric value
- */
- private static int resolveIndexArg(ValueEval arg, int srcCellRow, int srcCellCol) throws EvaluationException {
-
- ValueEval ev = OperandResolver.getSingleValue(arg, srcCellRow, srcCellCol);
- if (ev == MissingArgEval.instance) {
- return 0;
- }
- if (ev == BlankEval.instance) {
- return 0;
- }
- int result = OperandResolver.coerceValueToInt(ev);
- if (result < 0) {
- throw new EvaluationException(ErrorEval.VALUE_INVALID);
- }
- return result;
- }
+ // convert to area ref for simpler code in getValueFromArea()
+ return ((RefEval) arg0).offset(0, 0, 0, 0);
+ }
+ if((arg0 instanceof TwoDEval)) {
+ return (TwoDEval) arg0;
+ }
+ // else the other variation of this function takes an array as the first argument
+ // it seems like interface 'ArrayEval' does not even exist yet
+ throw new RuntimeException("Incomplete code - cannot handle first arg of type ("
+ + arg0.getClass().getName() + ")");
+
+ }
+
+ @Override
+ public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
+ switch (args.length) {
+ case 2:
+ return evaluate(srcRowIndex, srcColumnIndex, args[0], args[1]);
+ case 3:
+ return evaluate(srcRowIndex, srcColumnIndex, args[0], args[1], args[2]);
+ case 4:
+ return evaluate(srcRowIndex, srcColumnIndex, args[0], args[1], args[2], args[3]);
+ }
+ return ErrorEval.VALUE_INVALID;
+ }
+
+ private static ValueEval getValueFromArea(TwoDEval ae, int pRowIx, int pColumnIx)
+ throws EvaluationException {
+ assert pRowIx >= 0;
+ assert pColumnIx >= 0;
+
+ TwoDEval result = ae;
+
+ if (pRowIx != 0) {
+ // Slightly irregular logic for bounds checking errors
+ if (pRowIx > ae.getHeight()) {
+ // high bounds check fail gives #REF! if arg was explicitly passed
+ throw new EvaluationException(ErrorEval.REF_INVALID);
+ }
+ result = result.getRow(pRowIx-1);
+ }
+
+ if (pColumnIx != 0) {
+ // Slightly irregular logic for bounds checking errors
+ if (pColumnIx > ae.getWidth()) {
+ // high bounds check fail gives #REF! if arg was explicitly passed
+ throw new EvaluationException(ErrorEval.REF_INVALID);
+ }
+ result = result.getColumn(pColumnIx-1);
+ }
+ return result;
+ }
+
+
+ /**
+ * @param arg a 1-based index.
+ * @return the resolved 1-based index. Zero if the arg was missing or blank
+ * @throws EvaluationException if the arg is an error value evaluates to a negative numeric value
+ */
+ private static int resolveIndexArg(ValueEval arg, int srcCellRow, int srcCellCol) throws EvaluationException {
+
+ ValueEval ev = OperandResolver.getSingleValue(arg, srcCellRow, srcCellCol);
+ if (ev == MissingArgEval.instance) {
+ return 0;
+ }
+ if (ev == BlankEval.instance) {
+ return 0;
+ }
+ int result = OperandResolver.coerceValueToInt(ev);
+ if (result < 0) {
+ throw new EvaluationException(ErrorEval.VALUE_INVALID);
+ }
+ return result;
+ }
}
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Intercept.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Intercept.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Intercept.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/Intercept.java Sat May 22 20:56:44 2021
@@ -32,15 +32,15 @@ import org.apache.poi.ss.formula.functio
*/
public final class Intercept extends Fixed2ArgFunction {
- private final LinearRegressionFunction func;
- public Intercept() {
- func = new LinearRegressionFunction(FUNCTION.INTERCEPT);
- }
+ private final LinearRegressionFunction func;
+ public Intercept() {
+ func = new LinearRegressionFunction(FUNCTION.INTERCEPT);
+ }
- @Override
- public ValueEval evaluate(int srcRowIndex, int srcColumnIndex,
- ValueEval arg0, ValueEval arg1) {
- return func.evaluate(srcRowIndex, srcColumnIndex, arg0, arg1);
- }
+ @Override
+ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex,
+ ValueEval arg0, ValueEval arg1) {
+ return func.evaluate(srcRowIndex, srcColumnIndex, arg0, arg1);
+ }
}
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/LinearRegressionFunction.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/LinearRegressionFunction.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/LinearRegressionFunction.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/LinearRegressionFunction.java Sat May 22 20:56:44 2021
@@ -39,178 +39,178 @@ import org.apache.poi.ss.formula.functio
*/
public final class LinearRegressionFunction extends Fixed2ArgFunction {
- private static abstract class ValueArray implements ValueVector {
- private final int _size;
- protected ValueArray(int size) {
- _size = size;
- }
+ private static abstract class ValueArray implements ValueVector {
+ private final int _size;
+ protected ValueArray(int size) {
+ _size = size;
+ }
- @Override
+ @Override
public ValueEval getItem(int index) {
- if (index < 0 || index > _size) {
- throw new IllegalArgumentException("Specified index " + index
- + " is outside range (0.." + (_size - 1) + ")");
- }
- return getItemInternal(index);
- }
- protected abstract ValueEval getItemInternal(int index);
-
- public final int getSize() {
- return _size;
- }
- }
-
- private static final class SingleCellValueArray extends ValueArray {
- private final ValueEval _value;
- public SingleCellValueArray(ValueEval value) {
- super(1);
- _value = value;
- }
-
- protected ValueEval getItemInternal(int index) {
- return _value;
- }
- }
-
- private static final class RefValueArray extends ValueArray {
- private final RefEval _ref;
- private final int _width;
- public RefValueArray(RefEval ref) {
- super(ref.getNumberOfSheets());
- _ref = ref;
- _width = ref.getNumberOfSheets();
- }
-
- protected ValueEval getItemInternal(int index) {
- int sIx = (index % _width) + _ref.getFirstSheetIndex();
- return _ref.getInnerValueEval(sIx);
- }
- }
-
- private static final class AreaValueArray extends ValueArray {
- private final TwoDEval _ae;
- private final int _width;
-
- public AreaValueArray(TwoDEval ae) {
- super(ae.getWidth() * ae.getHeight());
- _ae = ae;
- _width = ae.getWidth();
- }
-
- protected ValueEval getItemInternal(int index) {
- int rowIx = index / _width;
- int colIx = index % _width;
- return _ae.getValue(rowIx, colIx);
- }
- }
-
- public enum FUNCTION {INTERCEPT, SLOPE}
- private final FUNCTION function;
-
- public LinearRegressionFunction(FUNCTION function) {
- this.function = function;
- }
-
- public ValueEval evaluate(int srcRowIndex, int srcColumnIndex,
- ValueEval arg0, ValueEval arg1) {
- double result;
- try {
- ValueVector vvY = createValueVector(arg0);
- ValueVector vvX = createValueVector(arg1);
- int size = vvX.getSize();
- if (size == 0 || vvY.getSize() != size) {
- return ErrorEval.NA;
- }
- result = evaluateInternal(vvX, vvY, size);
- } catch (EvaluationException e) {
- return e.getErrorEval();
- }
- if (Double.isNaN(result) || Double.isInfinite(result)) {
- return ErrorEval.NUM_ERROR;
- }
- return new NumberEval(result);
- }
-
- private double evaluateInternal(ValueVector x, ValueVector y, int size)
- throws EvaluationException {
-
- // error handling is as if the x is fully evaluated before y
- ErrorEval firstYerr = null;
- boolean accumlatedSome = false;
+ if (index < 0 || index > _size) {
+ throw new IllegalArgumentException("Specified index " + index
+ + " is outside range (0.." + (_size - 1) + ")");
+ }
+ return getItemInternal(index);
+ }
+ protected abstract ValueEval getItemInternal(int index);
+
+ public final int getSize() {
+ return _size;
+ }
+ }
+
+ private static final class SingleCellValueArray extends ValueArray {
+ private final ValueEval _value;
+ public SingleCellValueArray(ValueEval value) {
+ super(1);
+ _value = value;
+ }
+
+ protected ValueEval getItemInternal(int index) {
+ return _value;
+ }
+ }
+
+ private static final class RefValueArray extends ValueArray {
+ private final RefEval _ref;
+ private final int _width;
+ public RefValueArray(RefEval ref) {
+ super(ref.getNumberOfSheets());
+ _ref = ref;
+ _width = ref.getNumberOfSheets();
+ }
+
+ protected ValueEval getItemInternal(int index) {
+ int sIx = (index % _width) + _ref.getFirstSheetIndex();
+ return _ref.getInnerValueEval(sIx);
+ }
+ }
+
+ private static final class AreaValueArray extends ValueArray {
+ private final TwoDEval _ae;
+ private final int _width;
+
+ public AreaValueArray(TwoDEval ae) {
+ super(ae.getWidth() * ae.getHeight());
+ _ae = ae;
+ _width = ae.getWidth();
+ }
+
+ protected ValueEval getItemInternal(int index) {
+ int rowIx = index / _width;
+ int colIx = index % _width;
+ return _ae.getValue(rowIx, colIx);
+ }
+ }
+
+ public enum FUNCTION {INTERCEPT, SLOPE}
+ private final FUNCTION function;
+
+ public LinearRegressionFunction(FUNCTION function) {
+ this.function = function;
+ }
+
+ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex,
+ ValueEval arg0, ValueEval arg1) {
+ double result;
+ try {
+ ValueVector vvY = createValueVector(arg0);
+ ValueVector vvX = createValueVector(arg1);
+ int size = vvX.getSize();
+ if (size == 0 || vvY.getSize() != size) {
+ return ErrorEval.NA;
+ }
+ result = evaluateInternal(vvX, vvY, size);
+ } catch (EvaluationException e) {
+ return e.getErrorEval();
+ }
+ if (Double.isNaN(result) || Double.isInfinite(result)) {
+ return ErrorEval.NUM_ERROR;
+ }
+ return new NumberEval(result);
+ }
+
+ private double evaluateInternal(ValueVector x, ValueVector y, int size)
+ throws EvaluationException {
+
+ // error handling is as if the x is fully evaluated before y
+ ErrorEval firstYerr = null;
+ boolean accumlatedSome = false;
// first pass: read in data, compute xbar and ybar
double sumx = 0.0, sumy = 0.0;
- for (int i = 0; i < size; i++) {
- ValueEval vx = x.getItem(i);
- ValueEval vy = y.getItem(i);
- if (vx instanceof ErrorEval) {
- throw new EvaluationException((ErrorEval) vx);
- }
- if (vy instanceof ErrorEval) {
- if (firstYerr == null) {
- firstYerr = (ErrorEval) vy;
- continue;
- }
- }
- // only count pairs if both elements are numbers
- // all other combinations of value types are silently ignored
- if (vx instanceof NumberEval && vy instanceof NumberEval) {
- accumlatedSome = true;
- NumberEval nx = (NumberEval) vx;
- NumberEval ny = (NumberEval) vy;
- sumx += nx.getNumberValue();
- sumy += ny.getNumberValue();
- }
- }
-
- if (firstYerr != null) {
- throw new EvaluationException(firstYerr);
- }
-
- if (!accumlatedSome) {
- throw new EvaluationException(ErrorEval.DIV_ZERO);
- }
+ for (int i = 0; i < size; i++) {
+ ValueEval vx = x.getItem(i);
+ ValueEval vy = y.getItem(i);
+ if (vx instanceof ErrorEval) {
+ throw new EvaluationException((ErrorEval) vx);
+ }
+ if (vy instanceof ErrorEval) {
+ if (firstYerr == null) {
+ firstYerr = (ErrorEval) vy;
+ continue;
+ }
+ }
+ // only count pairs if both elements are numbers
+ // all other combinations of value types are silently ignored
+ if (vx instanceof NumberEval && vy instanceof NumberEval) {
+ accumlatedSome = true;
+ NumberEval nx = (NumberEval) vx;
+ NumberEval ny = (NumberEval) vy;
+ sumx += nx.getNumberValue();
+ sumy += ny.getNumberValue();
+ }
+ }
+
+ if (firstYerr != null) {
+ throw new EvaluationException(firstYerr);
+ }
+
+ if (!accumlatedSome) {
+ throw new EvaluationException(ErrorEval.DIV_ZERO);
+ }
- double xbar = sumx / size;
+ double xbar = sumx / size;
double ybar = sumy / size;
- // second pass: compute summary statistics
+ // second pass: compute summary statistics
double xxbar = 0.0, xybar = 0.0;
for (int i = 0; i < size; i++) {
- ValueEval vx = x.getItem(i);
- ValueEval vy = y.getItem(i);
+ ValueEval vx = x.getItem(i);
+ ValueEval vy = y.getItem(i);
- // only count pairs if both elements are numbers
- // all other combinations of value types are silently ignored
- if (vx instanceof NumberEval && vy instanceof NumberEval) {
- NumberEval nx = (NumberEval) vx;
- NumberEval ny = (NumberEval) vy;
- xxbar += (nx.getNumberValue() - xbar) * (nx.getNumberValue() - xbar);
- xybar += (nx.getNumberValue() - xbar) * (ny.getNumberValue() - ybar);
- }
- }
-
- if (xxbar == 0 ) {
- throw new EvaluationException(ErrorEval.DIV_ZERO);
- }
-
- double beta1 = xybar / xxbar;
- double beta0 = ybar - beta1 * xbar;
-
- return (function == FUNCTION.INTERCEPT) ? beta0 : beta1;
- }
-
- private static ValueVector createValueVector(ValueEval arg) throws EvaluationException {
- if (arg instanceof ErrorEval) {
- throw new EvaluationException((ErrorEval) arg);
- }
- if (arg instanceof TwoDEval) {
- return new AreaValueArray((TwoDEval) arg);
- }
- if (arg instanceof RefEval) {
- return new RefValueArray((RefEval) arg);
- }
- return new SingleCellValueArray(arg);
- }
+ // only count pairs if both elements are numbers
+ // all other combinations of value types are silently ignored
+ if (vx instanceof NumberEval && vy instanceof NumberEval) {
+ NumberEval nx = (NumberEval) vx;
+ NumberEval ny = (NumberEval) vy;
+ xxbar += (nx.getNumberValue() - xbar) * (nx.getNumberValue() - xbar);
+ xybar += (nx.getNumberValue() - xbar) * (ny.getNumberValue() - ybar);
+ }
+ }
+
+ if (xxbar == 0 ) {
+ throw new EvaluationException(ErrorEval.DIV_ZERO);
+ }
+
+ double beta1 = xybar / xxbar;
+ double beta0 = ybar - beta1 * xbar;
+
+ return (function == FUNCTION.INTERCEPT) ? beta0 : beta1;
+ }
+
+ private static ValueVector createValueVector(ValueEval arg) throws EvaluationException {
+ if (arg instanceof ErrorEval) {
+ throw new EvaluationException((ErrorEval) arg);
+ }
+ if (arg instanceof TwoDEval) {
+ return new AreaValueArray((TwoDEval) arg);
+ }
+ if (arg instanceof RefEval) {
+ return new RefValueArray((RefEval) arg);
+ }
+ return new SingleCellValueArray(arg);
+ }
}
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/LogicalFunction.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/LogicalFunction.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/LogicalFunction.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/LogicalFunction.java Sat May 22 20:56:44 2021
@@ -26,78 +26,78 @@ import org.apache.poi.ss.formula.eval.*;
public abstract class LogicalFunction extends Fixed1ArgFunction implements ArrayFunction{
@Override
- @SuppressWarnings("unused")
+ @SuppressWarnings("unused")
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) {
- ValueEval ve;
- try {
- ve = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex);
- } catch (EvaluationException e) {
- // Note - it is more usual to propagate error codes straight to the result like this:
+ ValueEval ve;
+ try {
+ ve = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex);
+ } catch (EvaluationException e) {
+ // Note - it is more usual to propagate error codes straight to the result like this:
// but logical functions behave a little differently
- // return e.getErrorEval();
+ // return e.getErrorEval();
- // this will usually cause a 'FALSE' result except for ISNONTEXT()
- ve = e.getErrorEval();
- }
- return BoolEval.valueOf(evaluate(ve));
-
- }
-
- @Override
- public ValueEval evaluateArray(ValueEval[] args, int srcRowIndex, int srcColumnIndex){
- if (args.length != 1) {
- return ErrorEval.VALUE_INVALID;
- }
- return evaluateOneArrayArg(args[0], srcRowIndex, srcColumnIndex, (valA) ->
- BoolEval.valueOf(evaluate(valA))
- );
- }
-
- /**
- * @param arg any {@link ValueEval}, potentially {@link BlankEval} or {@link ErrorEval}.
- */
- protected abstract boolean evaluate(ValueEval arg);
-
- public static final Function ISLOGICAL = new LogicalFunction() {
- @Override
- protected boolean evaluate(ValueEval arg) {
- return arg instanceof BoolEval;
- }
- };
- public static final Function ISNONTEXT = new LogicalFunction() {
- @Override
- protected boolean evaluate(ValueEval arg) {
- return !(arg instanceof StringEval);
- }
- };
- public static final Function ISNUMBER = new LogicalFunction() {
- @Override
- protected boolean evaluate(ValueEval arg) {
- return arg instanceof NumberEval;
- }
- };
- public static final Function ISTEXT = new LogicalFunction() {
- @Override
- protected boolean evaluate(ValueEval arg) {
- return arg instanceof StringEval;
- }
- };
-
- public static final Function ISBLANK = new LogicalFunction() {
-
- @Override
- protected boolean evaluate(ValueEval arg) {
- return arg instanceof BlankEval;
- }
- };
-
- public static final Function ISERROR = new LogicalFunction() {
-
- @Override
- protected boolean evaluate(ValueEval arg) {
- return arg instanceof ErrorEval;
- }
- };
+ // this will usually cause a 'FALSE' result except for ISNONTEXT()
+ ve = e.getErrorEval();
+ }
+ return BoolEval.valueOf(evaluate(ve));
+
+ }
+
+ @Override
+ public ValueEval evaluateArray(ValueEval[] args, int srcRowIndex, int srcColumnIndex){
+ if (args.length != 1) {
+ return ErrorEval.VALUE_INVALID;
+ }
+ return evaluateOneArrayArg(args[0], srcRowIndex, srcColumnIndex, (valA) ->
+ BoolEval.valueOf(evaluate(valA))
+ );
+ }
+
+ /**
+ * @param arg any {@link ValueEval}, potentially {@link BlankEval} or {@link ErrorEval}.
+ */
+ protected abstract boolean evaluate(ValueEval arg);
+
+ public static final Function ISLOGICAL = new LogicalFunction() {
+ @Override
+ protected boolean evaluate(ValueEval arg) {
+ return arg instanceof BoolEval;
+ }
+ };
+ public static final Function ISNONTEXT = new LogicalFunction() {
+ @Override
+ protected boolean evaluate(ValueEval arg) {
+ return !(arg instanceof StringEval);
+ }
+ };
+ public static final Function ISNUMBER = new LogicalFunction() {
+ @Override
+ protected boolean evaluate(ValueEval arg) {
+ return arg instanceof NumberEval;
+ }
+ };
+ public static final Function ISTEXT = new LogicalFunction() {
+ @Override
+ protected boolean evaluate(ValueEval arg) {
+ return arg instanceof StringEval;
+ }
+ };
+
+ public static final Function ISBLANK = new LogicalFunction() {
+
+ @Override
+ protected boolean evaluate(ValueEval arg) {
+ return arg instanceof BlankEval;
+ }
+ };
+
+ public static final Function ISERROR = new LogicalFunction() {
+
+ @Override
+ protected boolean evaluate(ValueEval arg) {
+ return arg instanceof ErrorEval;
+ }
+ };
/**
* Implementation of Excel {@code ISERR()} function.<p>
@@ -120,32 +120,32 @@ public abstract class LogicalFunction ex
}
};
- /**
- * Implementation for Excel ISNA() function.<p>
- *
- * <b>Syntax</b>:<br>
- * <b>ISNA</b>(<b>value</b>)<p>
- *
- * <b>value</b> The value to be tested<br>
- * <br>
- * Returns {@code TRUE} if the specified value is '#N/A', {@code FALSE} otherwise.
- */
- public static final Function ISNA = new LogicalFunction() {
-
- @Override
- protected boolean evaluate(ValueEval arg) {
- return arg == ErrorEval.NA;
- }
- };
-
- public static final Function ISREF = new Fixed1ArgFunction() {
-
- @Override
- public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) {
- if (arg0 instanceof RefEval || arg0 instanceof AreaEval || arg0 instanceof RefListEval) {
- return BoolEval.TRUE;
- }
- return BoolEval.FALSE;
- }
- };
+ /**
+ * Implementation for Excel ISNA() function.<p>
+ *
+ * <b>Syntax</b>:<br>
+ * <b>ISNA</b>(<b>value</b>)<p>
+ *
+ * <b>value</b> The value to be tested<br>
+ * <br>
+ * Returns {@code TRUE} if the specified value is '#N/A', {@code FALSE} otherwise.
+ */
+ public static final Function ISNA = new LogicalFunction() {
+
+ @Override
+ protected boolean evaluate(ValueEval arg) {
+ return arg == ErrorEval.NA;
+ }
+ };
+
+ public static final Function ISREF = new Fixed1ArgFunction() {
+
+ @Override
+ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) {
+ if (arg0 instanceof RefEval || arg0 instanceof AreaEval || arg0 instanceof RefListEval) {
+ return BoolEval.TRUE;
+ }
+ return BoolEval.FALSE;
+ }
+ };
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org