mirror of https://github.com/apache/poi.git
support blank cells in correl function
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1901683 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
44be0c0f5a
commit
3d0f69744c
|
@ -29,6 +29,9 @@ import org.apache.poi.ss.formula.eval.RefEval;
|
||||||
import org.apache.poi.ss.formula.eval.StringValueEval;
|
import org.apache.poi.ss.formula.eval.StringValueEval;
|
||||||
import org.apache.poi.ss.formula.eval.ValueEval;
|
import org.apache.poi.ss.formula.eval.ValueEval;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation for Excel CORREL() function.
|
* Implementation for Excel CORREL() function.
|
||||||
* <p>
|
* <p>
|
||||||
|
@ -50,22 +53,34 @@ public class Correl extends Fixed2ArgFunction {
|
||||||
@Override
|
@Override
|
||||||
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) {
|
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) {
|
||||||
try {
|
try {
|
||||||
|
final List<DoubleList> arrays = getNumberArrays(arg0, arg1);
|
||||||
final PearsonsCorrelation pc = new PearsonsCorrelation();
|
final PearsonsCorrelation pc = new PearsonsCorrelation();
|
||||||
final double correl = pc.correlation(
|
final double correl = pc.correlation(
|
||||||
getNumberArray(arg0), getNumberArray(arg1));
|
arrays.get(0).toArray(), arrays.get(1).toArray());
|
||||||
return new NumberEval(correl);
|
return new NumberEval(correl);
|
||||||
} catch (EvaluationException e) {
|
} catch (EvaluationException e) {
|
||||||
return e.getErrorEval();
|
return e.getErrorEval();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private double[] getNumberArray(ValueEval operand) throws EvaluationException {
|
private List<DoubleList> getNumberArrays(ValueEval operand0, ValueEval operand1) throws EvaluationException {
|
||||||
DoubleList retval = new DoubleList();
|
double[] retval0 = collectValuesWithBlanks(operand0).toArray();
|
||||||
collectValues(operand, retval);
|
double[] retval1 = collectValuesWithBlanks(operand1).toArray();
|
||||||
return retval.toArray();
|
DoubleList filtered0 = new DoubleList();
|
||||||
|
DoubleList filtered1 = new DoubleList();
|
||||||
|
for (int i = 0; i < retval0.length; i++) {
|
||||||
|
if (Double.isNaN(retval0[i]) || Double.isNaN(retval1[i])) {
|
||||||
|
//ignore
|
||||||
|
} else {
|
||||||
|
filtered0.add(retval0[i]);
|
||||||
|
filtered1.add(retval1[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Arrays.asList(filtered0, filtered1);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void collectValues(ValueEval operand, DoubleList temp) throws EvaluationException {
|
private DoubleList collectValuesWithBlanks(ValueEval operand) throws EvaluationException {
|
||||||
|
DoubleList doubleList = new DoubleList();
|
||||||
if (operand instanceof ThreeDEval) {
|
if (operand instanceof ThreeDEval) {
|
||||||
ThreeDEval ae = (ThreeDEval) operand;
|
ThreeDEval ae = (ThreeDEval) operand;
|
||||||
for (int sIx = ae.getFirstSheetIndex(); sIx <= ae.getLastSheetIndex(); sIx++) {
|
for (int sIx = ae.getFirstSheetIndex(); sIx <= ae.getLastSheetIndex(); sIx++) {
|
||||||
|
@ -74,11 +89,16 @@ public class Correl extends Fixed2ArgFunction {
|
||||||
for (int rrIx = 0; rrIx < height; rrIx++) {
|
for (int rrIx = 0; rrIx < height; rrIx++) {
|
||||||
for (int rcIx = 0; rcIx < width; rcIx++) {
|
for (int rcIx = 0; rcIx < width; rcIx++) {
|
||||||
ValueEval ve = ae.getValue(sIx, rrIx, rcIx);
|
ValueEval ve = ae.getValue(sIx, rrIx, rcIx);
|
||||||
collectValue(ve, temp);
|
Double d = collectValue(ve);
|
||||||
|
if (d == null) {
|
||||||
|
doubleList.add(Double.NaN);
|
||||||
|
} else {
|
||||||
|
doubleList.add(d.doubleValue());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return doubleList;
|
||||||
}
|
}
|
||||||
if (operand instanceof TwoDEval) {
|
if (operand instanceof TwoDEval) {
|
||||||
TwoDEval ae = (TwoDEval) operand;
|
TwoDEval ae = (TwoDEval) operand;
|
||||||
|
@ -87,46 +107,54 @@ public class Correl extends Fixed2ArgFunction {
|
||||||
for (int rrIx = 0; rrIx < height; rrIx++) {
|
for (int rrIx = 0; rrIx < height; rrIx++) {
|
||||||
for (int rcIx = 0; rcIx < width; rcIx++) {
|
for (int rcIx = 0; rcIx < width; rcIx++) {
|
||||||
ValueEval ve = ae.getValue(rrIx, rcIx);
|
ValueEval ve = ae.getValue(rrIx, rcIx);
|
||||||
collectValue(ve, temp);
|
Double d = collectValue(ve);
|
||||||
|
if (d == null) {
|
||||||
|
doubleList.add(Double.NaN);
|
||||||
|
} else {
|
||||||
|
doubleList.add(d.doubleValue());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return doubleList;
|
||||||
}
|
}
|
||||||
if (operand instanceof RefEval) {
|
if (operand instanceof RefEval) {
|
||||||
RefEval re = (RefEval) operand;
|
RefEval re = (RefEval) operand;
|
||||||
for (int sIx = re.getFirstSheetIndex(); sIx <= re.getLastSheetIndex(); sIx++) {
|
for (int sIx = re.getFirstSheetIndex(); sIx <= re.getLastSheetIndex(); sIx++) {
|
||||||
collectValue(re.getInnerValueEval(sIx), temp);
|
Double d = collectValue(re.getInnerValueEval(sIx));
|
||||||
|
if (d == null) {
|
||||||
|
doubleList.add(Double.NaN);
|
||||||
|
} else {
|
||||||
|
doubleList.add(d.doubleValue());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return;
|
return doubleList;
|
||||||
}
|
}
|
||||||
collectValue(operand, temp);
|
Double d = collectValue(operand);
|
||||||
|
if (d == null) {
|
||||||
|
doubleList.add(Double.NaN);
|
||||||
|
} else {
|
||||||
|
doubleList.add(d.doubleValue());
|
||||||
|
}
|
||||||
|
return doubleList;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void collectValue(ValueEval ve, DoubleList temp) throws EvaluationException {
|
private Double collectValue(ValueEval ve) throws EvaluationException {
|
||||||
if (ve == null) {
|
if (ve == null) {
|
||||||
throw new IllegalArgumentException("ve must not be null");
|
throw new IllegalArgumentException("ve must not be null");
|
||||||
}
|
}
|
||||||
if (ve instanceof NumericValueEval) {
|
if (ve instanceof NumericValueEval) {
|
||||||
NumericValueEval ne = (NumericValueEval) ve;
|
NumericValueEval ne = (NumericValueEval) ve;
|
||||||
temp.add(ne.getNumberValue());
|
return ne.getNumberValue();
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
if (ve instanceof StringValueEval) {
|
if (ve instanceof StringValueEval) {
|
||||||
String s = ((StringValueEval) ve).getStringValue().trim();
|
String s = ((StringValueEval) ve).getStringValue().trim();
|
||||||
Double d = OperandResolver.parseDouble(s);
|
return OperandResolver.parseDouble(s);
|
||||||
if (d == null) {
|
|
||||||
throw new EvaluationException(ErrorEval.VALUE_INVALID);
|
|
||||||
} else {
|
|
||||||
temp.add(d.doubleValue());
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
if (ve instanceof ErrorEval) {
|
if (ve instanceof ErrorEval) {
|
||||||
throw new EvaluationException((ErrorEval) ve);
|
throw new EvaluationException((ErrorEval) ve);
|
||||||
}
|
}
|
||||||
if (ve == BlankEval.instance) {
|
if (ve == BlankEval.instance) {
|
||||||
temp.add(0.0);
|
return null;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
throw new RuntimeException("Invalid ValueEval type passed for conversion: ("
|
throw new RuntimeException("Invalid ValueEval type passed for conversion: ("
|
||||||
+ ve.getClass() + ")");
|
+ ve.getClass() + ")");
|
||||||
|
|
|
@ -46,4 +46,8 @@ final class DoubleList {
|
||||||
_array[_count] = value;
|
_array[_count] = value;
|
||||||
_count++;
|
_count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getLength() {
|
||||||
|
return _count;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,8 +36,8 @@ final class TestCorrel {
|
||||||
|
|
||||||
//https://support.microsoft.com/en-us/office/correl-function-995dcef7-0c0a-4bed-a3fb-239d7b68ca92
|
//https://support.microsoft.com/en-us/office/correl-function-995dcef7-0c0a-4bed-a3fb-239d7b68ca92
|
||||||
@Test
|
@Test
|
||||||
void testMicrosoftExample() throws IOException {
|
void testMicrosoftExample1() throws IOException {
|
||||||
try (HSSFWorkbook wb = initWorkbook1()) {
|
try (HSSFWorkbook wb = initWorkbook1(false)) {
|
||||||
HSSFSheet sheet = wb.getSheetAt(0);
|
HSSFSheet sheet = wb.getSheetAt(0);
|
||||||
HSSFRow row = sheet.getRow(0);
|
HSSFRow row = sheet.getRow(0);
|
||||||
HSSFCell cell = row.createCell(100);
|
HSSFCell cell = row.createCell(100);
|
||||||
|
@ -46,14 +46,29 @@ final class TestCorrel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private HSSFWorkbook initWorkbook1() {
|
@Test
|
||||||
|
void testBlankValue() throws IOException {
|
||||||
|
try (HSSFWorkbook wb = initWorkbook1(true)) {
|
||||||
|
HSSFSheet sheet = wb.getSheetAt(0);
|
||||||
|
HSSFRow row = sheet.getRow(0);
|
||||||
|
HSSFCell cell = row.createCell(100);
|
||||||
|
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
|
||||||
|
assertDouble(fe, cell, "CORREL(A2:A6,B2:B6)", 0.9984884738, 0.0000000005);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private HSSFWorkbook initWorkbook1(boolean blankRow4) {
|
||||||
HSSFWorkbook wb = new HSSFWorkbook();
|
HSSFWorkbook wb = new HSSFWorkbook();
|
||||||
HSSFSheet sheet = wb.createSheet();
|
HSSFSheet sheet = wb.createSheet();
|
||||||
addRow(sheet, 0, "Data1", "Data2");
|
addRow(sheet, 0, "Data1", "Data2");
|
||||||
addRow(sheet, 1, 3, 9);
|
addRow(sheet, 1, 3, 9);
|
||||||
addRow(sheet, 2, 2, 7);
|
addRow(sheet, 2, 2, 7);
|
||||||
addRow(sheet, 3, 4, 12);
|
addRow(sheet, 3, 4, 12);
|
||||||
addRow(sheet, 4, 5, 15);
|
if (blankRow4) {
|
||||||
|
addRow(sheet, 4, 5);
|
||||||
|
} else {
|
||||||
|
addRow(sheet, 4, 5, 15);
|
||||||
|
}
|
||||||
addRow(sheet, 5, 6, 17);
|
addRow(sheet, 5, 6, 17);
|
||||||
return wb;
|
return wb;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue