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, "CUMPRINC", null);
r(m, "DEC2BIN", Dec2Bin.instance);
r(m, "DEC2HEX", null);
r(m, "DEC2HEX", Dec2Hex.instance);
r(m, "DEC2OCT", null);
r(m, "DELTA", Delta.instance);
r(m, "DISC", null);

View File

@ -54,9 +54,11 @@ import org.apache.poi.ss.formula.eval.*;
*/
public final class Dec2Hex extends Var1or2ArgFunction implements FreeRefFunction {
private final static long MIN_VALUE = new Long("-549755813888").longValue();
private final static long MAX_VALUE = new Long("549755813887").longValue();
private final static int DEFAULT_PLACES_VALUE = 10;;
public static final FreeRefFunction instance = new Dec2Hex();
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) {
ValueEval veText1;
@ -81,7 +83,8 @@ public final class Dec2Hex extends Var1or2ArgFunction implements FreeRefFunction
int placesNumber = 0;
if (number1 < 0) {
placesNumber = DEFAULT_PLACES_VALUE;
} else {
}
else if (places != null) {
ValueEval placesValueEval;
try {
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) {
hex = String.format("%0"+placesNumber+"X", number1.intValue() & 0x0FFFFF);
hex = String.format("%0"+placesNumber+"X", number1.intValue());
}
else {
hex = Integer.toHexString(number1.intValue());
}
if (number1 < 0) {
hex = "FF"+ hex.substring(2);
}
return new StringEval(hex.toUpperCase());
}
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) {
if (args.length != 2) {
return ErrorEval.VALUE_INVALID;
if (args.length == 1) {
return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0]);
}
if (args.length == 2) {
return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0], args[1]);
}
return ErrorEval.VALUE_INVALID;
}
}