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
}
}
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
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");
+ }
}