]> source.dussan.org Git - poi.git/commitdiff
Update Ref3DPxg to support multi-sheet ranges too, for #55906
authorNick Burch <nick@apache.org>
Fri, 25 Jul 2014 10:22:21 +0000 (10:22 +0000)
committerNick Burch <nick@apache.org>
Fri, 25 Jul 2014 10:22:21 +0000 (10:22 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1613376 13f79535-47bb-0310-9956-ffa450edef68

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

index d851b4474b7c8fadeb1b44bdd5ab48df0e817ebe..ce007d041541f9adba1c77a1ca2b6e6084d71ebe 100644 (file)
@@ -17,7 +17,9 @@
 
 package org.apache.poi.ss.formula.ptg;
 
+import org.apache.poi.ss.formula.SheetIdentifier;
 import org.apache.poi.ss.formula.SheetNameFormatter;
+import org.apache.poi.ss.formula.SheetRangeIdentifier;
 import org.apache.poi.ss.util.CellReference;
 import org.apache.poi.util.LittleEndianOutput;
 
@@ -31,21 +33,28 @@ import org.apache.poi.util.LittleEndianOutput;
  */
 public final class Ref3DPxg extends RefPtgBase implements Pxg {
     private int externalWorkbookNumber = -1;
-    private String sheetName;
+    private String firstSheetName;
+    private String lastSheetName;
 
-    public Ref3DPxg(int externalWorkbookNumber, String sheetName, String cellref) {
+    public Ref3DPxg(int externalWorkbookNumber, SheetIdentifier sheetName, String cellref) {
         this(externalWorkbookNumber, sheetName, new CellReference(cellref));
     }
-    public Ref3DPxg(int externalWorkbookNumber, String sheetName, CellReference c) {
+    public Ref3DPxg(int externalWorkbookNumber, SheetIdentifier sheetName, CellReference c) {
         super(c);
         this.externalWorkbookNumber = externalWorkbookNumber;
-        this.sheetName = sheetName;
+        
+        this.firstSheetName = sheetName.getSheetIdentifier().getName();
+        if (sheetName instanceof SheetRangeIdentifier) {
+            this.lastSheetName = ((SheetRangeIdentifier)sheetName).getLastSheetIdentifier().getName();
+        } else {
+            this.lastSheetName = null;
+        }
     }
     
-    public Ref3DPxg(String sheetName, String cellref) {
+    public Ref3DPxg(SheetIdentifier sheetName, String cellref) {
         this(sheetName, new CellReference(cellref));
     }
-    public Ref3DPxg(String sheetName, CellReference c) {
+    public Ref3DPxg(SheetIdentifier sheetName, CellReference c) {
         this(-1, sheetName, c);
     }
 
@@ -58,7 +67,11 @@ public final class Ref3DPxg extends RefPtgBase implements Pxg {
             sb.append("workbook=").append(getExternalWorkbookNumber());
             sb.append("] ");
         }
-        sb.append("sheet=").append(getSheetName());
+        sb.append("sheet=").append(firstSheetName);
+        if (lastSheetName != null) {
+            sb.append(" : ");
+            sb.append("sheet=").append(lastSheetName);
+        }
         sb.append(" ! ");
         sb.append(formatReferenceAsString());
         sb.append("]");
@@ -68,12 +81,18 @@ public final class Ref3DPxg extends RefPtgBase implements Pxg {
     public int getExternalWorkbookNumber() {
         return externalWorkbookNumber;
     }
-    public String getSheetName() {
-        return sheetName;
+    public String getSheetName() { // TODO Rename to getFirstSheetName
+        return firstSheetName;
+    }
+    public String getLastSheetName() {
+        return lastSheetName;
     }
     
-    public void setSheetName(String sheetName) {
-        this.sheetName = sheetName;
+    public void setSheetName(String sheetName) { // TODO Rename to setFirstSheetName
+        this.firstSheetName = sheetName;
+    }
+    public void setLastSheetName(String sheetName) {
+        this.lastSheetName = sheetName;
     }
     
     public String format2DRefAsString() {
@@ -87,8 +106,12 @@ public final class Ref3DPxg extends RefPtgBase implements Pxg {
             sb.append(externalWorkbookNumber);
             sb.append(']');
         }
-        if (sheetName != null) {
-            SheetNameFormatter.appendFormat(sb, sheetName);
+        if (firstSheetName != null) {
+            SheetNameFormatter.appendFormat(sb, firstSheetName);
+        }
+        if (lastSheetName != null) {
+            sb.append(':');
+            SheetNameFormatter.appendFormat(sb, lastSheetName);
         }
         sb.append('!');
         sb.append(formatReferenceAsString());
index 84f6366fe0ae5fa229772d9e7a0cc63bc574ac82..5c3773d1cb0c3ee932fcf2df8275f94043c5eecf 100644 (file)
@@ -192,13 +192,11 @@ public final class XSSFEvaluationWorkbook implements FormulaRenderingWorkbook, E
         }
        }
     public Ptg get3DReferencePtg(CellReference cell, SheetIdentifier sheet) {
-        String sheetName = sheet._sheetIdentifier.getName();
-        
         if (sheet._bookName != null) {
             int bookIndex = resolveBookIndex(sheet._bookName);
-            return new Ref3DPxg(bookIndex, sheetName, cell);
+            return new Ref3DPxg(bookIndex, sheet, cell);
         } else {
-            return new Ref3DPxg(sheetName, cell);
+            return new Ref3DPxg(sheet, cell);
         }
     }
     public Ptg get3DReferencePtg(AreaReference area, SheetIdentifier sheet) {
index bf86a004cbea62089c47803b60ee58e897e455ab..65e269d546626f5ef86944e4561d37b68583501a 100644 (file)
@@ -283,19 +283,17 @@ public final class TestXSSFFormulaParser {
             
             
             // Check things parse as expected:
-            // TODO Fix it so that the Pxgs get the second sheet too!
             
             
             // SUM to one cell over 3 workbooks, relative reference
             ptgs = parse(fpb, "SUM(Sheet1:Sheet3!A1)");
             assertEquals(2, ptgs.length);
             if (wb instanceof HSSFWorkbook) {
-                assertEquals(Ref3DPtg.class,     ptgs[0].getClass());
-                assertEquals("Sheet1:Sheet3!A1", toFormulaString(ptgs[0], fpb));
+                assertEquals(Ref3DPtg.class, ptgs[0].getClass());
             } else {
                 assertEquals(Ref3DPxg.class, ptgs[0].getClass());
-                assertEquals("Sheet1!A1",    toFormulaString(ptgs[0], fpb));
             }
+            assertEquals("Sheet1:Sheet3!A1", toFormulaString(ptgs[0], fpb));
             assertEquals(AttrPtg.class, ptgs[1].getClass());
             assertEquals("SUM",         toFormulaString(ptgs[1], fpb));
             
@@ -304,12 +302,11 @@ public final class TestXSSFFormulaParser {
             ptgs = parse(fpb, "MAX(Sheet1:Sheet3!A$1)");
             assertEquals(2, ptgs.length);
             if (wb instanceof HSSFWorkbook) {
-                assertEquals(Ref3DPtg.class,      ptgs[0].getClass());
-                assertEquals("Sheet1:Sheet3!A$1", toFormulaString(ptgs[0], fpb));
+                assertEquals(Ref3DPtg.class, ptgs[0].getClass());
             } else {
                 assertEquals(Ref3DPxg.class, ptgs[0].getClass());
-                assertEquals("Sheet1!A$1",   toFormulaString(ptgs[0], fpb));
             }
+            assertEquals("Sheet1:Sheet3!A$1", toFormulaString(ptgs[0], fpb));
             assertEquals(FuncVarPtg.class, ptgs[1].getClass());
             assertEquals("MAX",            toFormulaString(ptgs[1], fpb));
             
@@ -318,12 +315,11 @@ public final class TestXSSFFormulaParser {
             ptgs = parse(fpb, "MIN(Sheet1:Sheet3!$A$1)");
             assertEquals(2, ptgs.length);
             if (wb instanceof HSSFWorkbook) {
-                assertEquals(Ref3DPtg.class,       ptgs[0].getClass());
-                assertEquals("Sheet1:Sheet3!$A$1", toFormulaString(ptgs[0], fpb));
+                assertEquals(Ref3DPtg.class, ptgs[0].getClass());
             } else {
                 assertEquals(Ref3DPxg.class, ptgs[0].getClass());
-                assertEquals("Sheet1!$A$1",  toFormulaString(ptgs[0], fpb));
             }
+            assertEquals("Sheet1:Sheet3!$A$1", toFormulaString(ptgs[0], fpb));
             assertEquals(FuncVarPtg.class, ptgs[1].getClass());
             assertEquals("MIN",            toFormulaString(ptgs[1], fpb));