[githib-107] add areas function support. Thanks to Inji Hanbin. This closes #107
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1830685 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
8fc6199641
commit
32b370fb67
@ -139,6 +139,7 @@ public final class FunctionEval {
|
|||||||
retval[73] = CalendarFieldFunction.SECOND;
|
retval[73] = CalendarFieldFunction.SECOND;
|
||||||
retval[74] = new Now();
|
retval[74] = new Now();
|
||||||
// 75: AREAS
|
// 75: AREAS
|
||||||
|
retval[75] = new Areas();
|
||||||
retval[76] = new Rows();
|
retval[76] = new Rows();
|
||||||
retval[77] = new Columns();
|
retval[77] = new Columns();
|
||||||
retval[FunctionID.OFFSET] = new Offset(); //nominally 78
|
retval[FunctionID.OFFSET] = new Offset(); //nominally 78
|
||||||
|
35
src/java/org/apache/poi/ss/formula/functions/Areas.java
Normal file
35
src/java/org/apache/poi/ss/formula/functions/Areas.java
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package org.apache.poi.ss.formula.functions;
|
||||||
|
|
||||||
|
import org.apache.poi.ss.formula.eval.ErrorEval;
|
||||||
|
import org.apache.poi.ss.formula.eval.NumberEval;
|
||||||
|
import org.apache.poi.ss.formula.eval.RefListEval;
|
||||||
|
import org.apache.poi.ss.formula.eval.ValueEval;
|
||||||
|
import org.apache.poi.ss.formula.ptg.NumberPtg;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the number of areas in a reference. An area is a range of contiguous cells or a single cell.
|
||||||
|
*
|
||||||
|
* @author Loopbing (loopbing@gmail.com)
|
||||||
|
*/
|
||||||
|
public final class Areas implements Function {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
|
||||||
|
if (args.length == 0) {
|
||||||
|
return ErrorEval.VALUE_INVALID;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
ValueEval valueEval = args[0];
|
||||||
|
int result = 1;
|
||||||
|
if (valueEval instanceof RefListEval) {
|
||||||
|
RefListEval refListEval = (RefListEval) valueEval;
|
||||||
|
result = refListEval.getList().size();
|
||||||
|
}
|
||||||
|
NumberEval numberEval = new NumberEval(new NumberPtg(result));
|
||||||
|
return numberEval;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return ErrorEval.VALUE_INVALID;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -57,6 +57,7 @@ import org.junit.runners.Suite;
|
|||||||
TestValue.class,
|
TestValue.class,
|
||||||
TestXYNumericFunction.class,
|
TestXYNumericFunction.class,
|
||||||
TestAddress.class,
|
TestAddress.class,
|
||||||
|
TestAreas.class,
|
||||||
TestClean.class
|
TestClean.class
|
||||||
})
|
})
|
||||||
public class AllIndividualFunctionEvaluationTests {
|
public class AllIndividualFunctionEvaluationTests {
|
||||||
|
@ -0,0 +1,42 @@
|
|||||||
|
package org.apache.poi.ss.formula.functions;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
import org.apache.poi.hssf.usermodel.HSSFCell;
|
||||||
|
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
|
||||||
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||||
|
import org.apache.poi.ss.formula.eval.ErrorEval;
|
||||||
|
import org.apache.poi.ss.formula.eval.NumberEval;
|
||||||
|
import org.apache.poi.ss.usermodel.CellType;
|
||||||
|
import org.apache.poi.ss.usermodel.CellValue;
|
||||||
|
|
||||||
|
public final class TestAreas extends TestCase {
|
||||||
|
|
||||||
|
public void testAreas() {
|
||||||
|
HSSFWorkbook wb = new HSSFWorkbook();
|
||||||
|
HSSFCell cell = wb.createSheet().createRow(0).createCell(0);
|
||||||
|
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
|
||||||
|
|
||||||
|
String formulaText = "AREAS(B1)";
|
||||||
|
confirmResult(fe, cell, formulaText,1.0);
|
||||||
|
|
||||||
|
formulaText = "AREAS(B2:D4)";
|
||||||
|
confirmResult(fe, cell, formulaText,1.0);
|
||||||
|
|
||||||
|
formulaText = "AREAS((B2:D4,E5,F6:I9))";
|
||||||
|
confirmResult(fe, cell, formulaText,3.0);
|
||||||
|
|
||||||
|
formulaText = "AREAS((B2:D4,E5,C3,E4))";
|
||||||
|
confirmResult(fe, cell, formulaText,4.0);
|
||||||
|
|
||||||
|
formulaText = "AREAS((I9))";
|
||||||
|
confirmResult(fe, cell, formulaText,1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void confirmResult(HSSFFormulaEvaluator fe, HSSFCell cell, String formulaText,Double expectedResult) {
|
||||||
|
cell.setCellFormula(formulaText);
|
||||||
|
fe.notifyUpdateCell(cell);
|
||||||
|
CellValue result = fe.evaluate(cell);
|
||||||
|
assertEquals(result.getCellTypeEnum(), CellType.NUMERIC);
|
||||||
|
assertEquals(expectedResult, result.getNumberValue());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user