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

(-)src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Replace.java (-1 / +88 lines)
Lines 20-25 Link Here
20
 */
20
 */
21
package org.apache.poi.hssf.record.formula.functions;
21
package org.apache.poi.hssf.record.formula.functions;
22
22
23
public class Replace extends NotImplementedFunction {
23
import org.apache.poi.hssf.record.formula.eval.ErrorEval;
24
import org.apache.poi.hssf.record.formula.eval.Eval;
25
import org.apache.poi.hssf.record.formula.eval.NumericValueEval;
26
import org.apache.poi.hssf.record.formula.eval.StringEval;
27
import org.apache.poi.hssf.record.formula.eval.StringValueEval;
28
import org.apache.poi.hssf.record.formula.eval.ValueEval;
24
29
30
/**
31
 * An implementation of the REPLACE function:
32
 * Replaces part of a text string based on the number of characters 
33
 * you specify, with another text string.
34
 * @author Manda Wilson < wilson at c bio dot msk cc dot org >
35
 */
36
public class Replace extends TextFunction {
37
38
	/**
39
	 * Replaces part of a text string based on the number of characters 
40
	 * you specify, with another text string.
41
	 * 
42
	 * @see org.apache.poi.hssf.record.formula.eval.Eval
43
	 */
44
    public Eval evaluate(Eval[] operands, int srcCellRow, short srcCellCol) {		
45
    	Eval retval = null;
46
        String oldStr = null;
47
        String newStr = null;
48
        int startNum = 0;
49
        int numChars = 0;
50
        
51
        switch (operands.length) {
52
	        default:
53
	            retval = ErrorEval.VALUE_INVALID;
54
	        case 4:	        
55
	        	// first operand is text string containing characters to replace
56
	            // second operand is position of first character to replace
57
	            // third operand is the number of characters in the old string
58
	            // you want to replace with new string
59
	            // fourth operand is the new string
60
	            ValueEval firstveval = singleOperandEvaluate(operands[0], srcCellRow, srcCellCol);
61
	            ValueEval secondveval = singleOperandEvaluate(operands[1], srcCellRow, srcCellCol);
62
	            ValueEval thirdveval = singleOperandEvaluate(operands[2], srcCellRow, srcCellCol);
63
	            ValueEval fourthveval = singleOperandEvaluate(operands[3], srcCellRow, srcCellCol);
64
	            if (firstveval instanceof StringValueEval
65
	            	&& secondveval instanceof NumericValueEval
66
	            	&& thirdveval instanceof NumericValueEval
67
	            	&& fourthveval instanceof StringValueEval) {
68
	            	
69
	                StringValueEval oldStrEval = (StringValueEval) firstveval;
70
	                oldStr = oldStrEval.getStringValue();
71
	                
72
	                NumericValueEval startNumEval = (NumericValueEval) secondveval;
73
	                // NOTE: it is safe to cast to int here
74
	                // because in Excel =REPLACE("task", 2.7, 3, "est") 
75
	                // returns test 
76
	                // so 2.7 must be truncated to 2
77
	                // and =REPLACE("task", 1, 1.9, "") returns ask 
78
	                // so 1.9 must be truncated to 1
79
	                startNum = (int) startNumEval.getNumberValue();
80
	                
81
	                NumericValueEval numCharsEval = (NumericValueEval) thirdveval;
82
	                numChars = (int) numCharsEval.getNumberValue();
83
	                             
84
	                StringValueEval newStrEval = (StringValueEval) fourthveval;
85
	                newStr = newStrEval.getStringValue();
86
	            } else {
87
	            	retval = ErrorEval.VALUE_INVALID;
88
	            }
89
	    }
90
	        
91
        if (retval == null) {
92
			if (startNum < 1 || numChars < 0) {
93
				retval = ErrorEval.VALUE_INVALID;
94
			} else {
95
				StringBuffer strBuff = new StringBuffer(oldStr);
96
				// remove any characters that should be replaced
97
				if (startNum <= oldStr.length() && numChars != 0) {
98
					strBuff.delete(startNum - 1, startNum - 1 + numChars);
99
				} 
100
				// now insert (or append) newStr
101
				if (startNum > strBuff.length()) {
102
					strBuff.append(newStr);
103
				} else {
104
					strBuff.insert(startNum - 1, newStr);
105
				}
106
				retval = new StringEval(strBuff.toString());
107
			}
108
        } 
109
		return retval;
110
    }
111
25
}
112
}

Return to bug 44097