--- /dev/null
+package org.apache.poi.ss.formula.functions;\r
+\r
+import org.apache.poi.ss.formula.eval.*;\r
+\r
+/**\r
+ * Implementation for Excel CODE () function.<p/>\r
+ * <p/>\r
+ * <b>Syntax</b>:<br/> <b>CODE </b>(<b>text</b> )<br/>\r
+ * <p/>\r
+ * Returns a numeric code for the first character in a text string. The returned code corresponds to the character set used by your computer.\r
+ * <p/>\r
+ * text The text for which you want the code of the first character.\r
+ *\r
+ * @author cedric dot walter @ gmail dot com\r
+ */\r
+public class Code extends Fixed1ArgFunction {\r
+\r
+ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval textArg) {\r
+\r
+ ValueEval veText1;\r
+ try {\r
+ veText1 = OperandResolver.getSingleValue(textArg, srcRowIndex, srcColumnIndex);\r
+ } catch (EvaluationException e) {\r
+ return e.getErrorEval();\r
+ }\r
+ String text = OperandResolver.coerceValueToString(veText1);\r
+\r
+ if (text.length() == 0) {\r
+ return ErrorEval.VALUE_INVALID;\r
+ }\r
+\r
+ int code = (int)text.charAt(0);\r
+\r
+ return new StringEval(String.valueOf(code));\r
+ }\r
+}\r
+\r
\r
package org.apache.poi.ss.formula.functions;\r
\r
+import org.apache.poi.ss.formula.OperationEvaluationContext;\r
import org.apache.poi.ss.formula.eval.*;\r
\r
/**\r
*\r
* @author cedric dot walter @ gmail dot com\r
*/\r
-public final class Dec2Hex extends Var1or2ArgFunction {\r
+public final class Dec2Hex extends Var1or2ArgFunction implements FreeRefFunction {\r
\r
private final static long MIN_VALUE = new Long("-549755813888").longValue();\r
private final static long MAX_VALUE = new Long("549755813887").longValue();\r
private final static int DEFAULT_PLACES_VALUE = 10;;\r
\r
- @Override\r
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval number, ValueEval places) {\r
ValueEval veText1;\r
try {\r
return new StringEval(hex.toUpperCase());\r
}\r
\r
- @Override\r
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) {\r
return this.evaluate(srcRowIndex, srcColumnIndex, arg0, new StringEval(String.valueOf(DEFAULT_PLACES_VALUE)));\r
}\r
+\r
+ public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {\r
+ if (args.length != 2) {\r
+ return ErrorEval.VALUE_INVALID;\r
+ }\r
+ return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0], args[1]);\r
+ }\r
+\r
}
\ No newline at end of file
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+package org.apache.poi.ss.formula.functions;\r
+\r
+import junit.framework.TestCase;\r
+import org.apache.poi.ss.formula.eval.ErrorEval;\r
+import org.apache.poi.ss.formula.eval.StringEval;\r
+import org.apache.poi.ss.formula.eval.ValueEval;\r
+\r
+/**\r
+ * Tests for {@link Code}\r
+ *\r
+ * @author cedric dot walter @ gmail dot com\r
+ */\r
+public class TestCode extends TestCase\r
+{\r
+ private static ValueEval invokeValue(String number1) {\r
+ ValueEval[] args = new ValueEval[]{new StringEval(number1),};\r
+ return new Code().evaluate(args, -1, -1);\r
+ }\r
+\r
+ private static void confirmValue(String msg, String number1, String expected) {\r
+ ValueEval result = invokeValue(number1);\r
+ assertEquals(StringEval.class, result.getClass());\r
+ assertEquals(msg, expected, ((StringEval) result).getStringValue());\r
+ }\r
+\r
+ private static void confirmValueError(String msg, String number1, ErrorEval numError) {\r
+ ValueEval result = invokeValue(number1);\r
+ assertEquals(ErrorEval.class, result.getClass());\r
+ assertEquals(msg, numError, result);\r
+ }\r
+\r
+\r
+ public void testBasic() {\r
+ confirmValue("Displays the numeric code for A (65)", "A", "65");\r
+ confirmValue("Displays the numeric code for the first character in text ABCDEFGHI (65)", "ABCDEFGHI", "65");\r
+\r
+ confirmValue("Displays the numeric code for ! (33)", "!", "33");\r
+ }\r
+\r
+ public void testErrors() {\r
+ confirmValueError("Empty text", "", ErrorEval.VALUE_INVALID);\r
+ }\r
+}\r