github-55: NumberFormatException if XSSFName.setNameName is set with a long name that looks similar to a cell address. Thanks to Thomas S @Millie4Ever

This closes #55 on github.
https://github.com/apache/poi/pull/55

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1795595 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2017-05-19 08:26:41 +00:00
parent 0b767886bf
commit 284f7d19c5
2 changed files with 37 additions and 6 deletions

View File

@ -402,8 +402,14 @@ public final class XSSFName implements Name {
if (name.matches("[A-Za-z]+\\d+")) {
String col = name.replaceAll("\\d", "");
String row = name.replaceAll("[A-Za-z]", "");
if (CellReference.cellReferenceIsWithinRange(col, row, SpreadsheetVersion.EXCEL97)) {
throw new IllegalArgumentException("Invalid name: '"+name+"': cannot be $A$1-style cell reference");
try {
if (CellReference.cellReferenceIsWithinRange(col, row, SpreadsheetVersion.EXCEL2007)) {
throw new IllegalArgumentException("Invalid name: '"+name+"': cannot be $A$1-style cell reference");
}
} catch (final NumberFormatException e) {
// row was not parseable as an Integer, such as a BigInt
// therefore name passes the not-a-cell-reference criteria
}
}

View File

@ -17,14 +17,16 @@
package org.apache.poi.xssf.usermodel;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.junit.Test;
import org.apache.poi.xssf.XSSFITestDataProvider;
import static org.junit.Assert.*;
import java.io.IOException;
import java.util.Arrays;
import org.junit.Test;
import org.apache.poi.ss.usermodel.BaseTestNamedRange;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.XSSFITestDataProvider;
/**
* @author Yegor Kozlov
@ -130,4 +132,27 @@ public final class TestXSSFName extends BaseTestNamedRange {
wb.close();
}
//github-55
@Test
public void testSetNameNameCellAddress() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
wb.createSheet("First Sheet");
XSSFName name = wb.createName();
// Cell addresses/references are not allowed
for (String ref : Arrays.asList("A1", "$A$1", "A1:B2")) {
try {
name.setNameName(ref);
fail("cell addresses are not allowed: " + ref);
} catch (final IllegalArgumentException e) {
// expected
}
}
// Name that looks similar to a cell reference but is outside the cell reference row and column limits
name.setNameName("A0");
name.setNameName("F04030020010");
name.setNameName("XFDXFD10");
}
}