From d879bdb970b8b7fa3254a506fbc7f2cecb6fe5ed Mon Sep 17 00:00:00 2001 From: "Andrew C. Oliver" Date: Sun, 28 Apr 2002 18:45:10 +0000 Subject: [PATCH] Holy schisse - cell referenecs seem to be working at least for simple formulas git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352535 13f79535-47bb-0310-9956-ffa450edef68 --- .../hssf/record/formula/FormulaParser.java | 12 +++--- .../record/formula/ValueReferencePtg.java | 40 ++++++++++++++++--- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/java/org/apache/poi/hssf/record/formula/FormulaParser.java b/src/java/org/apache/poi/hssf/record/formula/FormulaParser.java index 58becf0faf..aa8d626ebb 100644 --- a/src/java/org/apache/poi/hssf/record/formula/FormulaParser.java +++ b/src/java/org/apache/poi/hssf/record/formula/FormulaParser.java @@ -68,7 +68,6 @@ import java.io.File; /** * EXPERIMENTAL * - * @author Avik Sengupta * * This class parses a formula string into a List of tokens in RPN order * Inspired by @@ -77,6 +76,9 @@ import java.io.File; * ::= [ ]* * ::= [ ::= | () | + * + * @author Avik Sengupta + * @author Andrew C. oliver (acoliver at apache dot org) */ public class FormulaParser { @@ -234,21 +236,21 @@ public class FormulaParser { /** Parse and Translate a Identifier */ private void Ident() { - String Name; - Name = GetName(); + String name; + name = GetName(); if (Look == '('){ //This is a function Match('('); int numArgs = Arguments(); Match(')'); //this is the end of the function - tokens.add(new DummyFunctionPtg(Name,numArgs)); + tokens.add(new DummyFunctionPtg(name,numArgs)); } else { //this can be either a cell ref or a named range !! boolean cellRef = true ; //we should probably do it with reg exp?? if (cellRef) { - tokens.add(new ValueReferencePtg()); //TODO we need to pass in Name somewhere?? + tokens.add(new ValueReferencePtg(name)); //TODO we need to pass in Name somewhere?? }else { //handle after named range is integrated!! } diff --git a/src/java/org/apache/poi/hssf/record/formula/ValueReferencePtg.java b/src/java/org/apache/poi/hssf/record/formula/ValueReferencePtg.java index 7270df95ab..a9b7aa06d1 100644 --- a/src/java/org/apache/poi/hssf/record/formula/ValueReferencePtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/ValueReferencePtg.java @@ -63,9 +63,14 @@ package org.apache.poi.hssf.record.formula; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.BitField; +import org.apache.poi.hssf.util.ReferenceUtil; + /** - * - * @author andy + * ValueReferencePtg - handles references (such as A1, A2, IA4) - Should also + * be made to handle relative versus absolute references but I don't know enough + * about using them in excel to know if its correct. Seems inverted to me. + * FIXME = correct abs vs relative references + * @author Andrew C. Oliver (acoliver@apache.org) */ public class ValueReferencePtg @@ -83,6 +88,18 @@ public class ValueReferencePtg public ValueReferencePtg() { } + + /** + * Takes in a String represnetation of a cell reference and fills out the + * numeric fields. + */ + public ValueReferencePtg(String cellref) { + int[] xy = ReferenceUtil.getXYFromReference(cellref); + setRow((short)xy[0]); + setColumn((short)xy[1]); + setColRelative(true); + setRowRelative(true); + } /** Creates new ValueReferencePtg */ @@ -107,6 +124,9 @@ public class ValueReferencePtg public void writeBytes(byte [] array, int offset) { + array[offset] = sid; + LittleEndian.putShort(array,offset+1,field_1_row); + LittleEndian.putShort(array,offset+3,field_2_col); } public void setRow(short row) @@ -123,10 +143,18 @@ public class ValueReferencePtg { return rowRelative.isSet(field_2_col); } - + + public void setRowRelative(boolean rel) { + field_2_col=rowRelative.setShortBoolean(field_2_col,rel); + } + public boolean isColRelative() { - return rowRelative.isSet(field_2_col); + return colRelative.isSet(field_2_col); + } + + public void setColRelative(boolean rel) { + field_2_col=colRelative.setShortBoolean(field_2_col,rel); } public void setColumnRaw(short col) @@ -146,7 +174,7 @@ public class ValueReferencePtg public short getColumn() { - return field_2_col; // fix this + return rowRelative.setShortBoolean(colRelative.setShortBoolean(field_2_col,false),false); } public int getSize() @@ -156,6 +184,6 @@ public class ValueReferencePtg public String toFormulaString() { - return "NO IDEA YET VALUE REF"; + return ReferenceUtil.getReferenceFromXY(getRow(),getColumn()); } } -- 2.39.5