Patch from Conor from bug #57747 - Add ISERR() function
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1675741 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
2be2a7cbfc
commit
5516228367
@ -154,6 +154,7 @@ public final class FunctionEval {
|
|||||||
|
|
||||||
retval[124] = TextFunction.FIND;
|
retval[124] = TextFunction.FIND;
|
||||||
|
|
||||||
|
retval[126] = LogicalFunction.ISERR;
|
||||||
retval[127] = LogicalFunction.ISTEXT;
|
retval[127] = LogicalFunction.ISTEXT;
|
||||||
retval[128] = LogicalFunction.ISNUMBER;
|
retval[128] = LogicalFunction.ISNUMBER;
|
||||||
retval[129] = LogicalFunction.ISBLANK;
|
retval[129] = LogicalFunction.ISBLANK;
|
||||||
|
@ -90,6 +90,27 @@ public abstract class LogicalFunction extends Fixed1ArgFunction {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implementation of Excel <tt>ISERR()</tt> function.<p/>
|
||||||
|
*
|
||||||
|
* <b>Syntax</b>:<br/>
|
||||||
|
* <b>ISERR</b>(<b>value</b>)<p/>
|
||||||
|
*
|
||||||
|
* <b>value</b> The value to be tested<p/>
|
||||||
|
*
|
||||||
|
* Returns the logical value <tt>TRUE</tt> if value refers to any error value except
|
||||||
|
* <tt>'#N/A'</tt>; otherwise, it returns <tt>FALSE</tt>.
|
||||||
|
*/
|
||||||
|
public static final Function ISERR = new LogicalFunction() {
|
||||||
|
@Override
|
||||||
|
protected boolean evaluate(ValueEval arg) {
|
||||||
|
if (arg instanceof ErrorEval) {
|
||||||
|
return arg != ErrorEval.NA;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation for Excel ISNA() function.<p/>
|
* Implementation for Excel ISNA() function.<p/>
|
||||||
*
|
*
|
||||||
|
@ -0,0 +1,88 @@
|
|||||||
|
package org.apache.poi.ss.formula.functions;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||||
|
import org.apache.poi.ss.usermodel.Cell;
|
||||||
|
import org.apache.poi.ss.usermodel.CellValue;
|
||||||
|
import org.apache.poi.ss.usermodel.FormulaEvaluator;
|
||||||
|
import org.apache.poi.ss.usermodel.Row;
|
||||||
|
import org.apache.poi.ss.usermodel.Sheet;
|
||||||
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
|
import org.apache.poi.ss.util.CellReference;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* LogicalFunction unit tests.
|
||||||
|
*/
|
||||||
|
public class TestLogicalFunction extends TestCase {
|
||||||
|
|
||||||
|
private FormulaEvaluator evaluator;
|
||||||
|
private Row row3;
|
||||||
|
private Cell cell1;
|
||||||
|
private Cell cell2;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setUp() throws IOException {
|
||||||
|
Workbook wb = new HSSFWorkbook();
|
||||||
|
try {
|
||||||
|
buildWorkbook(wb);
|
||||||
|
} finally {
|
||||||
|
wb.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void buildWorkbook(Workbook wb) {
|
||||||
|
Sheet sh = wb.createSheet();
|
||||||
|
Row row1 = sh.createRow(0);
|
||||||
|
Row row2 = sh.createRow(1);
|
||||||
|
row3 = sh.createRow(2);
|
||||||
|
|
||||||
|
row1.createCell(0, Cell.CELL_TYPE_NUMERIC);
|
||||||
|
row1.createCell(1, Cell.CELL_TYPE_NUMERIC);
|
||||||
|
|
||||||
|
row2.createCell(0, Cell.CELL_TYPE_NUMERIC);
|
||||||
|
row2.createCell(1, Cell.CELL_TYPE_NUMERIC);
|
||||||
|
|
||||||
|
row3.createCell(0);
|
||||||
|
row3.createCell(1);
|
||||||
|
|
||||||
|
CellReference a1 = new CellReference("A1");
|
||||||
|
CellReference a2 = new CellReference("A2");
|
||||||
|
CellReference b1 = new CellReference("B1");
|
||||||
|
CellReference b2 = new CellReference("B2");
|
||||||
|
|
||||||
|
sh.getRow(a1.getRow()).getCell(a1.getCol()).setCellValue(35);
|
||||||
|
sh.getRow(a2.getRow()).getCell(a2.getCol()).setCellValue(0);
|
||||||
|
sh.getRow(b1.getRow()).getCell(b1.getCol()).setCellFormula("A1/A2");
|
||||||
|
sh.getRow(b2.getRow()).getCell(b2.getCol()).setCellFormula("NA()");
|
||||||
|
|
||||||
|
evaluator = wb.getCreationHelper().createFormulaEvaluator();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testIsErr() {
|
||||||
|
cell1 = row3.createCell(0);
|
||||||
|
cell1.setCellFormula("ISERR(B1)"); // produces #DIV/0!
|
||||||
|
cell2 = row3.createCell(1);
|
||||||
|
cell2.setCellFormula("ISERR(B2)"); // produces #N/A
|
||||||
|
|
||||||
|
CellValue cell1Value = evaluator.evaluate(cell1);
|
||||||
|
CellValue cell2Value = evaluator.evaluate(cell2);
|
||||||
|
|
||||||
|
assertEquals(true, cell1Value.getBooleanValue());
|
||||||
|
assertEquals(false, cell2Value.getBooleanValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testIsError() {
|
||||||
|
cell1 = row3.createCell(0);
|
||||||
|
cell1.setCellFormula("ISERROR(B1)"); // produces #DIV/0!
|
||||||
|
cell2 = row3.createCell(1);
|
||||||
|
cell2.setCellFormula("ISERROR(B2)"); // produces #N/A
|
||||||
|
|
||||||
|
CellValue cell1Value = evaluator.evaluate(cell1);
|
||||||
|
CellValue cell2Value = evaluator.evaluate(cell2);
|
||||||
|
|
||||||
|
assertEquals(true, cell1Value.getBooleanValue());
|
||||||
|
assertEquals(true, cell2Value.getBooleanValue());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user