Author: nick
Date: Tue Jan  8 07:47:00 2008
New Revision: 610026

URL: http://svn.apache.org/viewvc?rev=610026&view=rev
Log:
Extend named range support in formulas to include non-contiguous named ranges

Modified:
    poi/trunk/src/documentation/content/xdocs/changes.xml
    poi/trunk/src/documentation/content/xdocs/status.xml
    poi/trunk/src/java/org/apache/poi/hssf/record/NameRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java
    poi/trunk/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java

Modified: poi/trunk/src/documentation/content/xdocs/changes.xml
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/changes.xml?rev=610026&r1=610025&r2=610026&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/changes.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/changes.xml Tue Jan  8 07:47:00 
2008
@@ -36,7 +36,7 @@
 
                <!-- Don't forget to update status.xml too! -->
         <release version="3.0.2-FINAL" date="2008-??-??">
-            <action dev="POI-DEVELOPERS" type="add">43510 - Add support for 
named ranges in formulas</action>
+            <action dev="POI-DEVELOPERS" type="add">43510 - Add support for 
named ranges in formulas, including non-contiguous named ranges</action>
             <action dev="POI-DEVELOPERS" type="add">43937 - Add support for 
hiding and un-hiding sheets, and checking their current hidden status</action>
             <action dev="POI-DEVELOPERS" type="fix">44167 - Fix for 
non-contiguous named ranges</action>
             <action dev="POI-DEVELOPERS" type="fix">44070 - Fix for shifting 
comments when shifting rows</action>

Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=610026&r1=610025&r2=610026&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Tue Jan  8 07:47:00 
2008
@@ -33,7 +33,7 @@
        <!-- Don't forget to update changes.xml too! -->
     <changes>
         <release version="3.0.2-FINAL" date="2008-??-??">
-            <action dev="POI-DEVELOPERS" type="add">43510 - Add support for 
named ranges in formulas</action>
+            <action dev="POI-DEVELOPERS" type="add">43510 - Add support for 
named ranges in formulas, including non-contiguous named ranges</action>
             <action dev="POI-DEVELOPERS" type="add">43937 - Add support for 
hiding and un-hiding sheets, and checking their current hidden status</action>
             <action dev="POI-DEVELOPERS" type="fix">44167 - Fix for 
non-contiguous named ranges</action>
             <action dev="POI-DEVELOPERS" type="fix">44070 - Fix for shifting 
comments when shifting rows</action>

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/NameRecord.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/NameRecord.java?rev=610026&r1=610025&r2=610026&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/NameRecord.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/NameRecord.java Tue Jan  8 
07:47:00 2008
@@ -24,6 +24,7 @@
 
 import org.apache.poi.hssf.model.Workbook;
 import org.apache.poi.hssf.record.formula.*;
+import org.apache.poi.hssf.util.AreaReference;
 import org.apache.poi.hssf.util.RangeAddress;
 import org.apache.poi.util.HexDump;
 import org.apache.poi.util.LittleEndian;
@@ -712,19 +713,32 @@
         }
 
         if (ra.hasRange()) {
-            ptg = new Area3DPtg();
-            ((Area3DPtg) ptg).setExternSheetIndex(externSheetIndex);
-            ((Area3DPtg) ptg).setArea(ref);
-            this.setDefinitionTextLength((short)ptg.getSize());
+               // Is it contiguous or not?
+               AreaReference[] refs = 
+                       AreaReference.generateContiguous(ref);
+            this.setDefinitionTextLength((short)0);
+
+            // Add the area reference(s) 
+               for(int i=0; i<refs.length; i++) {
+                   ptg = new Area3DPtg();
+                   ((Area3DPtg) ptg).setExternSheetIndex(externSheetIndex);
+                   ((Area3DPtg) ptg).setArea(refs[i].toString());
+                   field_13_name_definition.push(ptg);
+                   this.setDefinitionTextLength( (short)(getDefinitionLength() 
+ ptg.getSize()) );
+               }
+               // And then a union if we had more than one area
+               if(refs.length > 1) {
+                       ptg = new UnionPtg();
+                field_13_name_definition.push(ptg);
+                   this.setDefinitionTextLength( (short)(getDefinitionLength() 
+ ptg.getSize()) );
+               }
         } else {
             ptg = new Ref3DPtg();
             ((Ref3DPtg) ptg).setExternSheetIndex(externSheetIndex);
             ((Ref3DPtg) ptg).setArea(ref);
+            field_13_name_definition.push(ptg);
             this.setDefinitionTextLength((short)ptg.getSize());
         }
-
-        field_13_name_definition.push(ptg);
-
     }
 
     /**
@@ -858,6 +872,15 @@
             .append("\n");
         buffer.append("    .Name (Unicode text)  = ").append( getNameText() )
             .append("\n");
+        
+        buffer.append("    .Parts (" + field_13_name_definition.size() +"):")
+            .append("\n");
+        Iterator it = field_13_name_definition.iterator();
+        while(it.hasNext()) {
+               Ptg ptg = (Ptg)it.next();
+               buffer.append("       " + ptg.toString()).append("\n");
+        }
+        
         buffer.append("    .Menu text (Unicode string without length field)    
    = ").append( field_14_custom_menu_text )
             .append("\n");
         buffer.append("    .Description text (Unicode string without length 
field) = ").append( field_15_description_text )

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java?rev=610026&r1=610025&r2=610026&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java 
(original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java Tue 
Jan  8 07:47:00 2008
@@ -243,16 +243,22 @@
        public void setArea( String ref )
        {
                AreaReference ar = new AreaReference( ref );
+               CellReference[] crs = ar.getCells();
+               
+               CellReference firstCell = crs[0];
+               CellReference lastCell = firstCell;
+               if(crs.length > 1) {
+                       lastCell = crs[1];
+               }
 
-               setFirstRow( (short) ar.getCells()[0].getRow() );
-               setFirstColumn( (short) ar.getCells()[0].getCol() );
-               setLastRow( (short) ar.getCells()[1].getRow() );
-               setLastColumn( (short) ar.getCells()[1].getCol() );
-               setFirstColRelative( !ar.getCells()[0].isColAbsolute() );
-               setLastColRelative( !ar.getCells()[1].isColAbsolute() );
-               setFirstRowRelative( !ar.getCells()[0].isRowAbsolute() );
-               setLastRowRelative( !ar.getCells()[1].isRowAbsolute() );
-
+               setFirstRow(    (short) firstCell.getRow() );
+               setFirstColumn( (short) firstCell.getCol() );
+               setLastRow(     (short) lastCell.getRow() );
+               setLastColumn(  (short) lastCell.getCol() );
+               setFirstColRelative( !firstCell.isColAbsolute() );
+               setLastColRelative(  !lastCell.isColAbsolute() );
+               setFirstRowRelative( !firstCell.isRowAbsolute() );
+               setLastRowRelative(  !lastCell.isRowAbsolute() );
        }
 
        public String toFormulaString(Workbook book)

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=610026&r1=610025&r2=610026&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 
Tue Jan  8 07:47:00 2008
@@ -382,6 +382,15 @@
                assertTrue("two tokens expected, got "+ptgs.length,ptgs.length 
== 2);
                assertEquals(NamePtg.class, ptgs[0].getClass());
                assertEquals(FuncVarPtg.class, ptgs[1].getClass());
+               
+               // And make it non-contiguous
+               name.setReference("A1:A2,C3");
+               fp = HSSFFormulaEvaluator.getUnderlyingParser(workbook, 
"SUM(testName)");
+               fp.parse();
+               ptgs = fp.getRPNPtg();
+               assertTrue("two tokens expected, got "+ptgs.length,ptgs.length 
== 2);
+               assertEquals(NamePtg.class, ptgs[0].getClass());
+               assertEquals(FuncVarPtg.class, ptgs[1].getClass());
        }
 
        public void testLookupAndMatchFunctionArgs()



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to