Bug 65241 - Slope and Intercept calc doesn't handle blank cells in range.
Summary: Slope and Intercept calc doesn't handle blank cells in range.
Status: NEW
Alias: None
Product: POI
Classification: Unclassified
Component: XSSF (show other bugs)
Version: 4.1.2-FINAL
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-04-13 20:02 UTC by Thad Welch
Modified: 2021-04-27 10:08 UTC (History)
0 users



Attachments
test case for blank cells in range calc (7.23 KB, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet)
2021-04-13 20:02 UTC, Thad Welch
Details
bug fix (5.96 KB, patch)
2021-04-27 09:50 UTC, Thad Welch
Details | Diff
add Excel RSQ implementation (15.05 KB, patch)
2021-04-27 09:52 UTC, Thad Welch
Details | Diff
Excel RSQ implementation (1.82 KB, patch)
2021-04-27 09:53 UTC, Thad Welch
Details | Diff
changed header doc (1.79 KB, patch)
2021-04-27 09:56 UTC, Thad Welch
Details | Diff
changed header doc (9.19 KB, patch)
2021-04-27 09:56 UTC, Thad Welch
Details | Diff
test case (7.28 KB, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet)
2021-04-27 09:59 UTC, Thad Welch
Details
changed header doc (1.83 KB, text/x-csrc)
2021-04-27 10:01 UTC, Thad Welch
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Thad Welch 2021-04-13 20:02:26 UTC
Created attachment 37809 [details]
test case for blank cells in range calc

Hi, attached excel file shows different Excel/poi 4.1.2 Slope/Intercept calcs caused by the slope/intercept cell range containing blank cells. Code use to generate Excel file:
public static void main(String[] args) {
		try {
			File initialFile = new File(
					"C:\\Users\\twelch\\eclipse-workspace\\Main\\test_excel\\test_slope_intercept.xlsx");
			InputStream is = new FileInputStream(initialFile);
			XSSFWorkbook workbook = new XSSFWorkbook(is);
			is.close();
			
			// Excel formulas : D2 slope, D3 intercept
			// evaluate and save results in F2, F3
			XSSFCell slopeCell = cellAtAddr(workbook, "Sheet1!D2");
			XSSFCell interceptCell = cellAtAddr(workbook, "Sheet1!D3");
			
			evalCellSaveAtAddr(workbook, slopeCell, "Sheet1!F2");
			evalCellSaveAtAddr(workbook, interceptCell, "Sheet1!F3");
						
			workbook.setForceFormulaRecalculation(true);
			File outFile = new File(
					"C:\\Users\\twelch\\eclipse-workspace\\Main\\test_excel\\test_slope_intercept.xlsx");
			FileOutputStream os = new FileOutputStream(outFile);
			workbook.write(os);
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static XSSFCell cellAtAddr(XSSFWorkbook workbook, String cellAddr) {
		CellReference cr = new CellReference(cellAddr);
		XSSFRow row = workbook.getSheet(cr.getSheetName()).getRow(cr.getRow());
		if (row == null)
			row = workbook.getSheet(cr.getSheetName()).createRow(cr.getRow());
		XSSFCell cell = row.getCell(cr.getCol());
		if (cell == null)
			cell = row.createCell(cr.getCol());
		return cell;
	}
	
	public static void  evalCellSaveAtAddr(XSSFWorkbook workbook, XSSFCell evalCell, String destCellAddr) {
		FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
		XSSFCell destCell = cellAtAddr(workbook, destCellAddr);
		evaluator.evaluateFormulaCell(evalCell);
		double dval = evalCell.getNumericCellValue();
		destCell.setCellValue(dval);
	}
Comment 1 Thad Welch 2021-04-27 09:50:17 UTC
Created attachment 37825 [details]
bug fix

using org.apache.commons.math3.stat.regression.SimpleRegression
Comment 2 Thad Welch 2021-04-27 09:52:21 UTC
Created attachment 37826 [details]
add Excel RSQ implementation

using org.apache.commons.math3.stat.regression.SimpleRegression
Comment 3 Thad Welch 2021-04-27 09:53:24 UTC
Created attachment 37827 [details]
Excel RSQ implementation

using org.apache.commons.math3.stat.regression.SimpleRegression
Comment 4 Thad Welch 2021-04-27 09:56:05 UTC
Created attachment 37828 [details]
changed header doc

using org.apache.commons.math3.stat.regression.SimpleRegression
Comment 5 Thad Welch 2021-04-27 09:56:41 UTC
Created attachment 37829 [details]
changed header doc

using org.apache.commons.math3.stat.regression.SimpleRegression
Comment 6 Thad Welch 2021-04-27 09:59:28 UTC
Created attachment 37830 [details]
test case

test case for changes
Comment 7 Thad Welch 2021-04-27 10:01:56 UTC
Created attachment 37831 [details]
changed header doc

using org.apache.commons.math3.stat.regression.SimpleRegression
Comment 8 Thad Welch 2021-04-27 10:08:38 UTC
run test case
public static void main(String[] args) {
		try {
			File excleFile = new File(
					"C:\\Users\\twelch\\eclipse-workspace\\Main\\test_excel\\test_slope_intercept_rsquare.xlsx");
			InputStream is = new FileInputStream(excleFile);
			XSSFWorkbook workbook = new XSSFWorkbook(is);
			is.close();
			
			// Excel formulas : D2 slope, D3 intercept, D4 RSquare
			// evaluate and save results in F2, F3
			XSSFCell slopeCell = cellAtAddr(workbook, "Sheet1!D2");
			XSSFCell interceptCell = cellAtAddr(workbook, "Sheet1!D3");
			XSSFCell rSquareCell = cellAtAddr(workbook, "Sheet1!D4");
			
			evalCellSaveAtAddr(workbook, slopeCell, "Sheet1!F2");
			evalCellSaveAtAddr(workbook, interceptCell, "Sheet1!F3");
			evalCellSaveAtAddr(workbook, rSquareCell, "Sheet1!F4");
						
			workbook.setForceFormulaRecalculation(true);
			FileOutputStream os = new FileOutputStream(excleFile);
			workbook.write(os);
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}