diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 8ddac5f12e..8ef8414dbb 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 50718 - More helpful error message when you try to create a CellReference with #REF! 50784 - XSSFColors return by XSSFFont now have theme information applied to them 50846 - Improve how XSSFColor inherits from Themes 50847 - XSSFFont now accepts the full range of Charsets from FontChartset diff --git a/src/java/org/apache/poi/ss/util/CellReference.java b/src/java/org/apache/poi/ss/util/CellReference.java index 67f8fa0b15..12120b7489 100644 --- a/src/java/org/apache/poi/ss/util/CellReference.java +++ b/src/java/org/apache/poi/ss/util/CellReference.java @@ -85,6 +85,10 @@ public class CellReference { * delimited and escaped as per normal syntax rules for formulas. */ public CellReference(String cellRef) { + if(cellRef.endsWith("#REF!")) { + throw new IllegalArgumentException("Cell reference invalid: " + cellRef); + } + String[] parts = separateRefParts(cellRef); _sheetName = parts[0]; String colRef = parts[1]; @@ -335,7 +339,6 @@ public class CellReference { * name still in ALPHA-26 number format. The third element is the row. */ private static String[] separateRefParts(String reference) { - int plingPos = reference.lastIndexOf(SHEET_NAME_DELIMITER); String sheetName = parseSheetName(reference, plingPos); int start = plingPos+1; diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFName.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFName.java index 45da3e7e9c..3a61f3cd3f 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFName.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFName.java @@ -231,7 +231,7 @@ public final class TestHSSFName extends BaseTestNamedRange { try { new AreaReference(name2.getRefersToFormula()); fail("attempt to supply an invalid reference to AreaReference constructor results in exception"); - } catch (StringIndexOutOfBoundsException e) { // TODO - use a different exception for this condition + } catch (IllegalArgumentException e) { // TODO - use a stronger typed exception for this condition // expected during successful test } } diff --git a/src/testcases/org/apache/poi/ss/util/TestCellReference.java b/src/testcases/org/apache/poi/ss/util/TestCellReference.java index 4c86c0f740..c6ad609c54 100644 --- a/src/testcases/org/apache/poi/ss/util/TestCellReference.java +++ b/src/testcases/org/apache/poi/ss/util/TestCellReference.java @@ -192,6 +192,23 @@ public final class TestCellReference extends TestCase { confirmCrInRange(false, "A", "0", v97); confirmCrInRange(false, "A", "0", v2007); } + + public void testInvalidReference() { + try { + new CellReference("Sheet1!#REF!"); + fail("Shouldn't be able to create a #REF! refence"); + } catch(IllegalArgumentException e) {} + + try { + new CellReference("'MySheetName'!#REF!"); + fail("Shouldn't be able to create a #REF! refence"); + } catch(IllegalArgumentException e) {} + + try { + new CellReference("#REF!"); + fail("Shouldn't be able to create a #REF! refence"); + } catch(IllegalArgumentException e) {} + } private static void confirmCrInRange(boolean expResult, String colStr, String rowStr, SpreadsheetVersion sv) {