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 13fa8e7c9e Fix off-by-one when stripping the [] wrapper from an
external book name (#1245)
13fa8e7c9e is described below
commit 13fa8e7c9ef47a6bd0818ec96b329d3c91b620c3
Author: PJ Fanning <[email protected]>
AuthorDate: Fri Sep 4 00:08:52 2026 +0100
Fix off-by-one when stripping the [] wrapper from an external book name
(#1245)
BaseXSSFEvaluationWorkbook.resolveBookIndex used substring(1, length()-2)
to strip the [] wrapper from a book name, which also dropped the last
character of the name itself - "[Book1.xlsx]" became "Book1.xls". The
external links lookup then missed and, unless the name happened to look
like a 'file:///...' reference, the call failed with "Book not linked for
filename ...".
While in the same method:
* don't depend on FormulaParser leaving the single quotes around a quoted
book name in place - accept an absolute file reference with or without
them;
* guard findExternalLinkIndex against a null table list, which
XSSFWorkbook.getExternalLinksTables() can return, and re-fetch the list
after adding a placeholder rather than assuming the one we looked up is
live.
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
---
.../xssf/usermodel/BaseXSSFEvaluationWorkbook.java | 29 ++++++++++--
.../xssf/usermodel/TestXSSFEvaluationWorkbook.java | 53 ++++++++++++++++++++++
2 files changed, 77 insertions(+), 5 deletions(-)
diff --git
a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/BaseXSSFEvaluationWorkbook.java
b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/BaseXSSFEvaluationWorkbook.java
index 5621863c8b..7bc4ddd54b 100644
---
a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/BaseXSSFEvaluationWorkbook.java
+++
b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/BaseXSSFEvaluationWorkbook.java
@@ -101,7 +101,7 @@ public abstract class BaseXSSFEvaluationWorkbook implements
FormulaRenderingWork
private int resolveBookIndex(String bookName) {
// Strip the [] wrapper, if still present
if (bookName.startsWith("[") && bookName.endsWith("]")) {
- bookName = bookName.substring(1, bookName.length()-2);
+ bookName = bookName.substring(1, bookName.length()-1);
}
// Is it already in numeric form?
@@ -115,9 +115,9 @@ public abstract class BaseXSSFEvaluationWorkbook implements
FormulaRenderingWork
if (index != -1) return index;
// Is it an absolute file reference?
- if (bookName.startsWith("'file:///") && bookName.endsWith("'")) {
- String relBookName =
bookName.substring(bookName.lastIndexOf('/')+1);
- relBookName = relBookName.substring(0, relBookName.length()-1); //
Trailing '
+ String fileRef = asFileReference(bookName);
+ if (fileRef != null) {
+ String relBookName = fileRef.substring(fileRef.lastIndexOf('/')+1);
// Try with this name
index = findExternalLinkIndex(relBookName, tables);
@@ -128,14 +128,33 @@ public abstract class BaseXSSFEvaluationWorkbook
implements FormulaRenderingWork
// Note - this is really rather nasty...
ExternalLinksTable fakeLinkTable = new
FakeExternalLinksTable(relBookName);
_uBook.addExternalLinksTable(fakeLinkTable);
- return tables.size(); // 1 based results, 0 = current workbook
+ // 1 based results, 0 = current workbook. Re-fetch rather than
trusting the
+ // list we looked up above to be the live one (it may also have
been null)
+ return _uBook.getExternalLinksTables().size();
}
// Not properly referenced
throw new IllegalStateException("Book not linked for filename " +
bookName);
}
+ /**
+ * The formula parser leaves the single quotes around a quoted book name
in place, but
+ * don't rely on them being there - callers may hand us an unquoted name
just as easily.
+ *
+ * @return the file reference with any surrounding quotes removed, or
{@code null} if
+ * {@code bookName} isn't an absolute file reference
+ */
+ private static String asFileReference(String bookName) {
+ String name = bookName;
+ if (name.length() > 1 && name.startsWith("'") && name.endsWith("'")) {
+ name = name.substring(1, name.length()-1);
+ }
+ return name.startsWith("file:///") ? name : null;
+ }
/* This is case-sensitive. Is that correct? */
private int findExternalLinkIndex(String bookName,
List<ExternalLinksTable> tables) {
+ if (tables == null) {
+ return -1;
+ }
int i = 0;
for (ExternalLinksTable table : tables) {
if (table.getLinkedFileName().equals(bookName)) {
diff --git
a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFEvaluationWorkbook.java
b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFEvaluationWorkbook.java
index b0bdac458a..a912c788a3 100644
---
a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFEvaluationWorkbook.java
+++
b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFEvaluationWorkbook.java
@@ -19,12 +19,19 @@ package org.apache.poi.xssf.usermodel;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import java.io.IOException;
+
+import org.apache.poi.ss.formula.NameIdentifier;
+import org.apache.poi.ss.formula.SheetIdentifier;
+import org.apache.poi.ss.formula.ptg.Ref3DPxg;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.ss.util.CellReference;
+import org.apache.poi.xssf.XSSFTestDataSamples;
import org.junit.jupiter.api.Test;
class TestXSSFEvaluationWorkbook {
@@ -77,4 +84,50 @@ class TestXSSFEvaluationWorkbook {
assertEquals("3",cellC3.getStringCellValue());
}
+ @Test
+ void testResolveBookIndexWithBracketedName() throws IOException {
+ try (XSSFWorkbook wb =
XSSFTestDataSamples.openSampleWorkbook("ref-56737.xlsx")) {
+ XSSFEvaluationWorkbook ewb = XSSFEvaluationWorkbook.create(wb);
+ SheetIdentifier sheet = new SheetIdentifier("[56737.xlsx]", new
NameIdentifier("Uses", false));
+
+ Ref3DPxg ptg = (Ref3DPxg) ewb.get3DReferencePtg(new
CellReference("A1"), sheet);
+ // 1 based results, 0 = current workbook
+ assertEquals(1, ptg.getExternalWorkbookNumber());
+ }
+ }
+
+ @Test
+ void testResolveBookIndexForQuotedFileReference() throws IOException {
+ try (XSSFWorkbook wb = new XSSFWorkbook()) {
+ XSSFEvaluationWorkbook ewb = XSSFEvaluationWorkbook.create(wb);
+ SheetIdentifier sheet = new
SheetIdentifier("'file:///C:/temp/Book1.xlsx'",
+ new NameIdentifier("Sheet1", false));
+
+ // no link table for this file yet, so a placeholder one is added
+ Ref3DPxg ptg = (Ref3DPxg) ewb.get3DReferencePtg(new
CellReference("A1"), sheet);
+ assertEquals(1, ptg.getExternalWorkbookNumber());
+ assertEquals(1, wb.getExternalLinksTables().size());
+ assertEquals("Book1.xlsx",
wb.getExternalLinksTable(0).getLinkedFileName());
+
+ // asking again finds the placeholder rather than adding a second
one
+ ptg = (Ref3DPxg) ewb.get3DReferencePtg(new CellReference("A1"),
sheet);
+ assertEquals(1, ptg.getExternalWorkbookNumber());
+ assertEquals(1, wb.getExternalLinksTables().size());
+ }
+ }
+
+ @Test
+ void testResolveBookIndexForUnquotedFileReference() throws IOException {
+ try (XSSFWorkbook wb = new XSSFWorkbook()) {
+ XSSFEvaluationWorkbook ewb = XSSFEvaluationWorkbook.create(wb);
+ // the formula parser leaves the quotes in place, but we shouldn't
depend on that
+ SheetIdentifier sheet = new
SheetIdentifier("file:///C:/temp/Book1.xlsx",
+ new NameIdentifier("Sheet1", false));
+
+ Ref3DPxg ptg = (Ref3DPxg) ewb.get3DReferencePtg(new
CellReference("A1"), sheet);
+ assertEquals(1, ptg.getExternalWorkbookNumber());
+ assertEquals("Book1.xlsx",
wb.getExternalLinksTable(0).getLinkedFileName());
+ }
+ }
+
}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]