From 929dcf3fd5491e70a0fbc99a0f818434de93d329 Mon Sep 17 00:00:00 2001 From: Danny Muid Date: Sat, 17 May 2003 17:53:38 +0000 Subject: [PATCH] Fixes AreaReference when a Sheet name is provided PR: 19888 Submitted by: brett.knights@tanner.com Test Case Provided by: Arne.Clauss@gedas.de git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353104 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hssf/util/AreaReference.java | 29 ++++++--- .../apache/poi/hssf/util/CellReference.java | 65 ++++++++++--------- .../poi/hssf/util/TestAreaReference.java | 23 +++++++ 3 files changed, 77 insertions(+), 40 deletions(-) diff --git a/src/java/org/apache/poi/hssf/util/AreaReference.java b/src/java/org/apache/poi/hssf/util/AreaReference.java index 3d195e131..4b9617fed 100644 --- a/src/java/org/apache/poi/hssf/util/AreaReference.java +++ b/src/java/org/apache/poi/hssf/util/AreaReference.java @@ -55,8 +55,8 @@ package org.apache.poi.hssf.util; public class AreaReference { - - + + private CellReference [] cells; private int dim; @@ -80,7 +80,7 @@ private int dim; public CellReference[] getCells() { return cells; } - + public String toString() { StringBuffer retval = new StringBuffer(); for (int i=0;i -1; k--) { char thechar = ref.charAt(k); if ( pos == 0) { @@ -119,21 +122,24 @@ public class CellReference { } return retval-1; } - - + + /** * Seperates the row from the columns and returns an array. Element in * position one is the substring containing the columns still in ALPHA-26 * number format. */ - private String[] seperateRowColumns(String reference) { - + private String[] separateRefParts(String reference) { + // Look for end of sheet name. This will either set // start to 0 (if no sheet name present) or the // index after the sheet reference ends. - int start = reference.indexOf("!") + 1; + String retval[] = new String[3]; + + int start = reference.indexOf("!"); + if (start != -1) retval[0] = reference.substring(0, start); + start += 1; - String retval[] = new String[2]; int length = reference.length(); @@ -145,13 +151,12 @@ public class CellReference { break; } } - - - retval[0] = reference.substring(start,loc); - retval[1] = reference.substring(loc); + + retval[1] = reference.substring(start,loc); + retval[2] = reference.substring(loc); return retval; } - + /** * takes in a 0-based base-10 column and returns a ALPHA-26 representation */ @@ -161,24 +166,24 @@ public class CellReference { int div = col / 26; char small=(char)(mod + 65); char big = (char)(div + 64); - + if (div == 0) { retval = ""+small; } else { retval = ""+big+""+small; } - + return retval; } - - + + public String toString() { StringBuffer retval = new StringBuffer(); retval.append( (colAbs)?"$":""); retval.append( convertNumToColString(col)); retval.append((rowAbs)?"$":""); retval.append(row+1); - + return retval.toString(); } } diff --git a/src/testcases/org/apache/poi/hssf/util/TestAreaReference.java b/src/testcases/org/apache/poi/hssf/util/TestAreaReference.java index beed36367..78655bb39 100644 --- a/src/testcases/org/apache/poi/hssf/util/TestAreaReference.java +++ b/src/testcases/org/apache/poi/hssf/util/TestAreaReference.java @@ -56,6 +56,10 @@ package org.apache.poi.hssf.util; import junit.framework.TestCase; +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFRow; +import org.apache.poi.hssf.usermodel.HSSFSheet; + public class TestAreaReference extends TestCase { public TestAreaReference(String s) { super(s); @@ -77,4 +81,23 @@ public class TestAreaReference extends TestCase { assertTrue("col is abs",cf.isColAbsolute()); assertTrue("string is $B$2",cf.toString().equals("$B$2")); } + + /** + * References failed when sheet names were being used + * Reported by Arne.Clauss@gedas.de + */ + public void testReferenceWithSheet() { + String ref = "Tabelle1!$B$5"; + AreaReference myAreaReference = new AreaReference(ref); + CellReference[] myCellReference = myAreaReference.getCells(); + + assertNotNull("cell reference not null : "+myCellReference[0]); + assertEquals("Not Column B", (short)1,myCellReference[0].getCol()); + assertEquals("Not Row 5", 4,myCellReference[0].getRow()); + } + + public static void main(java.lang.String[] args) { + junit.textui.TestRunner.run(TestAreaReference.class); + } + } \ No newline at end of file