[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:
PJ Fanning 2018-05-01 14:47:29 +00:00
parent 8fc6199641
commit 32b370fb67
4 changed files with 79 additions and 0 deletions

View File

@ -139,6 +139,7 @@ public final class FunctionEval {
retval[73] = CalendarFieldFunction.SECOND;
retval[74] = new Now();
// 75: AREAS
retval[75] = new Areas();
retval[76] = new Rows();
retval[77] = new Columns();
retval[FunctionID.OFFSET] = new Offset(); //nominally 78

View 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;
}
}
}

View File

@ -57,6 +57,7 @@ import org.junit.runners.Suite;
TestValue.class,
TestXYNumericFunction.class,
TestAddress.class,
TestAreas.class,
TestClean.class
})
public class AllIndividualFunctionEvaluationTests {

View File

@ -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());
}
}