This is an automated email from the ASF dual-hosted git repository.
pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git
The following commit(s) were added to refs/heads/trunk by this push:
new ef728ee036 Fix `findColumnIndex()` to handle newline characters in
column headers (#1234)
ef728ee036 is described below
commit ef728ee0367411839e2d4cba01032088d48e9f4f
Author: Brad Wood <[email protected]>
AuthorDate: Tue Sep 1 06:24:43 2026 -0500
Fix `findColumnIndex()` to handle newline characters in column headers
(#1234)
Fix and tests
---
.../org/apache/poi/xssf/usermodel/XSSFTable.java | 9 ++++-
.../apache/poi/xssf/usermodel/TestXSSFTable.java | 47 ++++++++++++++++++++++
2 files changed, 54 insertions(+), 2 deletions(-)
diff --git
a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTable.java
b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTable.java
index d94545a7d6..e7c3fec655 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTable.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTable.java
@@ -877,10 +877,15 @@ public class XSSFTable extends POIXMLDocumentPart
implements Table {
}
}
// Table column names with special characters need a single quote
escape
- // but the escape is not present in the column definition
+ // but the escape is not present in the column definition.
+ // Also replace newline and carriage return characters with their
_x000a_
+ // and _x000d_ escape sequences to match the encoding applied by
+ // updateHeaders(), which uses the OOXML _xXXXX_ escape convention.
String unescapedString = columnHeader
.replace("''", "'")
- .replace("'#", "#");
+ .replace("'#", "#")
+ .replace("\n", "_x000a_")
+ .replace("\r", "_x000d_");
Integer idx = columnMap.get(unescapedString);
return idx == null ? -1 : idx;
}
diff --git
a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFTable.java
b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFTable.java
index 298f8084e0..8d3f45f258 100644
--- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFTable.java
+++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFTable.java
@@ -29,6 +29,9 @@ import
org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
+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.Row;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;
@@ -709,6 +712,50 @@ public final class TestXSSFTable {
List<XSSFTableColumn> tabColumns = wb2Table.getColumns();
assertEquals(2, tabColumns.size());
assertEquals("Column1_x000a_with a line break",
tabColumns.get(0).getName());
+ assertEquals(0, wb2Table.findColumnIndex("Column1\nwith a line
break"));
+ }
+ }
+ }
+
+ @Test
+ void testFormulaWithNewlineColumnName() throws IOException {
+ try (
+ XSSFWorkbook wb = new XSSFWorkbook();
+ UnsynchronizedByteArrayOutputStream bos =
UnsynchronizedByteArrayOutputStream.builder().get()
+ ) {
+ XSSFSheet sheet = wb.createSheet();
+ XSSFRow headersRow = sheet.createRow(0);
+ headersRow.createCell(0).setCellValue("Value\nA");
+ headersRow.createCell(1).setCellValue("Value\nB");
+
+ XSSFRow row1 = sheet.createRow(1);
+ row1.createCell(0).setCellValue(100);
+ row1.createCell(1).setCellValue(50);
+
+ XSSFRow row2 = sheet.createRow(2);
+ row2.createCell(0).setCellValue(200);
+ row2.createCell(1).setCellValue(75);
+
+ AreaReference area = wb.getCreationHelper().createAreaReference(
+ new CellReference(0, 0), new CellReference(2, 1));
+ XSSFTable table = sheet.createTable(area);
+ table.setName("Table1");
+
+ // write and reload so updateHeaders() encodes \n to _x000a_
+ wb.write(bos);
+ try (XSSFWorkbook wb2 = new XSSFWorkbook(bos.toInputStream())) {
+ XSSFSheet wb2Sheet = wb2.getSheetAt(0);
+ XSSFTable wb2Table = wb2Sheet.getTables().get(0);
+
+ assertEquals(0, wb2Table.findColumnIndex("Value\nA"));
+
+ XSSFRow formulaRow = wb2Sheet.createRow(3);
+ XSSFCell formulaCell = formulaRow.createCell(0,
CellType.FORMULA);
+ formulaCell.setCellFormula("SUM(Table1[Value\nA])");
+
+ FormulaEvaluator evaluator =
wb2.getCreationHelper().createFormulaEvaluator();
+ CellValue value = evaluator.evaluate(formulaCell);
+ assertEquals(300.0, value.getNumberValue(), 0.001);
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]