From 9fd5fb147d012c6a0bbe8c7bf66ca042ed47307e Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Sun, 2 Jun 2013 23:39:41 +0000 Subject: [PATCH] Bug 55047: REPT formula support git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1488811 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/ss/formula/eval/FunctionEval.java | 1 + .../apache/poi/ss/formula/functions/Rept.java | 74 ++++++++++++++++++ .../TestReptFunctionsFromSpreadsheet.java | 31 ++++++++ .../spreadsheet/ReptFunctionTestCaseData.xls | Bin 0 -> 28160 bytes 4 files changed, 106 insertions(+) create mode 100644 src/java/org/apache/poi/ss/formula/functions/Rept.java create mode 100644 src/testcases/org/apache/poi/ss/formula/functions/TestReptFunctionsFromSpreadsheet.java create mode 100644 test-data/spreadsheet/ReptFunctionTestCaseData.xls diff --git a/src/java/org/apache/poi/ss/formula/eval/FunctionEval.java b/src/java/org/apache/poi/ss/formula/eval/FunctionEval.java index 3ce3fe2772..d66e878c92 100644 --- a/src/java/org/apache/poi/ss/formula/eval/FunctionEval.java +++ b/src/java/org/apache/poi/ss/formula/eval/FunctionEval.java @@ -89,6 +89,7 @@ public final class FunctionEval { retval[27] = NumericFunction.ROUND; retval[28] = new Lookup(); retval[29] = new Index(); + retval[30] = new Rept(); retval[31] = TextFunction.MID; retval[32] = TextFunction.LEN; diff --git a/src/java/org/apache/poi/ss/formula/functions/Rept.java b/src/java/org/apache/poi/ss/formula/functions/Rept.java new file mode 100644 index 0000000000..a971e6b6d6 --- /dev/null +++ b/src/java/org/apache/poi/ss/formula/functions/Rept.java @@ -0,0 +1,74 @@ +/* ==================================================================== + 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 org.apache.poi.ss.formula.OperationEvaluationContext; +import org.apache.poi.ss.formula.eval.*; + +import java.math.BigDecimal; + +/** + * Implementation for Excel REPT () function.

+ *

+ * Syntax:
REPT (text,number_times )
+ *

+ * Repeats text a given number of times. Use REPT to fill a cell with a number of instances of a text string. + * + * text : text The text that you want to repeat. + * number_times: A positive number specifying the number of times to repeat text. + * + * If number_times is 0 (zero), REPT returns "" (empty text). + * If this argument contains a decimal value, this function ignores the numbers to the right side of the decimal point. + * + * The result of the REPT function cannot be longer than 32,767 characters, or REPT returns #VALUE!. + * + * @author cedric dot walter @ gmail dot com + */ +public class Rept extends Fixed2ArgFunction { + + + @Override + public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval text, ValueEval number_times) { + + ValueEval veText1; + try { + veText1 = OperandResolver.getSingleValue(text, srcRowIndex, srcColumnIndex); + } catch (EvaluationException e) { + return e.getErrorEval(); + } + String strText1 = OperandResolver.coerceValueToString(veText1); + double numberOfTime = 0; + try { + numberOfTime = OperandResolver.coerceValueToDouble(number_times); + } catch (EvaluationException e) { + return ErrorEval.VALUE_INVALID; + } + + int numberOfTimeInt = new Double(numberOfTime).intValue(); + StringBuffer strb = new StringBuffer(strText1.length() * numberOfTimeInt); + for(int i = 0; i < numberOfTimeInt; i++) { + strb.append(strText1); + } + + if (strb.toString().length() > 32767) { + return ErrorEval.VALUE_INVALID; + } + + return new StringEval(strb.toString()); + } +} diff --git a/src/testcases/org/apache/poi/ss/formula/functions/TestReptFunctionsFromSpreadsheet.java b/src/testcases/org/apache/poi/ss/formula/functions/TestReptFunctionsFromSpreadsheet.java new file mode 100644 index 0000000000..3aebcc5050 --- /dev/null +++ b/src/testcases/org/apache/poi/ss/formula/functions/TestReptFunctionsFromSpreadsheet.java @@ -0,0 +1,31 @@ +/* ==================================================================== + 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; + +/** + * Tests REPT() as loaded from a test data spreadsheet.

+ * + * @author cedric dot walter @ gmail dot com + */ +public class TestReptFunctionsFromSpreadsheet extends BaseTestFunctionsFromSpreadsheet { + + @Override + protected String getFilename() { + return "ReptFunctionTestCaseData.xls"; + } +} \ No newline at end of file diff --git a/test-data/spreadsheet/ReptFunctionTestCaseData.xls b/test-data/spreadsheet/ReptFunctionTestCaseData.xls new file mode 100644 index 0000000000000000000000000000000000000000..572f7d4bbf626ee79e146b0bc89368ea4aed2e2a GIT binary patch literal 28160 zcmeHQ4RBo5bw0aVNh|*#$r#)G+0SbmOSU9rNthoS%aZ&9jr=pRC{WBePtudM^=eny z9~(2cC@su%3P}^2zy#dp2WZ-c68;M5losf8LfSEDn}(qwo|JSblTI?tK$B^b0^9Gq z_r2Y>Z{I%ehBR#_dw1^cyXW0|&bjBDd+xdS-6#G1-?hK-xhF6Bj(E~GX^{8xEz;zI zhj4v`Q*IO7&*xcmeg|+3P(A%Wl0Zuoifn3>!+ozb|IK_znxOFSNLZeTyo7I&e?dMd zbF_m=YbqU|G=4E@Sn*(n$is^;ylX=dSY%jEBe%Ont}H_j&jZ$#bgB>k6_-R?1n_kIE<5EG!evp%F19A#s_I zG`@e4HA3Lyc?#eI@zHY5zAQaQjgSnSgO8B3oTElqmY;)4L^K~zM@6Z(2jb`oh2*M0 zJ|gF=aTXR4jN`0CWRc<{h|_I>oY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