Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AREAS function #107

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/java/org/apache/poi/ss/formula/eval/FunctionEval.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ private static Function[] produceFunctions() {
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
Expand Down
35 changes: 35 additions & 0 deletions src/java/org/apache/poi/ss/formula/functions/Areas.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
TestValue.class,
TestXYNumericFunction.class,
TestAddress.class,
TestAreas.class,
TestClean.class
})
public class AllIndividualFunctionEvaluationTests {
Expand Down
42 changes: 42 additions & 0 deletions src/testcases/org/apache/poi/ss/formula/functions/TestAreas.java
Original file line number Diff line number Diff line change
@@ -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());
}
}