]> source.dussan.org Git - poi.git/commitdiff
Correct non-sheet-based NameXPxg formula rendering, and add tests
authorNick Burch <nick@apache.org>
Mon, 21 Jul 2014 07:35:09 +0000 (07:35 +0000)
committerNick Burch <nick@apache.org>
Mon, 21 Jul 2014 07:35:09 +0000 (07:35 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1612203 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/ss/formula/ptg/NameXPxg.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFormulaParser.java

index 165579032708ed0cef7b9c6812bff6f48955deb3..b1ca488c788536569c4ee04193e3893287f93a03 100644 (file)
@@ -74,15 +74,20 @@ public final class NameXPxg extends OperandPtg implements Pxg {
 
     public String toFormulaString() {
         StringBuffer sb = new StringBuffer();
+        boolean needsExclamation = false;
         if (externalWorkbookNumber >= 0) {
             sb.append('[');
             sb.append(externalWorkbookNumber);
             sb.append(']');
+            needsExclamation = true;
         }
         if (sheetName != null) {
             SheetNameFormatter.appendFormat(sb, sheetName);
+            needsExclamation = true;
+        }
+        if (needsExclamation) {
+            sb.append('!');
         }
-        sb.append('!');
         sb.append(nameName);
         return sb.toString();
     }
index d18705eb28e27ebe2d35e888953174319c69cc49..13bde7910c6db12567e8ef692ff77b24a0c0914d 100644 (file)
@@ -24,7 +24,11 @@ import static org.junit.Assert.fail;
 import org.apache.poi.ss.formula.FormulaParseException;
 import org.apache.poi.ss.formula.FormulaParser;
 import org.apache.poi.ss.formula.FormulaType;
+import org.apache.poi.ss.formula.ptg.Area3DPxg;
+import org.apache.poi.ss.formula.ptg.AreaPtg;
+import org.apache.poi.ss.formula.ptg.AttrPtg;
 import org.apache.poi.ss.formula.ptg.FuncPtg;
+import org.apache.poi.ss.formula.ptg.FuncVarPtg;
 import org.apache.poi.ss.formula.ptg.IntPtg;
 import org.apache.poi.ss.formula.ptg.NamePtg;
 import org.apache.poi.ss.formula.ptg.NameXPxg;
@@ -78,6 +82,40 @@ public final class TestXSSFFormulaParser {
         } catch (FormulaParseException e){
             assertEquals("Specified named range 'XFD1048577' does not exist in the current workbook.", e.getMessage());
         }
+        
+        // Formula referencing one cell
+        ptgs = parse(fpb, "ISEVEN(A1)");
+        assertEquals(3, ptgs.length);
+        assertEquals(NameXPxg.class,   ptgs[0].getClass());
+        assertEquals(RefPtg.class,     ptgs[1].getClass());
+        assertEquals(FuncVarPtg.class, ptgs[2].getClass());
+        assertEquals("ISEVEN", ptgs[0].toFormulaString());
+        assertEquals("A1",     ptgs[1].toFormulaString());
+        assertEquals("#external#", ptgs[2].toFormulaString());
+        
+        // Formula referencing an area
+        ptgs = parse(fpb, "SUM(A1:B3)");
+        assertEquals(2, ptgs.length);
+        assertEquals(AreaPtg.class, ptgs[0].getClass());
+        assertEquals(AttrPtg.class, ptgs[1].getClass());
+        assertEquals("A1:B3", ptgs[0].toFormulaString());
+        assertEquals("SUM",   ptgs[1].toFormulaString());
+        
+        // Formula referencing one cell in a different sheet
+        ptgs = parse(fpb, "SUM(Sheet1!A1)");
+        assertEquals(2, ptgs.length);
+        assertEquals(Ref3DPxg.class, ptgs[0].getClass());
+        assertEquals(AttrPtg.class,  ptgs[1].getClass());
+        assertEquals("Sheet1!A1", ptgs[0].toFormulaString());
+        assertEquals("SUM",       ptgs[1].toFormulaString());
+        
+        // Formula referencing an area in a different sheet
+        ptgs = parse(fpb, "SUM(Sheet1!A1:B3)");
+        assertEquals(2, ptgs.length);
+        assertEquals(Area3DPxg.class,ptgs[0].getClass());
+        assertEquals(AttrPtg.class,  ptgs[1].getClass());
+        assertEquals("Sheet1!A1:B3", ptgs[0].toFormulaString());
+        assertEquals("SUM",          ptgs[1].toFormulaString());
     }
 
        @Test