Various smaller changes

Improve exception messages
Add more JavaDoc
Provide more information on test-failures

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1899534 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2022-04-03 13:25:27 +00:00
parent d80f48dd82
commit 0ee8c135c4
5 changed files with 43 additions and 14 deletions

View File

@ -1107,7 +1107,7 @@ public final class FormulaParser {
public CellReference getCellReference() {
if (_type != Type.CELL) {
throw new IllegalStateException("Not applicable to this type");
throw new IllegalStateException("Not applicable to this reference-type, expected CELL, but had " + _type);
}
return new CellReference(_rep);
}

View File

@ -45,6 +45,7 @@ final class LazyAreaEval extends AreaEvalBase {
public ValueEval getRelativeValue(int relativeRowIndex, int relativeColumnIndex) {
return getRelativeValue(getFirstSheetIndex(), relativeRowIndex, relativeColumnIndex);
}
@Override
public ValueEval getRelativeValue(int sheetIndex, int relativeRowIndex, int relativeColumnIndex) {
int rowIx = (relativeRowIndex + getFirstRow() ) ;
@ -60,6 +61,7 @@ final class LazyAreaEval extends AreaEvalBase {
return new LazyAreaEval(area, _evaluator);
}
@Override
public LazyAreaEval getRow(int rowIndex) {
if (rowIndex >= getHeight()) {
@ -69,6 +71,7 @@ final class LazyAreaEval extends AreaEvalBase {
int absRowIx = getFirstRow() + rowIndex;
return new LazyAreaEval(absRowIx, getFirstColumn(), absRowIx, getLastColumn(), _evaluator);
}
@Override
public LazyAreaEval getColumn(int columnIndex) {
if (columnIndex >= getWidth()) {

View File

@ -170,6 +170,14 @@ public class AreaReference {
return splitAreaReferences(reference).length == 1;
}
/**
* Construct an AreaReference which spans one more rows
*
* @param version Is the spreadsheet in format Excel97 or newer Excel versions
* @param start The 1-based start-index of the rows
* @param end The 1-based end-index of the rows
* @return An AreaReference that spans the given rows
*/
public static AreaReference getWholeRow(SpreadsheetVersion version, String start, String end) {
if (null == version) {
version = DEFAULT_SPREADSHEET_VERSION;
@ -177,6 +185,17 @@ public class AreaReference {
return new AreaReference("$A" + start + ":$" + version.getLastColumnName() + end, version);
}
/**
* Construct an AreaReference which spans one more columns.
*
* Columns are specified in the Excel format, i.e. "A" is the first column
* "B" the seconds, ... "AA", ..
*
* @param version Is the spreadsheet in format Excel97 or newer Excel versions
* @param start The ABC-based start-index of the columns
* @param end The ABC-based end-index of the columns
* @return An AreaReference that spans the given columns
*/
public static AreaReference getWholeColumn(SpreadsheetVersion version, String start, String end) {
if (null == version) {
version = DEFAULT_SPREADSHEET_VERSION;

View File

@ -37,6 +37,7 @@ import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.LocaleUtil;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.params.ParameterizedTest;
@ -171,13 +172,16 @@ public final class TestFormulasFromSpreadsheet {
CellValue actValue = evaluator.evaluate(c);
Cell expValue = (expectedValuesRow == null) ? null : expectedValuesRow.getCell(colnum);
String msg = String.format(Locale.ROOT, "Function '%s': Formula: %s @ %d:%d"
, targetFunctionName, c.getCellFormula(), formulasRow.getRowNum(), colnum);
String msg = String.format(Locale.ROOT, "Function '%s': Formula: %s @ %d:%d (%s)"
, targetFunctionName, c.getCellFormula(), formulasRow.getRowNum(), colnum,
new CellReference(formulasRow.getRowNum(), colnum).formatAsString());
assertNotNull(expValue, msg + " - Bad setup data expected value is null");
assertNotNull(actValue, msg + " - actual value was null");
final CellType cellType = expValue.getCellType();
msg += ", cellType: " + cellType + ", actCellType: " + actValue.getCellType() + ": " + actValue.formatAsString();
switch (cellType) {
case BLANK:
assertEquals(CellType.BLANK, actValue.getCellType(), msg);

View File

@ -18,8 +18,11 @@
package org.apache.poi.ss.formula.functions;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Sheet;
import org.junit.jupiter.api.Test;
import java.io.IOException;
@ -35,8 +38,8 @@ final class TestConcat {
@Test
void testConcatWithStrings() throws IOException {
try (HSSFWorkbook wb = initWorkbook1()) {
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(0);
FormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
Cell cell = wb.getSheetAt(0).getRow(0).createCell(0);
confirmResult(fe, cell, "CONCAT(\"The\",\" \",\"sun\",\" \",\"will\",\" \",\"come\",\" \",\"up\",\" \",\"tomorrow.\")",
"The sun will come up tomorrow.");
}
@ -45,8 +48,8 @@ final class TestConcat {
@Test
void testConcatWithColumns() throws IOException {
try (HSSFWorkbook wb = initWorkbook1()) {
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(0);
FormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
Cell cell = wb.getSheetAt(0).getRow(0).createCell(0);
confirmResult(fe, cell, "CONCAT(B:B, C:C)", "Asa1a2a4a5a6a7Bsb1b2b4b5b6b7");
}
}
@ -54,8 +57,8 @@ final class TestConcat {
@Test
void testConcatWithCellRanges() throws IOException {
try (HSSFWorkbook wb = initWorkbook1()) {
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(0);
FormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
Cell cell = wb.getSheetAt(0).getRow(0).createCell(0);
confirmResult(fe, cell, "CONCAT(B2:C8)", "a1b1a2b2a4b4a5b5a6b6a7b7");
}
}
@ -63,8 +66,8 @@ final class TestConcat {
@Test
void testConcatWithCellRefs() throws IOException {
try (HSSFWorkbook wb = initWorkbook2()) {
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
HSSFCell cell = wb.getSheetAt(0).createRow(5).createCell(0);
FormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
Cell cell = wb.getSheetAt(0).createRow(5).createCell(0);
confirmResult(fe, cell, "CONCAT(\"Stream population for \", A2,\" \", A3, \" is \", A4, \"/mile.\")",
"Stream population for brook trout species is 32/mile.");
confirmResult(fe, cell, "CONCAT(B2,\" \", C2)", "Andreas Hauser");
@ -76,7 +79,7 @@ final class TestConcat {
private HSSFWorkbook initWorkbook1() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
Sheet sheet = wb.createSheet();
addRow(sheet, 0, null, "As", "Bs");
for (int i = 1; i <= 7; i++) {
if (i != 3) {
@ -88,7 +91,7 @@ final class TestConcat {
private HSSFWorkbook initWorkbook2() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
Sheet sheet = wb.createSheet();
addRow(sheet, 0, "Data", "First Name", "Last name");
addRow(sheet, 1, "brook trout", "Andreas", "Hauser");
addRow(sheet, 2, "species", "Fourth", "Pine");
@ -96,7 +99,7 @@ final class TestConcat {
return wb;
}
private static void confirmResult(HSSFFormulaEvaluator fe, HSSFCell cell, String formulaText, String expectedResult) {
private static void confirmResult(FormulaEvaluator fe, Cell cell, String formulaText, String expectedResult) {
cell.setCellFormula(formulaText);
fe.notifyUpdateCell(cell);
CellValue result = fe.evaluate(cell);