From 2354bbb0b0060992310998a04f650df8a070dcb7 Mon Sep 17 00:00:00 2001 From: Javen O'Neal Date: Sat, 31 Oct 2015 11:39:01 +0000 Subject: [PATCH] supporting changes for bug 58452: copy cell formulas containing unregistered function names git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1711600 13f79535-47bb-0310-9956-ffa450edef68 --- .../usermodel/HSSFEvaluationWorkbook.java | 9 +++++++++ .../ss/formula/FormulaParsingWorkbook.java | 9 +++++++++ .../apache/poi/ss/formula/ptg/NameXPxg.java | 3 +++ .../usermodel/BaseXSSFEvaluationWorkbook.java | 19 ++++++++++++++++++- .../apache/poi/xssf/XSSFTestDataSamples.java | 3 +++ 5 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationWorkbook.java index 79a59627a..dbf85b869 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationWorkbook.java @@ -62,6 +62,11 @@ public final class HSSFEvaluationWorkbook implements FormulaRenderingWorkbook, E _uBook = book; _iBook = book.getWorkbook(); } + + @Override + public HSSFName createName() { + return _uBook.createName(); + } public int getExternalSheetIndex(String sheetName) { int sheetIndex = _uBook.getSheetIndex(sheetName); @@ -79,6 +84,10 @@ public final class HSSFEvaluationWorkbook implements FormulaRenderingWorkbook, E int extIx = getSheetExtIx(sheet); return new Area3DPtg(areaRef, extIx); } + /** + * Return an external name (named range, function, user-defined function) Ptg + */ + @Override public NameXPtg getNameXPtg(String name, SheetIdentifier sheet) { int sheetRefIndex = getSheetExtIx(sheet); return _iBook.getNameXPtg(name, sheetRefIndex, _uBook.getUDFFinder()); diff --git a/src/java/org/apache/poi/ss/formula/FormulaParsingWorkbook.java b/src/java/org/apache/poi/ss/formula/FormulaParsingWorkbook.java index a2be6f8f2..fe907fbb1 100644 --- a/src/java/org/apache/poi/ss/formula/FormulaParsingWorkbook.java +++ b/src/java/org/apache/poi/ss/formula/FormulaParsingWorkbook.java @@ -19,6 +19,7 @@ package org.apache.poi.ss.formula; import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.formula.ptg.Ptg; +import org.apache.poi.ss.usermodel.Name; import org.apache.poi.ss.util.AreaReference; import org.apache.poi.ss.util.CellReference; @@ -34,7 +35,15 @@ public interface FormulaParsingWorkbook { * named range name matching is case insensitive */ EvaluationName getName(String name, int sheetIndex); + + /** + * Return the underlying workbook + */ + Name createName(); + /** + * Return an external name (named range, function, user-defined function) Ptg + */ Ptg getNameXPtg(String name, SheetIdentifier sheet); /** diff --git a/src/java/org/apache/poi/ss/formula/ptg/NameXPxg.java b/src/java/org/apache/poi/ss/formula/ptg/NameXPxg.java index b1ca488c7..8969ad47b 100644 --- a/src/java/org/apache/poi/ss/formula/ptg/NameXPxg.java +++ b/src/java/org/apache/poi/ss/formula/ptg/NameXPxg.java @@ -40,6 +40,9 @@ public final class NameXPxg extends OperandPtg implements Pxg { public NameXPxg(String sheetName, String nameName) { this(-1, sheetName, nameName); } + public NameXPxg(String nameName) { + this(-1, null, nameName); + } public String toString(){ StringBuffer sb = new StringBuffer(); diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/BaseXSSFEvaluationWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/BaseXSSFEvaluationWorkbook.java index c8a7fae52..04b559ba1 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/BaseXSSFEvaluationWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/BaseXSSFEvaluationWorkbook.java @@ -130,6 +130,15 @@ public abstract class BaseXSSFEvaluationWorkbook implements FormulaRenderingWork } } + /** + * Return EvaluationName wrapper around the matching XSSFName (named range) + * @param name case-aware but case-insensitive named range in workbook + * @param sheetIndex index of sheet if named range scope is limited to one sheet + * if named range scope is global to the workbook, sheetIndex is -1. + * @return If name is a named range in the workbook, returns + * EvaluationName corresponding to that named range + * Returns null if there is no named range with the same name and scope in the workbook + */ public EvaluationName getName(String name, int sheetIndex) { for (int i = 0; i < _uBook.getNumberOfNames(); i++) { XSSFName nm = _uBook.getNameAt(i); @@ -137,7 +146,7 @@ public abstract class BaseXSSFEvaluationWorkbook implements FormulaRenderingWork int nameSheetindex = nm.getSheetIndex(); if (name.equalsIgnoreCase(nameText) && (nameSheetindex == -1 || nameSheetindex == sheetIndex)) { - return new Name(_uBook.getNameAt(i), i, this); + return new Name(nm, i, this); } } return sheetIndex == -1 ? null : getName(name, -1); @@ -179,6 +188,10 @@ public abstract class BaseXSSFEvaluationWorkbook implements FormulaRenderingWork } + /** + * Return an external name (named range, function, user-defined function) Pxg + */ + @Override public NameXPxg getNameXPtg(String name, SheetIdentifier sheet) { // First, try to find it as a User Defined Function IndexedUDFFinder udfFinder = (IndexedUDFFinder)getUDFFinder(); @@ -290,6 +303,10 @@ public abstract class BaseXSSFEvaluationWorkbook implements FormulaRenderingWork int ix = namePtg.getIndex(); return new Name(_uBook.getNameAt(ix), ix, this); } + @Override + public XSSFName createName() { + return _uBook.createName(); + } public UDFFinder getUDFFinder(){ return _uBook.getUDFFinder(); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/XSSFTestDataSamples.java b/src/ooxml/testcases/org/apache/poi/xssf/XSSFTestDataSamples.java index 7906759ac..d9dbfbfea 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/XSSFTestDataSamples.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/XSSFTestDataSamples.java @@ -44,6 +44,9 @@ public class XSSFTestDataSamples { */ public static final String TEST_OUTPUT_DIR = "poi.test.xssf.output.dir"; + public static File getSampleFile(String sampleFileName) { + return HSSFTestDataSamples.getSampleFile(sampleFileName); + } public static OPCPackage openSamplePackage(String sampleName) { try { return OPCPackage.open(