mirror of https://github.com/apache/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
This commit is contained in:
parent
56a5f076e5
commit
8fccd72f9d
|
@ -1473,6 +1473,18 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
|
|||
return names.get(nameIndex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<HSSFName> getNames(String name) {
|
||||
List<HSSFName> nameList = new ArrayList<HSSFName>();
|
||||
for(HSSFName nr : names) {
|
||||
if(nr.getNameName().equals(name)) {
|
||||
nameList.add(nr);
|
||||
}
|
||||
}
|
||||
|
||||
return nameList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HSSFName getNameAt(int nameIndex) {
|
||||
int nNames = names.size();
|
||||
|
|
|
@ -351,6 +351,15 @@ public interface Workbook extends Closeable, Iterable<Sheet> {
|
|||
* @return the defined name with the specified name. <code>null</code> 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<? extends Name> getNames(String name);
|
||||
|
||||
/**
|
||||
* @param nameIndex position of the named range (0-based)
|
||||
* @return the defined name at the specified index
|
||||
|
|
|
@ -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<? extends Name> getNames(String name) {
|
||||
return _wb.getNames(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param nameIndex position of the named range (0-based)
|
||||
* @return the defined name at the specified index
|
||||
|
|
|
@ -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<PackagePart> mediaParts = getPackage().getPartsByName(Pattern.compile("/xl/media/.*?"));
|
||||
pictures = new ArrayList<XSSFPictureData>(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<XSSFName> getNames(String name) {
|
||||
List<XSSFName> names = new ArrayList<XSSFName>();
|
||||
for(XSSFName nr : namedRanges) {
|
||||
if(nr.getNameName().equals(name)) {
|
||||
names.add(nr);
|
||||
}
|
||||
}
|
||||
|
||||
return names;
|
||||
}
|
||||
|
||||
@Override
|
||||
public XSSFName getNameAt(int nameIndex) {
|
||||
int nNames = namedRanges.size();
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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<? extends Name> 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());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue