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

(-)src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Trim.java (-1 / +56 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 Trim extends NotImplementedFunction {
23
import org.apache.poi.hssf.record.formula.eval.BlankEval;
24
import org.apache.poi.hssf.record.formula.eval.ErrorEval;
25
import org.apache.poi.hssf.record.formula.eval.Eval;
26
import org.apache.poi.hssf.record.formula.eval.NumberEval;
27
import org.apache.poi.hssf.record.formula.eval.StringEval;
28
import org.apache.poi.hssf.record.formula.eval.StringValueEval;
29
import org.apache.poi.hssf.record.formula.eval.ValueEval;
24
30
31
/**
32
 * @author Manda Wilson < wilson at c bio dot msk cc dot org >
33
 *
34
 */
35
public class Trim extends TextFunction {
36
37
	/**
38
	 * Removes leading and trailing spaces from value if evaluated operand value is string.
39
	 * Returns StringEval only if evaluated operand is of type string (and is not blank or null) 
40
	 * or number.  If evaluated operand is of type string and is blank or null, or if evaluated
41
	 * operand is of type blank, returns BlankEval.  Otherwise returns ErrorEval.
42
	 * 
43
	 * @param operands
44
	 * @param srcCellRow
45
	 * @param srcCellCol
46
	 * @return 
47
	 * @see org.apache.poi.hssf.record.formula.eval.Eval
48
	 */
49
    public Eval evaluate(Eval[] operands, int srcCellRow, short srcCellCol) {
50
    	Eval retval = ErrorEval.VALUE_INVALID;
51
        String str = null;
52
        
53
        switch (operands.length) {
54
	        default:
55
	            break;
56
	        case 1:
57
	            ValueEval veval = singleOperandEvaluate(operands[0], srcCellRow, srcCellCol);
58
	            if (veval instanceof StringValueEval) {
59
	                StringValueEval sve = (StringValueEval) veval;
60
	                str = sve.getStringValue();
61
	                if (str == null || str.trim().equals("")) {
62
	                	return BlankEval.INSTANCE;
63
	                }
64
	            }
65
	            else if (veval instanceof NumberEval) {
66
	                NumberEval neval = (NumberEval) veval;
67
	                str = neval.getStringValue();
68
	            } 
69
	            else if (veval instanceof BlankEval) {
70
	            	return BlankEval.INSTANCE;
71
	            }
72
	    }
73
	        
74
        if (str != null) {
75
            retval = new StringEval(str.trim());
76
        } 
77
        return retval;
78
    }
79
	
25
}
80
}

Return to bug 44050