View | Details | Raw Unified | Return to bug 55047
Collapse All | Expand All

(-)poi/src/java/org/apache/poi/ss/formula/eval/FunctionEval.java (+2 lines)
Lines 89-94 Link Here
89
		retval[27] = NumericFunction.ROUND;
89
		retval[27] = NumericFunction.ROUND;
90
		retval[28] = new Lookup();
90
		retval[28] = new Lookup();
91
		retval[29] = new Index();
91
		retval[29] = new Index();
92
		retval[30] = new Rept();
92
93
93
		retval[31] = TextFunction.MID;
94
		retval[31] = TextFunction.MID;
94
		retval[32] = TextFunction.LEN;
95
		retval[32] = TextFunction.LEN;
Lines 151-156 Link Here
151
		retval[118] = TextFunction.TRIM;
152
		retval[118] = TextFunction.TRIM;
152
		retval[119] = new Replace();
153
		retval[119] = new Replace();
153
		retval[120] = new Substitute();
154
		retval[120] = new Substitute();
155
        retval[121] = new Code();
154
156
155
		retval[124] = TextFunction.FIND;
157
		retval[124] = TextFunction.FIND;
156
158
(-)poi/src/testcases/org/apache/poi/ss/formula/functions/TestReptFunctionsFromSpreadsheet.java (+31 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Licensed to the Apache Software Foundation (ASF) under one or more
3
   contributor license agreements.  See the NOTICE file distributed with
4
   this work for additional information regarding copyright ownership.
5
   The ASF licenses this file to You under the Apache License, Version 2.0
6
   (the "License"); you may not use this file except in compliance with
7
   the License.  You may obtain a copy of the License at
8
9
       http://www.apache.org/licenses/LICENSE-2.0
10
11
   Unless required by applicable law or agreed to in writing, software
12
   distributed under the License is distributed on an "AS IS" BASIS,
13
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
   See the License for the specific language governing permissions and
15
   limitations under the License.
16
==================================================================== */
17
18
package org.apache.poi.ss.formula.functions;
19
20
/**
21
 * Tests REPT() as loaded from a test data spreadsheet.<p/>
22
 *
23
 * @author cedric dot walter @ gmail dot com
24
 */
25
public class TestReptFunctionsFromSpreadsheet extends BaseTestFunctionsFromSpreadsheet {
26
27
    @Override
28
    protected String getFilename() {
29
        return "ReptFunctionTestCaseData.xls";
30
    }
31
}
(-)poi/src/java/org/apache/poi/ss/formula/functions/Rept.java (+74 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Licensed to the Apache Software Foundation (ASF) under one or more
3
   contributor license agreements.  See the NOTICE file distributed with
4
   this work for additional information regarding copyright ownership.
5
   The ASF licenses this file to You under the Apache License, Version 2.0
6
   (the "License"); you may not use this file except in compliance with
7
   the License.  You may obtain a copy of the License at
8
9
       http://www.apache.org/licenses/LICENSE-2.0
10
11
   Unless required by applicable law or agreed to in writing, software
12
   distributed under the License is distributed on an "AS IS" BASIS,
13
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
   See the License for the specific language governing permissions and
15
   limitations under the License.
16
==================================================================== */
17
18
package org.apache.poi.ss.formula.functions;
19
20
import org.apache.poi.ss.formula.OperationEvaluationContext;
21
import org.apache.poi.ss.formula.eval.*;
22
23
import java.math.BigDecimal;
24
25
/**
26
 * Implementation for Excel REPT () function.<p/>
27
 * <p/>
28
 * <b>Syntax</b>:<br/> <b>REPT  </b>(<b>text</b>,<b>number_times</b> )<br/>
29
 * <p/>
30
 * Repeats text a given number of times. Use REPT to fill a cell with a number of instances of a text string.
31
 *
32
 * text : text The text that you want to repeat.
33
 * number_times:	A positive number specifying the number of times to repeat text.
34
 *
35
 * If number_times is 0 (zero), REPT returns "" (empty text).
36
 * If this argument contains a decimal value, this function ignores the numbers to the right side of the decimal point.
37
 *
38
 * The result of the REPT function cannot be longer than 32,767 characters, or REPT returns #VALUE!.
39
 *
40
 * @author cedric dot walter @ gmail dot com
41
 */
42
public class Rept extends Fixed2ArgFunction  {
43
44
45
    @Override
46
    public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval text, ValueEval number_times) {
47
48
        ValueEval veText1;
49
        try {
50
            veText1 = OperandResolver.getSingleValue(text, srcRowIndex, srcColumnIndex);
51
        } catch (EvaluationException e) {
52
            return e.getErrorEval();
53
        }
54
        String strText1 = OperandResolver.coerceValueToString(veText1);
55
        double numberOfTime = 0;
56
        try {
57
            numberOfTime = OperandResolver.coerceValueToDouble(number_times);
58
        } catch (EvaluationException e) {
59
            return ErrorEval.VALUE_INVALID;
60
        }
61
62
        int numberOfTimeInt = new Double(numberOfTime).intValue();
63
        StringBuffer strb = new StringBuffer(strText1.length() * numberOfTimeInt);
64
        for(int i = 0; i < numberOfTimeInt; i++) {
65
            strb.append(strText1);
66
        }
67
68
        if (strb.toString().length() > 32767) {
69
            return ErrorEval.VALUE_INVALID;
70
        }
71
72
        return new StringEval(strb.toString());
73
    }
74
}

Return to bug 55047