From 33dbe52e16f8ccaf5f9203e419c910a8a0741472 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Tue, 8 Jan 2008 15:47:00 +0000 Subject: [PATCH] Extend named range support in formulas to include non-contiguous named ranges git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@610026 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/changes.xml | 2 +- src/documentation/content/xdocs/status.xml | 2 +- .../apache/poi/hssf/record/NameRecord.java | 37 +++++++++++++++---- .../poi/hssf/record/formula/Area3DPtg.java | 24 +++++++----- .../poi/hssf/model/TestFormulaParser.java | 9 +++++ 5 files changed, 56 insertions(+), 18 deletions(-) diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index 448cca0a4a..b5d60ad17f 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -36,7 +36,7 @@ - 43510 - Add support for named ranges in formulas + 43510 - Add support for named ranges in formulas, including non-contiguous named ranges 43937 - Add support for hiding and un-hiding sheets, and checking their current hidden status 44167 - Fix for non-contiguous named ranges 44070 - Fix for shifting comments when shifting rows diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index edc7cccd37..58d6df9aa6 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -33,7 +33,7 @@ - 43510 - Add support for named ranges in formulas + 43510 - Add support for named ranges in formulas, including non-contiguous named ranges 43937 - Add support for hiding and un-hiding sheets, and checking their current hidden status 44167 - Fix for non-contiguous named ranges 44070 - Fix for shifting comments when shifting rows diff --git a/src/java/org/apache/poi/hssf/record/NameRecord.java b/src/java/org/apache/poi/hssf/record/NameRecord.java index 9ddec5a819..0d0d7f1cda 100644 --- a/src/java/org/apache/poi/hssf/record/NameRecord.java +++ b/src/java/org/apache/poi/hssf/record/NameRecord.java @@ -24,6 +24,7 @@ import java.util.Iterator; 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 @@ public class NameRecord extends Record { } 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 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 @@ public class NameRecord extends Record { .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 ) diff --git a/src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java b/src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java index 470b6e390b..d808a94c4d 100644 --- a/src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java @@ -243,16 +243,22 @@ public class Area3DPtg extends Ptg 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) diff --git a/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java b/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java index 51d3e66996..4a84209fb5 100644 --- a/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java +++ b/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java @@ -382,6 +382,15 @@ public class TestFormulaParser extends TestCase { 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()