Bug 55036: patch for missing function Dec2HEx

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1531395 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Cédric Walter 2013-10-11 19:16:43 +00:00
parent 1d8a31724a
commit b43a685b6c
3 changed files with 23 additions and 14 deletions

View File

@ -89,7 +89,7 @@ public final class AnalysisToolPak implements UDFFinder {
r(m, "CUMIPMT", null); r(m, "CUMIPMT", null);
r(m, "CUMPRINC", null); r(m, "CUMPRINC", null);
r(m, "DEC2BIN", Dec2Bin.instance); r(m, "DEC2BIN", Dec2Bin.instance);
r(m, "DEC2HEX", null); r(m, "DEC2HEX", Dec2Hex.instance);
r(m, "DEC2OCT", null); r(m, "DEC2OCT", null);
r(m, "DELTA", Delta.instance); r(m, "DELTA", Delta.instance);
r(m, "DISC", null); r(m, "DISC", null);

View File

@ -45,7 +45,7 @@ import org.apache.poi.ss.formula.eval.*;
* <ul> * <ul>
* <li>If this argument is omitted, this function uses the minimum number of characters necessary.</li> * <li>If this argument is omitted, this function uses the minimum number of characters necessary.</li>
* <li>If this function requires more than places characters, it returns the #NUM! error value.</li> * <li>If this function requires more than places characters, it returns the #NUM! error value.</li>
* <li>If this argument is nonnumeric, this function returns the #VALUE! error value.</li> * <li>If this argument is non numeric, this function returns the #VALUE! error value.</li>
* <li>If this argument is negative, this function returns the #NUM! error value.</li> * <li>If this argument is negative, this function returns the #NUM! error value.</li>
* <li>If this argument contains a decimal value, this function ignores the numbers to the right side of the decimal point.</li> * <li>If this argument contains a decimal value, this function ignores the numbers to the right side of the decimal point.</li>
* </ul> * </ul>
@ -54,9 +54,11 @@ import org.apache.poi.ss.formula.eval.*;
*/ */
public final class Dec2Hex extends Var1or2ArgFunction implements FreeRefFunction { public final class Dec2Hex extends Var1or2ArgFunction implements FreeRefFunction {
private final static long MIN_VALUE = new Long("-549755813888").longValue(); public static final FreeRefFunction instance = new Dec2Hex();
private final static long MAX_VALUE = new Long("549755813887").longValue();
private final static int DEFAULT_PLACES_VALUE = 10;; private final static long MIN_VALUE = Long.parseLong("-549755813888");
private final static long MAX_VALUE = Long.parseLong("549755813887");
private final static int DEFAULT_PLACES_VALUE = 10;
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval number, ValueEval places) { public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval number, ValueEval places) {
ValueEval veText1; ValueEval veText1;
@ -81,7 +83,8 @@ public final class Dec2Hex extends Var1or2ArgFunction implements FreeRefFunction
int placesNumber = 0; int placesNumber = 0;
if (number1 < 0) { if (number1 < 0) {
placesNumber = DEFAULT_PLACES_VALUE; placesNumber = DEFAULT_PLACES_VALUE;
} else { }
else if (places != null) {
ValueEval placesValueEval; ValueEval placesValueEval;
try { try {
placesValueEval = OperandResolver.getSingleValue(places, srcRowIndex, srcColumnIndex); placesValueEval = OperandResolver.getSingleValue(places, srcRowIndex, srcColumnIndex);
@ -104,26 +107,32 @@ public final class Dec2Hex extends Var1or2ArgFunction implements FreeRefFunction
} }
} }
String hex = ""; String hex;
if (placesNumber != 0) { if (placesNumber != 0) {
hex = String.format("%0"+placesNumber+"X", number1.intValue() & 0x0FFFFF); hex = String.format("%0"+placesNumber+"X", number1.intValue());
} }
else { else {
hex = Integer.toHexString(number1.intValue()); hex = Integer.toHexString(number1.intValue());
} }
if (number1 < 0) {
hex = "FF"+ hex.substring(2);
}
return new StringEval(hex.toUpperCase()); return new StringEval(hex.toUpperCase());
} }
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) { public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) {
return this.evaluate(srcRowIndex, srcColumnIndex, arg0, new StringEval(String.valueOf(DEFAULT_PLACES_VALUE))); return this.evaluate(srcRowIndex, srcColumnIndex, arg0, null);
} }
public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) { public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
if (args.length != 2) { if (args.length == 1) {
return ErrorEval.VALUE_INVALID; return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0]);
} }
return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0], args[1]); if (args.length == 2) {
return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0], args[1]);
}
return ErrorEval.VALUE_INVALID;
} }
} }