improved toString and refactored toFormulaString on Area(3D)Ptg

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@658833 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Josh Micich 2008-05-21 19:57:40 +00:00
parent 15db21d326
commit b1ad25f7e3
3 changed files with 79 additions and 95 deletions

View File

@ -27,7 +27,7 @@ import org.apache.poi.util.LittleEndian;
/** /**
* Title: Area 3D Ptg - 3D referecnce (Sheet + Area)<P> * Title: Area 3D Ptg - 3D reference (Sheet + Area)<P>
* Description: Defined a area in Extern Sheet. <P> * Description: Defined a area in Extern Sheet. <P>
* REFERENCE: <P> * REFERENCE: <P>
* @author Libin Roman (Vista Portal LDT. Developer) * @author Libin Roman (Vista Portal LDT. Developer)
@ -35,7 +35,6 @@ import org.apache.poi.util.LittleEndian;
* @author Jason Height (jheight at chariot dot net dot au) * @author Jason Height (jheight at chariot dot net dot au)
* @version 1.0-pre * @version 1.0-pre
*/ */
public class Area3DPtg extends Ptg implements AreaI public class Area3DPtg extends Ptg implements AreaI
{ {
public final static byte sid = 0x3b; public final static byte sid = 0x3b;
@ -84,23 +83,15 @@ public class Area3DPtg extends Ptg implements AreaI
setExternSheetIndex(externalSheetIndex); setExternSheetIndex(externalSheetIndex);
} }
public String toString() public String toString() {
{ StringBuffer sb = new StringBuffer();
StringBuffer buffer = new StringBuffer(); sb.append(getClass().getName());
sb.append(" [");
buffer.append( "AreaPtg\n" ); sb.append("sheetIx=").append(getExternSheetIndex());
buffer.append( "Index to Extern Sheet = " + getExternSheetIndex() ).append( "\n" ); sb.append(" ! ");
buffer.append( "firstRow = " + getFirstRow() ).append( "\n" ); sb.append(AreaReference.formatAsString(this));
buffer.append( "lastRow = " + getLastRow() ).append( "\n" ); sb.append("]");
buffer.append( "firstCol = " + getFirstColumn() ).append( "\n" ); return sb.toString();
buffer.append( "lastCol = " + getLastColumn() ).append( "\n" );
buffer.append( "firstColRel= "
+ isFirstRowRelative() ).append( "\n" );
buffer.append( "lastColRowRel = "
+ isLastRowRelative() ).append( "\n" );
buffer.append( "firstColRel = " + isFirstColRelative() ).append( "\n" );
buffer.append( "lastColRel = " + isLastColRelative() ).append( "\n" );
return buffer.toString();
} }
public void writeBytes( byte[] array, int offset ) public void writeBytes( byte[] array, int offset )
@ -284,7 +275,7 @@ public class Area3DPtg extends Ptg implements AreaI
} }
// Now the normal area bit // Now the normal area bit
retval.append( AreaPtg.toFormulaString(this, book) ); retval.append(AreaReference.formatAsString(this));
// All done // All done
return retval.toString(); return retval.toString();
@ -326,6 +317,7 @@ public class Area3DPtg extends Ptg implements AreaI
public int hashCode() public int hashCode()
{ {
// TODO - hashCode seems to be unused
int result; int result;
result = (int) field_1_index_extern_sheet; result = (int) field_1_index_extern_sheet;
result = 29 * result + (int) field_2_first_row; result = 29 * result + (int) field_2_first_row;

View File

@ -114,23 +114,13 @@ public class AreaPtg extends Ptg implements AreaI {
return "AreaPtg"; return "AreaPtg";
} }
public String toString() public String toString() {
{ StringBuffer sb = new StringBuffer();
StringBuffer buffer = new StringBuffer(); sb.append(getClass().getName());
sb.append(" [");
buffer.append(getAreaPtgName()); sb.append(AreaReference.formatAsString(this));
buffer.append("\n"); sb.append("]");
buffer.append("firstRow = " + getFirstRow()).append("\n"); return sb.toString();
buffer.append("lastRow = " + getLastRow()).append("\n");
buffer.append("firstCol = " + getFirstColumn()).append("\n");
buffer.append("lastCol = " + getLastColumn()).append("\n");
buffer.append("firstColRowRel= "
+ isFirstRowRelative()).append("\n");
buffer.append("lastColRowRel = "
+ isLastRowRelative()).append("\n");
buffer.append("firstColRel = " + isFirstColRelative()).append("\n");
buffer.append("lastColRel = " + isLastColRelative()).append("\n");
return buffer.toString();
} }
public void writeBytes(byte [] array, int offset) { public void writeBytes(byte [] array, int offset) {
@ -307,19 +297,8 @@ public class AreaPtg extends Ptg implements AreaI {
field_4_last_column = column; field_4_last_column = column;
} }
public String toFormulaString(HSSFWorkbook book) public String toFormulaString(HSSFWorkbook book) {
{ return AreaReference.formatAsString(this);
return toFormulaString(this, book);
}
protected static String toFormulaString(AreaI area, HSSFWorkbook book) {
CellReference topLeft = new CellReference(area.getFirstRow(),area.getFirstColumn(),!area.isFirstRowRelative(),!area.isFirstColRelative());
CellReference botRight = new CellReference(area.getLastRow(),area.getLastColumn(),!area.isLastRowRelative(),!area.isLastColRelative());
if(AreaReference.isWholeColumnReference(topLeft, botRight)) {
return (new AreaReference(topLeft, botRight)).formatAsString();
} else {
return topLeft.formatAsString() + ":" + botRight.formatAsString();
}
} }
public byte getDefaultOperandClass() { public byte getDefaultOperandClass() {

View File

@ -15,12 +15,13 @@
limitations under the License. limitations under the License.
==================================================================== */ ==================================================================== */
package org.apache.poi.hssf.util; package org.apache.poi.hssf.util;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import org.apache.poi.hssf.record.formula.AreaI;
public final class AreaReference { public final class AreaReference {
/** The character (!) that separates sheet names from cell references */ /** The character (!) that separates sheet names from cell references */
@ -50,13 +51,13 @@ public final class AreaReference {
// Special handling for whole-column references // Special handling for whole-column references
if(parts.length == 2 && parts[0].length() == 1 && if(parts.length == 2 && parts[0].length() == 1 &&
parts[1].length() == 1 && parts[1].length() == 1 &&
parts[0].charAt(0) >= 'A' && parts[0].charAt(0) <= 'Z' && parts[0].charAt(0) >= 'A' && parts[0].charAt(0) <= 'Z' &&
parts[1].charAt(0) >= 'A' && parts[1].charAt(0) <= 'Z') { parts[1].charAt(0) >= 'A' && parts[1].charAt(0) <= 'Z') {
// Represented internally as x$1 to x$65536 // Represented internally as x$1 to x$65536
// which is the maximum range of rows // which is the maximum range of rows
parts[0] = parts[0] + "$1"; parts[0] = parts[0] + "$1";
parts[1] = parts[1] + "$65536"; parts[1] = parts[1] + "$65536";
} }
_firstCell = new CellReference(parts[0]); _firstCell = new CellReference(parts[0]);
@ -74,9 +75,9 @@ public final class AreaReference {
* Creates an area ref from a pair of Cell References. * Creates an area ref from a pair of Cell References.
*/ */
public AreaReference(CellReference topLeft, CellReference botRight) { public AreaReference(CellReference topLeft, CellReference botRight) {
_firstCell = topLeft; _firstCell = topLeft;
_lastCell = botRight; _lastCell = botRight;
_isSingleCell = false; _isSingleCell = false;
} }
/** /**
@ -98,17 +99,17 @@ public final class AreaReference {
* such as C:C or D:G ? * such as C:C or D:G ?
*/ */
public static boolean isWholeColumnReference(CellReference topLeft, CellReference botRight) { public static boolean isWholeColumnReference(CellReference topLeft, CellReference botRight) {
// These are represented as something like // These are represented as something like
// C$1:C$65535 or D$1:F$0 // C$1:C$65535 or D$1:F$0
// i.e. absolute from 1st row to 0th one // i.e. absolute from 1st row to 0th one
if(topLeft.getRow() == 0 && topLeft.isRowAbsolute() && if(topLeft.getRow() == 0 && topLeft.isRowAbsolute() &&
botRight.getRow() == 65535 && botRight.isRowAbsolute()) { botRight.getRow() == 65535 && botRight.isRowAbsolute()) {
return true; return true;
} }
return false; return false;
} }
public boolean isWholeColumnReference() { public boolean isWholeColumnReference() {
return isWholeColumnReference(_firstCell, _lastCell); return isWholeColumnReference(_firstCell, _lastCell);
} }
/** /**
@ -155,26 +156,26 @@ public final class AreaReference {
* Returns a reference to every cell covered by this area * Returns a reference to every cell covered by this area
*/ */
public CellReference[] getAllReferencedCells() { public CellReference[] getAllReferencedCells() {
// Special case for single cell reference // Special case for single cell reference
if(_isSingleCell) { if(_isSingleCell) {
return new CellReference[] { _firstCell, }; return new CellReference[] { _firstCell, };
} }
// Interpolate between the two // Interpolate between the two
int minRow = Math.min(_firstCell.getRow(), _lastCell.getRow()); int minRow = Math.min(_firstCell.getRow(), _lastCell.getRow());
int maxRow = Math.max(_firstCell.getRow(), _lastCell.getRow()); int maxRow = Math.max(_firstCell.getRow(), _lastCell.getRow());
int minCol = Math.min(_firstCell.getCol(), _lastCell.getCol()); int minCol = Math.min(_firstCell.getCol(), _lastCell.getCol());
int maxCol = Math.max(_firstCell.getCol(), _lastCell.getCol()); int maxCol = Math.max(_firstCell.getCol(), _lastCell.getCol());
String sheetName = _firstCell.getSheetName(); String sheetName = _firstCell.getSheetName();
ArrayList refs = new ArrayList(); ArrayList refs = new ArrayList();
for(int row=minRow; row<=maxRow; row++) { for(int row=minRow; row<=maxRow; row++) {
for(int col=minCol; col<=maxCol; col++) { for(int col=minCol; col<=maxCol; col++) {
CellReference ref = new CellReference(sheetName, row, col, _firstCell.isRowAbsolute(), _firstCell.isColAbsolute()); CellReference ref = new CellReference(sheetName, row, col, _firstCell.isRowAbsolute(), _firstCell.isColAbsolute());
refs.add(ref); refs.add(ref);
} }
} }
return (CellReference[])refs.toArray(new CellReference[refs.size()]); return (CellReference[])refs.toArray(new CellReference[refs.size()]);
} }
/** /**
@ -189,13 +190,13 @@ public final class AreaReference {
* @return the text representation of this area reference as it would appear in a formula. * @return the text representation of this area reference as it would appear in a formula.
*/ */
public String formatAsString() { public String formatAsString() {
// Special handling for whole-column references // Special handling for whole-column references
if(isWholeColumnReference()) { if(isWholeColumnReference()) {
return return
CellReference.convertNumToColString(_firstCell.getCol()) CellReference.convertNumToColString(_firstCell.getCol())
+ ":" + + ":" +
CellReference.convertNumToColString(_lastCell.getCol()); CellReference.convertNumToColString(_lastCell.getCol());
} }
StringBuffer sb = new StringBuffer(32); StringBuffer sb = new StringBuffer(32);
sb.append(_firstCell.formatAsString()); sb.append(_firstCell.formatAsString());
@ -210,6 +211,18 @@ public final class AreaReference {
} }
return sb.toString(); return sb.toString();
} }
/**
* Formats a 2-D area as it would appear in a formula. See formatAsString() (no-arg)
*/
public static String formatAsString(AreaI area) {
CellReference topLeft = new CellReference(area.getFirstRow(),area.getFirstColumn(),!area.isFirstRowRelative(),!area.isFirstColRelative());
CellReference botRight = new CellReference(area.getLastRow(),area.getLastColumn(),!area.isLastRowRelative(),!area.isLastColRelative());
if(isWholeColumnReference(topLeft, botRight)) {
return (new AreaReference(topLeft, botRight)).formatAsString();
}
return topLeft.formatAsString() + ":" + botRight.formatAsString();
}
public String toString() { public String toString() {
StringBuffer sb = new StringBuffer(64); StringBuffer sb = new StringBuffer(64);
sb.append(getClass().getName()).append(" ["); sb.append(getClass().getName()).append(" [");