Skip to content

Commit

Permalink
[bug-63934] Fix parsing of structure references. Thanks to Matthias R…
Browse files Browse the repository at this point in the history
…aschhofer. This closes #514

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1912263 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
pjfanning committed Sep 12, 2023
1 parent e94297b commit ca681fd
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
Expand All @@ -32,6 +36,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import org.apache.poi.ss.usermodel.BaseTestFormulaEvaluator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
Expand Down Expand Up @@ -444,4 +449,17 @@ void testBug62834() throws IOException {
assertEquals("another value", value.getStringCellValue(), "wrong value A5");
}
}

@Test
void testBug63934() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("63934.xlsx")) {

final Cell cell = wb.getSheetAt(0).getRow(1).getCell(1);
assertNotNull(cell);

final FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
final CellValue value = evaluator.evaluate(cell);
assertEquals("Male", value.getStringValue());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ The following cases are tested (copied from FormulaParser.parseStructuredReferen
16 Table1[[#Headers],[#Data],[col2]]
17 Table1[[#This Row], [col1]]
18 Table1[ [col1]:[col2] ]
19 Table1[]
*/

final String tbl = "\\_Prime.1";
Expand Down Expand Up @@ -697,7 +698,7 @@ The following cases are tested (copied from FormulaParser.parseStructuredReferen
assertEquals(1, ptgs.length);
assertEquals("Table!A1:C7", ptgs[0].toFormulaString(), "Table1[#All]");

////// Case 5: Evaluate "Table1[#Data]" (excludes Header and Data rows) ////////
////// Case 5: Evaluate "Table1[#Data]" (excludes Header and Total rows) ////////
ptgs = parse(fpb, tbl+"[#Data]");
assertEquals(1, ptgs.length);
assertEquals("Table!A2:C7", ptgs[0].toFormulaString(), "Table1[#Data]");
Expand Down Expand Up @@ -790,6 +791,13 @@ The following cases are tested (copied from FormulaParser.parseStructuredReferen
assertEquals(1, ptgs.length);
assertEquals("Table!B2:C7", ptgs[0].toFormulaString(), "Table1[[col]:[col2]]");

////// Case 19: Evaluate "Table1[]" ////////
// Excludes Header and Total rows, equivalent to Table1[#Data] (see case 5).
// This is the only case where [] is allowed.
ptgs = parse(fpb, tbl+"[]");
assertEquals(1, ptgs.length);
assertEquals("Table!A2:C7", ptgs[0].toFormulaString(), "Table1[]");

wb.close();
}
}
24 changes: 24 additions & 0 deletions poi/src/main/java/org/apache/poi/ss/formula/FormulaParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ private ParseNode parseRangeable() {
* Parses a structured reference, returns it as area reference.
* Examples:
* <pre>
* Table1[]
* Table1[col]
* Table1[[#Totals],[col]]
* Table1[#Totals]
Expand Down Expand Up @@ -637,6 +638,29 @@ private ParseNode parseStructuredReference(String tableName) {
int savePtr0 = _pointer;
nextChar();

// Special case: Table1[] is equivalent to Table1[#Data]
if (look == ']') {

// Consume the ']' character
nextChar();

// Skip header and total rows if available
int actualStartRow = startRow;
if (tbl.getHeaderRowCount() > 0) {
actualStartRow = startRow + 1;
}
int actualEndRow = endRow;
if (tbl.getTotalsRowCount() > 0) {
actualEndRow = endRow - 1;
}

final CellReference topLeft = new CellReference(actualStartRow, startCol);
final CellReference bottomRight = new CellReference(actualEndRow, endCol);
final SheetIdentifier sheetIden = new SheetIdentifier( null, new NameIdentifier(sheetName, true));
final Ptg ptg = _book.get3DReferencePtg(new AreaReference(topLeft, bottomRight, _ssVersion), sheetIden);
return new ParseNode(ptg);
}

boolean isTotalsSpec = false;
boolean isThisRowSpec = false;
boolean isDataSpec = false;
Expand Down
Binary file added test-data/spreadsheet/63934.xlsx
Binary file not shown.

0 comments on commit ca681fd

Please sign in to comment.