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
This commit is contained in:
Danny Mui 2003-05-17 17:53:38 +00:00
parent 5ec4ed341b
commit 944a5624d3
3 changed files with 77 additions and 40 deletions

View File

@ -96,13 +96,22 @@ private int dim;
* String array
*/
private String[] seperateAreaRefs(String reference) {
String retval[] = new String[2];
String[] retval = null;
int length = reference.length();
int loc = reference.indexOf(':',0);
if(loc == -1){
retval = new String[1];
retval[0] = reference;
}
else{
retval = new String[2];
int sheetStart = reference.indexOf("!");
retval[0] = reference.substring(0,loc);
retval[1] = reference.substring(loc+1);
retval[0] = reference.substring(0, sheetStart+1) + reference.substring(sheetStart + 1,loc);
retval[1] = reference.substring(0, sheetStart+1) + reference.substring(loc+1);
}
return retval;
}
}

View File

@ -64,18 +64,20 @@ public class CellReference {
/** Creates new CellReference */
private int row;
private int col;
private String sheetName;
private boolean rowAbs;
private boolean colAbs;
public CellReference(String cellRef) {
String[] parts = seperateRowColumns(cellRef);
String ref = parts[0];
String[] parts = separateRefParts(cellRef);
sheetName = parts[0];
String ref = parts[1];
if (ref.charAt(0) == '$') {
colAbs=true;
ref=ref.substring(1);
}
col = convertColStringToNum(ref);
ref=parts[1];
ref=parts[2];
if (ref.charAt(0) == '$') {
rowAbs=true;
ref=ref.substring(1);
@ -95,9 +97,10 @@ public class CellReference {
}
public int getRow(){return row;}
public int getCol(){return col;}
public short getCol(){return (short) col;}
public boolean isRowAbsolute(){return rowAbs;}
public boolean isColAbsolute(){return colAbs;}
public String getSheetName(){return sheetName;}
/**
* takes in a column reference portion of a CellRef and converts it from
@ -126,14 +129,17 @@ public class CellReference {
* 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();
@ -146,9 +152,8 @@ public class CellReference {
}
}
retval[0] = reference.substring(start,loc);
retval[1] = reference.substring(loc);
retval[1] = reference.substring(start,loc);
retval[2] = reference.substring(loc);
return retval;
}

View File

@ -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);
}
}