git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1488733 13f79535-47bb-0310-9956-ffa450edef68tags/3.10-beta1
@@ -0,0 +1,37 @@ | |||
package org.apache.poi.ss.formula.functions; | |||
import org.apache.poi.ss.formula.eval.*; | |||
/** | |||
* Implementation for Excel CODE () function.<p/> | |||
* <p/> | |||
* <b>Syntax</b>:<br/> <b>CODE </b>(<b>text</b> )<br/> | |||
* <p/> | |||
* Returns a numeric code for the first character in a text string. The returned code corresponds to the character set used by your computer. | |||
* <p/> | |||
* text The text for which you want the code of the first character. | |||
* | |||
* @author cedric dot walter @ gmail dot com | |||
*/ | |||
public class Code extends Fixed1ArgFunction { | |||
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval textArg) { | |||
ValueEval veText1; | |||
try { | |||
veText1 = OperandResolver.getSingleValue(textArg, srcRowIndex, srcColumnIndex); | |||
} catch (EvaluationException e) { | |||
return e.getErrorEval(); | |||
} | |||
String text = OperandResolver.coerceValueToString(veText1); | |||
if (text.length() == 0) { | |||
return ErrorEval.VALUE_INVALID; | |||
} | |||
int code = (int)text.charAt(0); | |||
return new StringEval(String.valueOf(code)); | |||
} | |||
} | |||
@@ -17,6 +17,7 @@ | |||
package org.apache.poi.ss.formula.functions; | |||
import org.apache.poi.ss.formula.OperationEvaluationContext; | |||
import org.apache.poi.ss.formula.eval.*; | |||
/** | |||
@@ -51,13 +52,12 @@ import org.apache.poi.ss.formula.eval.*; | |||
* | |||
* @author cedric dot walter @ gmail dot com | |||
*/ | |||
public final class Dec2Hex extends Var1or2ArgFunction { | |||
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;; | |||
@Override | |||
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval number, ValueEval places) { | |||
ValueEval veText1; | |||
try { | |||
@@ -115,8 +115,15 @@ public final class Dec2Hex extends Var1or2ArgFunction { | |||
return new StringEval(hex.toUpperCase()); | |||
} | |||
@Override | |||
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) { | |||
return this.evaluate(srcRowIndex, srcColumnIndex, arg0, new StringEval(String.valueOf(DEFAULT_PLACES_VALUE))); | |||
} | |||
public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) { | |||
if (args.length != 2) { | |||
return ErrorEval.VALUE_INVALID; | |||
} | |||
return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0], args[1]); | |||
} | |||
} |
@@ -42,7 +42,6 @@ public final class Delta extends Fixed2ArgFunction { | |||
private final static NumberEval ONE = new NumberEval(1); | |||
private final static NumberEval ZERO = new NumberEval(0); | |||
@Override | |||
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg1, ValueEval arg2) { | |||
ValueEval veText1; | |||
try { |
@@ -0,0 +1,59 @@ | |||
/* ==================================================================== | |||
Licensed to the Apache Software Foundation (ASF) under one or more | |||
contributor license agreements. See the NOTICE file distributed with | |||
this work for additional information regarding copyright ownership. | |||
The ASF licenses this file to You under the Apache License, Version 2.0 | |||
(the "License"); you may not use this file except in compliance with | |||
the License. You may obtain a copy of the License at | |||
http://www.apache.org/licenses/LICENSE-2.0 | |||
Unless required by applicable law or agreed to in writing, software | |||
distributed under the License is distributed on an "AS IS" BASIS, | |||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
See the License for the specific language governing permissions and | |||
limitations under the License. | |||
==================================================================== */ | |||
package org.apache.poi.ss.formula.functions; | |||
import junit.framework.TestCase; | |||
import org.apache.poi.ss.formula.eval.ErrorEval; | |||
import org.apache.poi.ss.formula.eval.StringEval; | |||
import org.apache.poi.ss.formula.eval.ValueEval; | |||
/** | |||
* Tests for {@link Code} | |||
* | |||
* @author cedric dot walter @ gmail dot com | |||
*/ | |||
public class TestCode extends TestCase | |||
{ | |||
private static ValueEval invokeValue(String number1) { | |||
ValueEval[] args = new ValueEval[]{new StringEval(number1),}; | |||
return new Code().evaluate(args, -1, -1); | |||
} | |||
private static void confirmValue(String msg, String number1, String expected) { | |||
ValueEval result = invokeValue(number1); | |||
assertEquals(StringEval.class, result.getClass()); | |||
assertEquals(msg, expected, ((StringEval) result).getStringValue()); | |||
} | |||
private static void confirmValueError(String msg, String number1, ErrorEval numError) { | |||
ValueEval result = invokeValue(number1); | |||
assertEquals(ErrorEval.class, result.getClass()); | |||
assertEquals(msg, numError, result); | |||
} | |||
public void testBasic() { | |||
confirmValue("Displays the numeric code for A (65)", "A", "65"); | |||
confirmValue("Displays the numeric code for the first character in text ABCDEFGHI (65)", "ABCDEFGHI", "65"); | |||
confirmValue("Displays the numeric code for ! (33)", "!", "33"); | |||
} | |||
public void testErrors() { | |||
confirmValueError("Empty text", "", ErrorEval.VALUE_INVALID); | |||
} | |||
} |