]> source.dussan.org Git - poi.git/commitdiff
github-55: NumberFormatException if XSSFName.setNameName is set with a long name...
authorJaven O'Neal <onealj@apache.org>
Fri, 19 May 2017 08:26:41 +0000 (08:26 +0000)
committerJaven O'Neal <onealj@apache.org>
Fri, 19 May 2017 08:26:41 +0000 (08:26 +0000)
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

src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFName.java

index 1e97f82753a505376bb0f52fb9d24c599045ca08..f72e10c0fd052c3d7bf16946dbac9f1c15844f07 100644 (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
             }
         }
         
index a188a11ed3aa4ec4d78aae0691b5c2ac0cf6d9e6..3989d74d6620962c16f2fe63b2d1d35336aa271a 100644 (file)
 
 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");
+    }
 }