From 8f4aff0e1a8b0e8430dd840828c097898a8eb731 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Thu, 12 Aug 2010 15:10:12 +0000 Subject: [PATCH] fixed FormulaParser to correctly process defined names with underscore, see bugzilla 49725 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@984823 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../apache/poi/ss/formula/FormulaParser.java | 2 +- .../poi/hssf/model/TestFormulaParser.java | 46 ++++++++++++++++++- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 0b791f101..842b64b71 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 49725 - fixed FormulaParser to correctly process defined names with underscore 48526 - added implementation for RANDBETWEEN() 49725 - avoid exception in OperandResolver.parseDouble when input is minus ("-") 49723 - fixed OperandResolver to correctly handle inputs with leading decimal place diff --git a/src/java/org/apache/poi/ss/formula/FormulaParser.java b/src/java/org/apache/poi/ss/formula/FormulaParser.java index 44c873643..0f186e744 100644 --- a/src/java/org/apache/poi/ss/formula/FormulaParser.java +++ b/src/java/org/apache/poi/ss/formula/FormulaParser.java @@ -665,7 +665,7 @@ public final class FormulaParser { hasDigits = true; } else if (Character.isLetter(ch)) { hasLetters = true; - } else if (ch =='$') { + } else if (ch =='$' || ch =='_') { // } else { break; diff --git a/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java b/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java index 72e055fe7..aac3dd7d8 100644 --- a/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java +++ b/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java @@ -271,7 +271,51 @@ public final class TestFormulaParser extends TestCase { cell.setCellFormula("Cash_Flow!A1"); } - // bug 38396 : Formula with exponential numbers not parsed correctly. + /** bug 49725, defined names with underscore */ + public void testNamesWithUnderscore() { + HSSFWorkbook wb = new HSSFWorkbook(); //or new XSSFWorkbook(); + HSSFSheet sheet = wb.createSheet("NamesWithUnderscore"); + + HSSFName nm; + + nm = wb.createName(); + nm.setNameName("DA6_LEO_WBS_Number"); + nm.setRefersToFormula("33"); + + nm = wb.createName(); + nm.setNameName("DA6_LEO_WBS_Name"); + nm.setRefersToFormula("33"); + + nm = wb.createName(); + nm.setNameName("A1_"); + nm.setRefersToFormula("22"); + + nm = wb.createName(); + nm.setNameName("_A1"); + nm.setRefersToFormula("11"); + + nm = wb.createName(); + nm.setNameName("A_1"); + nm.setRefersToFormula("44"); + + nm = wb.createName(); + nm.setNameName("A_1_"); + nm.setRefersToFormula("44"); + + HSSFRow row = sheet.createRow(0); + HSSFCell cell = row.createCell(0); + + cell.setCellFormula("DA6_LEO_WBS_Number*2"); + assertEquals("DA6_LEO_WBS_Number*2", cell.getCellFormula()); + + cell.setCellFormula("(A1_*_A1+A_1)/A_1_"); + assertEquals("(A1_*_A1+A_1)/A_1_", cell.getCellFormula()); + + cell.setCellFormula("INDEX(DA6_LEO_WBS_Name,MATCH($A3,DA6_LEO_WBS_Number,0))"); + assertEquals("INDEX(DA6_LEO_WBS_Name,MATCH($A3,DA6_LEO_WBS_Number,0))", cell.getCellFormula()); + } + + // bug 38396 : Formula with exponential numbers not parsed correctly. public void testExponentialParsing() { confirmTokenClasses("1.3E21/2", NumberPtg.class, IntPtg.class, DividePtg.class); confirmTokenClasses("1322E21/2", NumberPtg.class, IntPtg.class, DividePtg.class);