diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java index 1e97f8275..f72e10c0f 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java @@ -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 } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFName.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFName.java index a188a11ed..3989d74d6 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFName.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFName.java @@ -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"); + } }