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();
* @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
{
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
*/
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();
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);
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();
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;
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}.
*/
HSSFName namedRange1 = wb1.getNameAt(0);
//Getting it sheet name
sheetName = namedRange1.getSheetName();
+ assertNotNull(sheetName);
// sanity check
SanityChecker c = new SanityChecker();
workbook.close();
}
+ @SuppressWarnings("deprecation")
@Test
public void testDeletedReference() throws Exception {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("24207.xls");
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();
}
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}.
*
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());
+ }
}