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;
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;
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;
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);
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;
@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.");
}
@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");
}
}
@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");
}
}
@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");
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) {
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");
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);