--- src/java/org/apache/poi/hssf/record/formula/functions/Find.java (revision 702751) +++ src/java/org/apache/poi/hssf/record/formula/functions/Find.java (working copy) @@ -20,6 +20,36 @@ */ package org.apache.poi.hssf.record.formula.functions; -public class Find extends NotImplementedFunction { +import org.apache.poi.hssf.record.formula.eval.Eval; +import org.apache.poi.hssf.record.formula.eval.ErrorEval; +import org.apache.poi.hssf.record.formula.eval.ValueEval; +import org.apache.poi.hssf.record.formula.eval.NumberEval; +import org.apache.poi.hssf.record.formula.eval.EvaluationException; +/** + * @author Torstein Tauno Svendsen (torstei@officenet.no) + */ + +public class Find extends TextFunction { + + protected ValueEval evaluateFunc(Eval[] args, int srcCellRow, short srcCellCol) throws EvaluationException { + if(args.length < 2 || args.length > 3) { + return ErrorEval.VALUE_INVALID; + } + String needle = evaluateStringArg(args[0], srcCellRow, srcCellCol); + String haystack = evaluateStringArg(args[1], srcCellRow, srcCellCol); + int startpos = 0; + if( args.length == 3 ) { + try { + startpos = evaluateIntArg(args[2], srcCellRow, srcCellCol); + if(startpos <= 0 ) return ErrorEval.VALUE_INVALID; + startpos--; + } catch (EvaluationException e) { + return ErrorEval.VALUE_INVALID; + } + } + int r = haystack.indexOf(needle,startpos); + if( r == -1 ) return ErrorEval.VALUE_INVALID; + return new NumberEval( r +1 ); + } } --- src/testcases/org/apache/poi/hssf/record/formula/functions/TestFind.java (revision 0) +++ src/testcases/org/apache/poi/hssf/record/formula/functions/TestFind.java (revision 0) @@ -0,0 +1,72 @@ +/* ==================================================================== + 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.hssf.record.formula.functions; + +import junit.framework.TestCase; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFRow; +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator; + +/** + * @author Torstein Svendsen (torstei@officenet.no) + */ + +public final class TestFind extends TestCase { + + public void testFind() { + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet sheet1 = wb.createSheet(); + + HSSFRow row = sheet1.createRow(0); + HSSFCell cell = row.createCell(0); + + + cell.setCellFormula("find(\"n\", \"haystack\")"); + HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb); + HSSFFormulaEvaluator.CellValue result = fe.evaluate(cell); + assertEquals(HSSFCell.CELL_TYPE_ERROR, result.getCellType()); + + fe.clearAllCachedResultValues(); + cell.setCellFormula("find(\"h\", \"haystack\")"); + result = fe.evaluate(cell); + assertEquals( result.getCellType(), HSSFCell.CELL_TYPE_NUMERIC); + assertTrue( 1 == result.getNumberValue()); + + cell.setCellFormula("find(\"a\", \"haystack\",2)"); + fe.clearAllCachedResultValues(); + result = fe.evaluate(cell); + assertEquals( result.getCellType(), HSSFCell.CELL_TYPE_NUMERIC); + assertTrue( 2 == result.getNumberValue()); + + cell.setCellFormula("find(\"a\", \"haystack\",3)"); + fe.clearAllCachedResultValues(); + result = fe.evaluate(cell); + assertEquals( result.getCellType(), HSSFCell.CELL_TYPE_NUMERIC); + + assertTrue( 6 == result.getNumberValue()); + + cell.setCellFormula("find(\"k\", \"haystack\",9)"); + fe.clearAllCachedResultValues(); + result = fe.evaluate(cell); + assertEquals( result.getCellType(), HSSFCell.CELL_TYPE_ERROR); + + + } +}