From c392b4fa726fa2f8e457a73493cf95d4e45a04e2 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Sat, 19 Jul 2014 19:10:47 +0000 Subject: [PATCH] The Event User Model fake-workbook for formula evaluations needs to know about the sheet names git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1611946 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/eventusermodel/EventWorkbookBuilder.java | 9 ++++++++- .../poi/ss/formula/ptg/ExternSheetNameResolver.java | 11 ++++++++--- .../hssf/eventusermodel/TestEventWorkbookBuilder.java | 7 +++++-- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/java/org/apache/poi/hssf/eventusermodel/EventWorkbookBuilder.java b/src/java/org/apache/poi/hssf/eventusermodel/EventWorkbookBuilder.java index c6036d658..b0d0388a2 100644 --- a/src/java/org/apache/poi/hssf/eventusermodel/EventWorkbookBuilder.java +++ b/src/java/org/apache/poi/hssf/eventusermodel/EventWorkbookBuilder.java @@ -138,7 +138,14 @@ public class EventWorkbookBuilder { } public HSSFWorkbook getStubHSSFWorkbook() { - return HSSFWorkbook.create(getStubWorkbook()); + // Create a base workbook + HSSFWorkbook wb = HSSFWorkbook.create(getStubWorkbook()); + // Stub the sheets, so sheet name lookups work + for (BoundSheetRecord bsr : boundSheetRecords) { + wb.createSheet(bsr.getSheetname()); + } + // Ready for Formula use! + return wb; } public InternalWorkbook getStubWorkbook() { return createStubWorkbook( diff --git a/src/java/org/apache/poi/ss/formula/ptg/ExternSheetNameResolver.java b/src/java/org/apache/poi/ss/formula/ptg/ExternSheetNameResolver.java index 150bd99e7..be20167ed 100644 --- a/src/java/org/apache/poi/ss/formula/ptg/ExternSheetNameResolver.java +++ b/src/java/org/apache/poi/ss/formula/ptg/ExternSheetNameResolver.java @@ -17,8 +17,8 @@ package org.apache.poi.ss.formula.ptg; -import org.apache.poi.ss.formula.FormulaRenderingWorkbook; import org.apache.poi.ss.formula.EvaluationWorkbook.ExternalSheet; +import org.apache.poi.ss.formula.FormulaRenderingWorkbook; import org.apache.poi.ss.formula.SheetNameFormatter; /** @@ -36,8 +36,13 @@ final class ExternSheetNameResolver { if (externalSheet != null) { String wbName = externalSheet.getWorkbookName(); String sheetName = externalSheet.getSheetName(); - sb = new StringBuffer(wbName.length() + sheetName.length() + cellRefText.length() + 4); - SheetNameFormatter.appendFormat(sb, wbName, sheetName); + if (wbName != null) { + sb = new StringBuffer(wbName.length() + sheetName.length() + cellRefText.length() + 4); + SheetNameFormatter.appendFormat(sb, wbName, sheetName); + } else { + sb = new StringBuffer(sheetName.length() + cellRefText.length() + 4); + SheetNameFormatter.appendFormat(sb, sheetName); + } } else { String sheetName = book.getSheetNameByExternSheet(field_1_index_extern_sheet); sb = new StringBuffer(sheetName.length() + cellRefText.length() + 4); diff --git a/src/testcases/org/apache/poi/hssf/eventusermodel/TestEventWorkbookBuilder.java b/src/testcases/org/apache/poi/hssf/eventusermodel/TestEventWorkbookBuilder.java index 552afca9d..e7869755a 100644 --- a/src/testcases/org/apache/poi/hssf/eventusermodel/TestEventWorkbookBuilder.java +++ b/src/testcases/org/apache/poi/hssf/eventusermodel/TestEventWorkbookBuilder.java @@ -97,8 +97,11 @@ public final class TestEventWorkbookBuilder extends TestCase { // Check these stubs have the right stuff on them assertEquals("Sheet1", stubWB.getSheetName(0)); - assertEquals("S2", stubWB.getSheetName(1)); - assertEquals("Sh3", stubWB.getSheetName(2)); + assertEquals("Sheet1", stubHSSF.getSheetName(0)); + assertEquals("S2", stubWB.getSheetName(1)); + assertEquals("S2", stubHSSF.getSheetName(1)); + assertEquals("Sh3", stubWB.getSheetName(2)); + assertEquals("Sh3", stubHSSF.getSheetName(2)); // Check we can get the formula without breaking for(int i=0; i