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

(-)src/java/org/apache/poi/hssf/record/formula/functions/Find.java (-1 / +31 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 Find extends NotImplementedFunction {
23
import org.apache.poi.hssf.record.formula.eval.Eval;
24
import org.apache.poi.hssf.record.formula.eval.ErrorEval;
25
import org.apache.poi.hssf.record.formula.eval.ValueEval;
26
import org.apache.poi.hssf.record.formula.eval.NumberEval;
27
import org.apache.poi.hssf.record.formula.eval.EvaluationException;
24
28
29
/**  
30
 * @author Torstein Tauno Svendsen (torstei@officenet.no)
31
 */
32
33
public class Find extends TextFunction {
34
35
	protected ValueEval evaluateFunc(Eval[] args, int srcCellRow, short srcCellCol) throws EvaluationException {
36
		if(args.length < 2 || args.length > 3) {
37
			return ErrorEval.VALUE_INVALID;
38
		}
39
		String needle  = evaluateStringArg(args[0], srcCellRow, srcCellCol);
40
		String haystack = evaluateStringArg(args[1], srcCellRow, srcCellCol);
41
		int startpos = 0;
42
		if( args.length == 3 ) {
43
			try {
44
				startpos = evaluateIntArg(args[2], srcCellRow, srcCellCol);
45
				if(startpos <= 0 ) return ErrorEval.VALUE_INVALID;
46
				startpos--;
47
			} catch (EvaluationException e) {
48
				return ErrorEval.VALUE_INVALID;
49
			}
50
		}
51
		int r = haystack.indexOf(needle,startpos);
52
		if( r == -1 ) return ErrorEval.VALUE_INVALID;
53
		return new NumberEval( r +1 );
54
	}
25
}
55
}
(-)src/testcases/org/apache/poi/hssf/record/formula/functions/TestFind.java (+72 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.hssf.record.formula.functions;
19
20
import junit.framework.TestCase;
21
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
22
import org.apache.poi.hssf.usermodel.HSSFSheet;
23
import org.apache.poi.hssf.usermodel.HSSFRow;
24
import org.apache.poi.hssf.usermodel.HSSFCell;
25
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
26
27
/**
28
 * @author Torstein Svendsen (torstei@officenet.no)
29
 */
30
31
public final class TestFind extends TestCase {
32
33
	public void testFind() {
34
        HSSFWorkbook wb = new HSSFWorkbook();
35
        HSSFSheet sheet1 = wb.createSheet();       
36
37
        HSSFRow row = sheet1.createRow(0);
38
        HSSFCell cell = row.createCell(0);
39
40
41
        cell.setCellFormula("find(\"n\", \"haystack\")");
42
        HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
43
        HSSFFormulaEvaluator.CellValue result = fe.evaluate(cell);
44
        assertEquals(HSSFCell.CELL_TYPE_ERROR, result.getCellType());
45
46
		fe.clearAllCachedResultValues();
47
		cell.setCellFormula("find(\"h\", \"haystack\")");
48
		result = fe.evaluate(cell);
49
		assertEquals( result.getCellType(), HSSFCell.CELL_TYPE_NUMERIC);
50
        assertTrue( 1 ==  result.getNumberValue());
51
52
		cell.setCellFormula("find(\"a\", \"haystack\",2)");
53
		fe.clearAllCachedResultValues();
54
		result = fe.evaluate(cell);
55
		assertEquals( result.getCellType(), HSSFCell.CELL_TYPE_NUMERIC);
56
		assertTrue( 2 ==  result.getNumberValue());
57
58
		cell.setCellFormula("find(\"a\", \"haystack\",3)");
59
		fe.clearAllCachedResultValues();
60
		result = fe.evaluate(cell);
61
		assertEquals( result.getCellType(), HSSFCell.CELL_TYPE_NUMERIC);
62
63
		assertTrue( 6 ==  result.getNumberValue());
64
65
		cell.setCellFormula("find(\"k\", \"haystack\",9)");
66
		fe.clearAllCachedResultValues();
67
		result = fe.evaluate(cell);
68
		assertEquals( result.getCellType(), HSSFCell.CELL_TYPE_ERROR);
69
70
        
71
   }
72
}

Return to bug 45966