You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by jo...@apache.org on 2008/08/08 22:14:24 UTC
svn commit: r684075 - in /poi/trunk/src:
java/org/apache/poi/hssf/model/FormulaParser.java
testcases/org/apache/poi/hssf/model/TestFormulaParser.java
Author: josh
Date: Fri Aug 8 13:14:24 2008
New Revision: 684075
URL: http://svn.apache.org/viewvc?rev=684075&view=rev
Log:
improved error message for FormulaParser when the formula has a leading equals sign
Modified:
poi/trunk/src/java/org/apache/poi/hssf/model/FormulaParser.java
poi/trunk/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java
Modified: poi/trunk/src/java/org/apache/poi/hssf/model/FormulaParser.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/model/FormulaParser.java?rev=684075&r1=684074&r2=684075&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/model/FormulaParser.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/model/FormulaParser.java Fri Aug 8 13:14:24 2008
@@ -20,7 +20,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
-import java.util.regex.Pattern;
//import PTGs .. since we need everything, import *
import org.apache.poi.hssf.record.formula.*;
@@ -137,9 +136,16 @@
/** Report What Was Expected */
private RuntimeException expected(String s) {
- String msg = "Parse error near char " + (pointer-1) + " '" + look + "'"
- + " in specified formula '" + formulaString + "'. Expected "
- + s;
+ String msg;
+
+ if (look == '=' && formulaString.substring(0, pointer-1).trim().length() < 1) {
+ msg = "The specified formula '" + formulaString
+ + "' starts with an equals sign which is not allowed.";
+ } else {
+ msg = "Parse error near char " + (pointer-1) + " '" + look + "'"
+ + " in specified formula '" + formulaString + "'. Expected "
+ + s;
+ }
return new FormulaParseException(msg);
}
Modified: poi/trunk/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java?rev=684075&r1=684074&r2=684075&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java Fri Aug 8 13:14:24 2008
@@ -156,10 +156,10 @@
HSSFRow row = sheet.createRow(0);
HSSFCell cell;
- cell = row.createCell((short)0);
+ cell = row.createCell(0);
cell.setCellFormula("NoQuotesNeeded!A1");
- cell = row.createCell((short)1);
+ cell = row.createCell(1);
cell.setCellFormula("'Quotes Needed Here &#$@'!A1");
}
@@ -226,7 +226,7 @@
HSSFRow row = sheet.createRow(0);
HSSFCell cell;
- cell = row.createCell((short)0);
+ cell = row.createCell(0);
cell.setCellFormula("Cash_Flow!A1");
}
@@ -259,7 +259,7 @@
HSSFSheet sheet = wb.createSheet("Test");
HSSFRow row = sheet.createRow(0);
- HSSFCell cell = row.createCell((short)0);
+ HSSFCell cell = row.createCell(0);
String formula = null;
cell.setCellFormula("1.3E21/3");
@@ -330,7 +330,7 @@
HSSFSheet sheet = wb.createSheet("Test");
HSSFRow row = sheet.createRow(0);
- HSSFCell cell = row.createCell((short)0);
+ HSSFCell cell = row.createCell(0);
String formula = null;
// starts from decimal point
@@ -369,7 +369,7 @@
HSSFSheet sheet = wb.createSheet("Test");
HSSFRow row = sheet.createRow(0);
- HSSFCell cell = row.createCell((short)0);
+ HSSFCell cell = row.createCell(0);
String formula = null;
cell.setCellFormula("A1.A2");
@@ -566,6 +566,7 @@
confirmParseErrorLiteral(ErrPtg.NAME_INVALID, "#NAME?");
confirmParseErrorLiteral(ErrPtg.NUM_ERROR, "#NUM!");
confirmParseErrorLiteral(ErrPtg.N_A, "#N/A");
+ parseFormula("HLOOKUP(F7,#REF!,G7,#REF!)");
}
private static void confirmParseErrorLiteral(ErrPtg expectedToken, String formula) {
@@ -603,7 +604,7 @@
wb.setSheetName(0, "Sheet1");
HSSFRow row = sheet.createRow(0);
- HSSFCell cell = row.createCell((short)0);
+ HSSFCell cell = row.createCell(0);
cell.setCellFormula("right(\"test\"\"ing\", 3)");
String actualCellFormula = cell.getCellFormula();
if("RIGHT(\"test\"ing\",3)".equals(actualCellFormula)) {
@@ -670,7 +671,7 @@
wb.setSheetName(0, "Sheet1");
HSSFRow row = sheet.createRow(0);
- HSSFCell cell = row.createCell((short)0);
+ HSSFCell cell = row.createCell(0);
cell.setCellFormula("SUM(A32769:A32770)");
if("SUM(A-32767:A-32766)".equals(cell.getCellFormula())) {
fail("Identified bug 44539");
@@ -768,6 +769,13 @@
} catch (FormulaParseException e) {
assertEquals("Parse error near char 10 ';' in specified formula 'round(3.14;2)'. Expected ',' or ')'", e.getMessage());
}
+
+ try {
+ parseFormula(" =2+2");
+ throw new AssertionFailedError("Didn't get parse exception as expected");
+ } catch (FormulaParseException e) {
+ assertEquals("The specified formula ' =2+2' starts with an equals sign which is not allowed.", e.getMessage());
+ }
}
/**
@@ -811,7 +819,7 @@
assertEquals(2, ptgs.length);
assertEquals(NamePtg.class, ptgs[0].getClass());
- HSSFCell cell = sheet.createRow(0).createCell((short)0);
+ HSSFCell cell = sheet.createRow(0).createCell(0);
cell.setCellFormula("count(pfy1)");
assertEquals("COUNT(pfy1)", cell.getCellFormula());
try {
@@ -823,6 +831,5 @@
}
}
cell.setCellFormula("count(fp1)"); // plain cell ref, col is in range
-
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org