From: Dominik Stadler Date: Sun, 3 Apr 2022 13:25:27 +0000 (+0000) Subject: Various smaller changes X-Git-Tag: REL_5_2_3~375 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=0ee8c135c422a58384225cf5fb09f1a826b817f7;p=poi.git 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 --- diff --git a/poi/src/main/java/org/apache/poi/ss/formula/FormulaParser.java b/poi/src/main/java/org/apache/poi/ss/formula/FormulaParser.java index 00cd8fa1e6..4e7f1db0e9 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/FormulaParser.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/FormulaParser.java @@ -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); } diff --git a/poi/src/main/java/org/apache/poi/ss/formula/LazyAreaEval.java b/poi/src/main/java/org/apache/poi/ss/formula/LazyAreaEval.java index 583ffacccc..99e869a95c 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/LazyAreaEval.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/LazyAreaEval.java @@ -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()) { diff --git a/poi/src/main/java/org/apache/poi/ss/util/AreaReference.java b/poi/src/main/java/org/apache/poi/ss/util/AreaReference.java index ff9482c25f..6ccb17852a 100644 --- a/poi/src/main/java/org/apache/poi/ss/util/AreaReference.java +++ b/poi/src/main/java/org/apache/poi/ss/util/AreaReference.java @@ -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; diff --git a/poi/src/test/java/org/apache/poi/ss/formula/eval/TestFormulasFromSpreadsheet.java b/poi/src/test/java/org/apache/poi/ss/formula/eval/TestFormulasFromSpreadsheet.java index 4c22725b1e..2e814d9c2b 100644 --- a/poi/src/test/java/org/apache/poi/ss/formula/eval/TestFormulasFromSpreadsheet.java +++ b/poi/src/test/java/org/apache/poi/ss/formula/eval/TestFormulasFromSpreadsheet.java @@ -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); diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestConcat.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestConcat.java index 84f19207fa..ef0f366577 100644 --- a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestConcat.java +++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestConcat.java @@ -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)", "A’sa1a2a4a5a6a7B’sb1b2b4b5b6b7"); } } @@ -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, "A’s", "B’s"); 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);