From af0406cd100de823fb5de84ae9fa4d54edf46932 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Mon, 26 Jul 2010 15:52:57 +0000 Subject: [PATCH] Fixed parsing formulas containing defined names beginning with an underscore, see Bug 9640 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@979329 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../apache/poi/ss/formula/FormulaParser.java | 4 +++- .../poi/hssf/usermodel/TestFormulas.java | 23 +++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index d6fe9299e5..043a23cd07 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 49640 - Fixed parsing formulas containing defined names beginning with an underscore 49538 - Added implementation for POISSON() 49524 - Support for setting cell text to be vertically rotated, via style.setRotation(0xff) 49609 - Case insensitive matching of OOXML part names diff --git a/src/java/org/apache/poi/ss/formula/FormulaParser.java b/src/java/org/apache/poi/ss/formula/FormulaParser.java index 9e63e6f4b9..44c8736436 100644 --- a/src/java/org/apache/poi/ss/formula/FormulaParser.java +++ b/src/java/org/apache/poi/ss/formula/FormulaParser.java @@ -52,6 +52,7 @@ import org.apache.poi.ss.util.CellReference.NameType; * @author Peter M. Murray (pete at quantrix dot com) * @author Pavel Krupets (pkrupets at palmtreebusiness dot com) * @author Josh Micich + * @author David Lewis (DLewis400 at gmail dot com) */ public final class FormulaParser { private static final class Identifier { @@ -537,7 +538,8 @@ public final class FormulaParser { // which will either be named ranges or functions StringBuilder sb = new StringBuilder(); - if (!Character.isLetter(look)) { + // defined names may begin with a letter or underscore + if (!Character.isLetter(look) && look != '_') { throw expected("number, string, or defined name"); } while (isValidDefinedNameChar(look)) { diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java b/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java index 3e2feaed02..4e2bb2302a 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java @@ -31,6 +31,9 @@ import org.apache.poi.hssf.record.formula.NamePtg; import org.apache.poi.hssf.util.CellReference; import org.apache.poi.util.TempFile; import org.apache.poi.ss.formula.FormulaType; +import org.apache.poi.ss.usermodel.Name; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Cell; /** * @author Andrew C. Oliver (acoliver at apache dot org) @@ -935,4 +938,24 @@ public final class TestFormulas extends TestCase { assertEquals(5.0, evaluator.evaluate(sh2.getRow(0).getCell(1)).getNumberValue(), 0.0); assertEquals(15.0, evaluator.evaluate(sh2.getRow(0).getCell(2)).getNumberValue(), 0.0); } + + /** + * Verify that FormulaParser handles defined names beginning with underscores, + * see Bug #49640 + */ + public void testFormulasWithUnderscore(){ + HSSFWorkbook wb = new HSSFWorkbook(); + Name nm1 = wb.createName(); + nm1.setNameName("_score1"); + nm1.setRefersToFormula("A1"); + + Name nm2 = wb.createName(); + nm2.setNameName("_score2"); + nm2.setRefersToFormula("A2"); + + Sheet sheet = wb.createSheet(); + Cell cell = sheet.createRow(0).createCell(2); + cell.setCellFormula("_score1*SUM(_score1+_score2)"); + assertEquals("_score1*SUM(_score1+_score2)", cell.getCellFormula()); + } } -- 2.39.5