From: Dominik Stadler Date: Sun, 13 Mar 2016 21:30:38 +0000 (+0000) Subject: Bug 56930: Add Workbook.getNames() to allow to query for names that appear multiple... X-Git-Tag: REL_3_15_BETA2~449 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=8fccd72f9d0e98569a3f4ef6aae0daff83072b17;p=poi.git Bug 56930: Add Workbook.getNames() to allow to query for names that appear multiple times git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1734863 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index 5f179f032e..20f6c37c58 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -1473,6 +1473,18 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss return names.get(nameIndex); } + @Override + public List getNames(String name) { + List nameList = new ArrayList(); + for(HSSFName nr : names) { + if(nr.getNameName().equals(name)) { + nameList.add(nr); + } + } + + return nameList; + } + @Override public HSSFName getNameAt(int nameIndex) { int nNames = names.size(); diff --git a/src/java/org/apache/poi/ss/usermodel/Workbook.java b/src/java/org/apache/poi/ss/usermodel/Workbook.java index 2392ad3db6..df754a3b6e 100644 --- a/src/java/org/apache/poi/ss/usermodel/Workbook.java +++ b/src/java/org/apache/poi/ss/usermodel/Workbook.java @@ -351,6 +351,15 @@ public interface Workbook extends Closeable, Iterable { * @return the defined name with the specified name. null if not found. */ Name getName(String name); + + /** + * Returns all defined names with the given name. + * + * @param name the name of the defined name + * @return a list of the defined names with the specified name. An empty list is returned if none is found. + */ + List getNames(String name); + /** * @param nameIndex position of the named range (0-based) * @return the defined name at the specified index diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java index 3342fb1b8f..d5befb6153 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java @@ -956,6 +956,18 @@ public class SXSSFWorkbook implements Workbook { { return _wb.getName(name); } + + /** + * Returns all defined names with the given name. + * + * @param name the name of the defined name + * @return a list of the defined names with the specified name. An empty list is returned if none is found. + */ + @Override + public List getNames(String name) { + return _wb.getNames(name); + } + /** * @param nameIndex position of the named range (0-based) * @return the defined name at the specified index diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index f09313524d..8bd34905b6 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -522,7 +522,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { */ public int addPicture(InputStream is, int format) throws IOException { int imageNumber = getAllPictures().size() + 1; - XSSFPictureData img = (XSSFPictureData)createRelationship(XSSFPictureData.RELATIONS[format], XSSFFactory.getInstance(), imageNumber, true).getDocumentPart(); + XSSFPictureData img = createRelationship(XSSFPictureData.RELATIONS[format], XSSFFactory.getInstance(), imageNumber, true).getDocumentPart(); OutputStream out = img.getPackagePart().getOutputStream(); IOUtils.copy(is, out); out.close(); @@ -872,7 +872,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { List mediaParts = getPackage().getPartsByName(Pattern.compile("/xl/media/.*?")); pictures = new ArrayList(mediaParts.size()); for(PackagePart part : mediaParts){ - pictures.add(new XSSFPictureData(part, null)); + pictures.add(new XSSFPictureData(part)); } } return pictures; //YK: should return Collections.unmodifiableList(pictures); @@ -908,6 +908,18 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { return namedRanges.get(nameIndex); } + @Override + public List getNames(String name) { + List names = new ArrayList(); + for(XSSFName nr : namedRanges) { + if(nr.getNameName().equals(name)) { + names.add(nr); + } + } + + return names; + } + @Override public XSSFName getNameAt(int nameIndex) { int nNames = namedRanges.size(); diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFName.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFName.java index 49a925c132..16333fc67b 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFName.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFName.java @@ -17,10 +17,6 @@ package org.apache.poi.hssf.usermodel; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - import org.apache.poi.POITestCase; import org.apache.poi.hssf.HSSFITestDataProvider; import org.apache.poi.hssf.HSSFTestDataSamples; @@ -33,6 +29,8 @@ import org.apache.poi.ss.util.AreaReference; import org.apache.poi.ss.util.CellRangeAddress; import org.junit.Test; +import static org.junit.Assert.*; + /** * Tests various functionality having to do with {@link org.apache.poi.ss.usermodel.Name}. */ @@ -119,6 +117,7 @@ public final class TestHSSFName extends BaseTestNamedRange { HSSFName namedRange1 = wb1.getNameAt(0); //Getting it sheet name sheetName = namedRange1.getSheetName(); + assertNotNull(sheetName); // sanity check SanityChecker c = new SanityChecker(); @@ -198,6 +197,7 @@ public final class TestHSSFName extends BaseTestNamedRange { workbook.close(); } + @SuppressWarnings("deprecation") @Test public void testDeletedReference() throws Exception { HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("24207.xls"); @@ -233,8 +233,8 @@ public final class TestHSSFName extends BaseTestNamedRange { wb.createSheet("CSCO"); Ptg[] ptgs = HSSFFormulaParser.parse("CSCO!$E$71", wb, FormulaType.NAMEDRANGE, 0); - for (int i = 0; i < ptgs.length; i++) { - assertEquals('R', ptgs[i].getRVAType()); + for (Ptg ptg : ptgs) { + assertEquals('R', ptg.getRVAType()); } wb.close(); } diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestNamedRange.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestNamedRange.java index 0e05490106..66c783b46f 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestNamedRange.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestNamedRange.java @@ -29,6 +29,8 @@ import org.apache.poi.ss.util.AreaReference; import org.apache.poi.ss.util.CellReference; import org.junit.Test; +import java.util.List; + /** * Tests of implementations of {@link org.apache.poi.ss.usermodel.Name}. * @@ -643,4 +645,33 @@ public abstract class BaseTestNamedRange { wb.close(); } + + @Test + public void testBug56930() { + Workbook wb = _testDataProvider.createWorkbook(); + + // x1 on sheet1 defines "x=1" + wb.createSheet("sheet1"); + Name x1 = wb.createName(); + + x1.setNameName("x"); + x1.setRefersToFormula("1"); + x1.setSheetIndex(wb.getSheetIndex("sheet1")); + + // x2 on sheet2 defines "x=2" + wb.createSheet("sheet2"); + Name x2 = wb.createName(); + x2.setNameName("x"); + x2.setRefersToFormula("2"); + x2.setSheetIndex(wb.getSheetIndex("sheet2")); + + List names = wb.getNames("x"); + assertEquals("Had: " + names, 2, names.size()); + assertEquals("1", names.get(0).getRefersToFormula()); + assertEquals("2", names.get(1).getRefersToFormula()); + + assertEquals("1", wb.getName("x").getRefersToFormula()); + wb.removeName("x"); + assertEquals("2", wb.getName("x").getRefersToFormula()); + } }