import org.apache.poi.hssf.record.formula.*;
import org.apache.poi.hssf.model.*;
import org.apache.poi.hssf.usermodel.*;
+import org.apache.poi.hssf.util.SheetReferences;
/**
* FormulaViewer - finds formulas in a BIFF8 file and attempts to read them/display
StringBuffer buf = new StringBuffer();
if (token instanceof ExpPtg) return;
- buf.append(name=((OperationPtg) token).toFormulaString());
+ buf.append(name=((OperationPtg) token).toFormulaString((SheetReferences)null));
buf.append(sep);
switch (token.getPtgClass()) {
case Ptg.CLASS_REF :
StringBuffer buf = new StringBuffer();
for (int i=0;i<numptgs;i++) {
token = (Ptg) tokens.get(i);
- buf.append( token.toFormulaString());
+ buf.append( token.toFormulaString((SheetReferences)null));
switch (token.getPtgClass()) {
case Ptg.CLASS_REF :
buf.append("(R)");
private String composeFormula(FormulaRecord record)
{
- return FormulaParser.toFormulaString(record.getParsedExpression());
+ return FormulaParser.toFormulaString((SheetReferences)null,record.getParsedExpression());
}
/**
import org.apache.poi.util.POILogFactory;
import org.apache.poi.hssf
.record.*; // normally I don't do this, buy we literally mean ALL
-import org.apache.poi.hssf.record.formula.FormulaParser;
import org.apache.poi.hssf.record.formula.Ptg;
import org.apache.poi.util.IntList;
import org.apache.poi.util.POILogger;
import java.util.List;
import java.util.Iterator;
-import org.apache.poi.hssf.record.*;
+
import org.apache.poi.util.POILogger;
import org.apache.poi.util.POILogFactory;
+import org.apache.poi.hssf.record.*;
+import org.apache.poi.hssf.util.SheetReferences;
+
/**
* Workbook
* Low level model implementation of a Workbook. Provides creational methods
public class Workbook {
private static final int DEBUG = POILogger.DEBUG;
- public static Workbook currentBook = null;
+// public static Workbook currentBook = null;
/**
* constant used to set the "codepage" wherever "codepage" is set in records
protected SSTRecord sst = null;
/**
- * Holds the Extern Sheet with referenced to bound sheets
+ * Holds the Extern Sheet with references to bound sheets
*/
protected ExternSheetRecord externSheet= null;
protected Record createEOF() {
return new EOFRecord();
}
+
+ public SheetReferences getSheetReferences() {
+ SheetReferences refs = new SheetReferences();
+
+ if (externSheet != null) {
+ for (int k = 0; k < externSheet.getNumOfREFStructures(); k++) {
+ String sheetName = findSheetNameFromExternSheet((short)k);
+ refs.addSheetReference(sheetName, k);
+ }
+ }
+ return refs;
+ }
/** fins the sheet name by his extern sheet index
* @param num extern sheet index
}
}
- /** sets the number of the REF structors , that is in Excel file
+ /**
+ * sets the number of the REF structors , that is in Excel file
* @param numStruct number of REF structs
*/
public void setNumOfREFStructures(short numStruct) {
field_1_number_of_REF_sturcutres = numStruct;
}
- /** return the number of the REF structors , that is in Excel file
+ /**
+ * return the number of the REF structors , that is in Excel file
* @return number of REF structs
*/
public short getNumOfREFStructures() {
return field_1_number_of_REF_sturcutres;
}
- /** adds REF struct (ExternSheetSubRecord)
+ /**
+ * adds REF struct (ExternSheetSubRecord)
* @param rec REF struct
*/
public void addREFRecord(ExternSheetSubRecord rec) {
field_2_REF_structures.add(rec);
}
- /** returns the number of REF Record , which is in model
+ /** returns the number of REF Records, which is in model
* @return number of REF records
*/
- public int getNumOfREFRecord() {
+ public int getNumOfREFRecords() {
return field_2_REF_structures.size();
}
- /** return the REF record (ExternSheetSubRecord)
+ /** returns the REF record (ExternSheetSubRecord)
* @param elem index to place
* @return REF record
*/
buffer.append("[EXTERNSHEET]\n");
buffer.append(" numOfRefs = ").append(getNumOfREFStructures()).append("\n");
- for (int k=0; k < this.getNumOfREFRecord(); k++) {
+ for (int k=0; k < this.getNumOfREFRecords(); k++) {
buffer.append("refrec #").append(k).append('\n');
buffer.append(getREFRecordAt(k).toString());
buffer.append("----refrec #").append(k).append('\n');
*/
public int serialize(int offset, byte [] data) {
LittleEndian.putShort(data, 0 + offset, sid);
- LittleEndian.putShort(data, 2 + offset,(short)(2 + (getNumOfREFRecord() *6)));
+ LittleEndian.putShort(data, 2 + offset,(short)(2 + (getNumOfREFRecords() *6)));
LittleEndian.putShort(data, 4 + offset, getNumOfREFStructures());
int pos = 6 ;
- for (int k = 0; k < getNumOfREFRecord(); k++) {
+ for (int k = 0; k < getNumOfREFRecords(); k++) {
ExternSheetSubRecord record = getREFRecordAt(k);
System.arraycopy(record.serialize(), 0, data, pos + offset, 6);
}
public int getRecordSize() {
- return 4 + 2 + getNumOfREFRecord() * 6;
+ return 4 + 2 + getNumOfREFRecords() * 6;
}
/**
import org.apache.poi.hssf.record.formula.Ref3DPtg;
import java.util.List;
import org.apache.poi.hssf.util.RangeAddress;
+import org.apache.poi.hssf.util.SheetReferences;
/**
* Title: Name Record (aka Named Range) <P>
/** gets the reference , the area only (range)
* @return area reference
*/
- public String getAreaReference(){
+ public String getAreaReference(SheetReferences refs){
if (field_13_name_definition == null) return "#REF!";
Ptg ptg = (Ptg) field_13_name_definition.peek();
String result = "";
if (ptg.getClass() == Area3DPtg.class){
- result = ((Area3DPtg) ptg).toFormulaString();
+ result = ((Area3DPtg) ptg).toFormulaString(refs);
} else if (ptg.getClass() == Ref3DPtg.class){
- result = ((Ref3DPtg) ptg).toFormulaString();
+ result = ((Ref3DPtg) ptg).toFormulaString(refs);
}
return result;
package org.apache.poi.hssf.record.formula;
import org.apache.poi.util.BinaryTree;
+import org.apache.poi.hssf.util.SheetReferences;
+
/**
* This class provides the base functionality for Excel sheet functions
* There are two kinds of function Ptgs - tFunc and tFuncVar
return lookupName(field_2_fnc_index);
}
- public String toFormulaString() {
+ public String toFormulaString(SheetReferences refs) {
return getName();
}
return returnClass;
}
- protected byte getParameterClass(int index) {
+ public byte getParameterClass(int index) {
try {
return paramClass[index];
} catch (ArrayIndexOutOfBoundsException aioobe) {
import java.util.List;
+import org.apache.poi.hssf.util.SheetReferences;
+
/**
* Addition operator PTG the "+" binomial operator. If you need more
* explanation than that then well...We really can't help you here.
/** Creates new AddPtg */
- protected AddPtg()
+ public AddPtg()
{
}
}
/** Implementation of method from Ptg */
- public String toFormulaString()
+ public String toFormulaString(SheetReferences refs)
{
return "+";
}
StringBuffer buffer = new StringBuffer();
buffer.append(operands[ 0 ]);
- buffer.append(toFormulaString());
+ buffer.append(ADD);
buffer.append(operands[ 1 ]);
return buffer.toString();
}
import org.apache.poi.hssf.util.RangeAddress;
import org.apache.poi.hssf.util.AreaReference;
import org.apache.poi.hssf.util.CellReference;
+import org.apache.poi.hssf.util.SheetReferences;
import org.apache.poi.hssf.model.Workbook;
import org.apache.poi.util.BitField;
/** Creates new AreaPtg */
public Area3DPtg() {}
- protected Area3DPtg(String arearef, short externIdx) {
+ public Area3DPtg(String arearef, short externIdx) {
AreaReference ar = new AreaReference(arearef);
setFirstRow((short)ar.getCells()[0].getRow());
}
- public String toFormulaString()
+ public String toFormulaString(SheetReferences refs)
{
StringBuffer retval = new StringBuffer();
- Object book = Workbook.currentBook;
- if (book != null) {
- retval.append(((Workbook) book).findSheetNameFromExternSheet(this.field_1_index_extern_sheet));
+ if (refs != null) {
+ retval.append(refs.getSheetName(this.field_1_index_extern_sheet));
retval.append('!');
}
retval.append((new CellReference(getFirstRow(),getFirstColumn(),!isFirstRowRelative(),!isFirstColRelative())).toString());
import org.apache.poi.hssf.util.AreaReference;
import org.apache.poi.hssf.util.CellReference;
+import org.apache.poi.hssf.util.SheetReferences;
/**
* Specifies a rectangular area of cells A1:A4 for instance.
- protected AreaPtg(String arearef) {
+ public AreaPtg(String arearef) {
AreaReference ar = new AreaReference(arearef);
setFirstRow((short)ar.getCells()[0].getRow());
setFirstColumn((short)ar.getCells()[0].getCol());
field_4_last_column = column;
}
- public String toFormulaString()
+ public String toFormulaString(SheetReferences refs)
{
return (new CellReference(getFirstRow(),getFirstColumn(),!isFirstRowRelative(),!isFirstColRelative())).toString() + ":" +
(new CellReference(getLastRow(),getLastColumn(),!isLastRowRelative(),!isLastColRelative())).toString();
*/
package org.apache.poi.hssf.record.formula;
+import org.apache.poi.hssf.util.SheetReferences;
+
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.BitField;
return operands[ 0 ];
}
else {
- return toFormulaString() + "(" + operands[ 0 ] + ")";
+ return toFormulaString((SheetReferences)null) + "(" + operands[ 0 ] + ")";
}
}
return -1;
}
- public String toFormulaString() {
+ public String toFormulaString(SheetReferences refs) {
if(semiVolatile.isSet(field_1_options)) {
return "ATTR(semiVolatile)";
}
import java.util.List;
+import org.apache.poi.hssf.util.SheetReferences;
+
/**
*
* @author andy
// doesn't need anything
}
- protected ConcatPtg() {
+ public ConcatPtg() {
}
return 2;
}
- public String toFormulaString()
+ public String toFormulaString(SheetReferences refs)
{
return CONCAT;
}
import java.util.List;
+import org.apache.poi.hssf.util.SheetReferences;
+
/**
- *
- * @author andy
+ * This PTG implements the standard binomial divide "/"
+ * @author Andrew C. Oliver acoliver at apache dot org
*/
public class DividePtg
/** Creates new AddPtg */
- protected DividePtg()
+ public DividePtg()
{
}
return 2;
}
- public String toFormulaString()
+ public String toFormulaString(SheetReferences refs)
{
return "/";
}
StringBuffer buffer = new StringBuffer();
buffer.append(operands[ 0 ]);
- buffer.append(toFormulaString());
+ buffer.append(toFormulaString((SheetReferences)null));
buffer.append(operands[ 1 ]);
return buffer.toString();
}
*/
package org.apache.poi.hssf.record.formula;
+import org.apache.poi.hssf.util.SheetReferences;
+
/**
*
* @author andy
/** Creates new ExpPtg */
- protected ExpPtg()
+ public ExpPtg()
{
}
return SIZE;
}
- public String toFormulaString()
+ public String toFormulaString(SheetReferences refs)
{
return "NO IDEA SHARED FORMULA EXP PTG";
}
/**
* Create a function ptg from a string tokenised by the parser
*/
- protected FuncVarPtg(String pName, byte pNumOperands) {
+ public FuncVarPtg(String pName, byte pNumOperands) {
field_1_num_args = pNumOperands;
field_2_fnc_index = lookupIndex(pName);
try{
package org.apache.poi.hssf.record.formula;
import org.apache.poi.util.LittleEndian;
+import org.apache.poi.hssf.util.SheetReferences;
/**
* Integer (short intger)
* Stores a (java) short value in a formula
- * @author andy
+ * @author Andrew C. Oliver (acoliver at apache dot org)
*/
public class IntPtg
// IntPtg should be able to create itself, shouldnt have to call setValue
- protected IntPtg(String formulaToken) {
+ public IntPtg(String formulaToken) {
setValue(Short.parseShort(formulaToken));
}
return SIZE;
}
- public String toFormulaString()
+ public String toFormulaString(SheetReferences refs)
{
return "" + getValue();
}
package org.apache.poi.hssf.record.formula;
import org.apache.poi.util.LittleEndian;
+import org.apache.poi.hssf.util.SheetReferences;
/**
*
return SIZE;
}
- public String toFormulaString()
+ public String toFormulaString(SheetReferences refs)
{
return "ERR#";
}
package org.apache.poi.hssf.record.formula;
+import org.apache.poi.hssf.util.SheetReferences;
/**
* Missing Function Arguments
private final static int SIZE = 1;
public final static byte sid = 0x16;
- protected MissingArgPtg()
+ public MissingArgPtg()
{
}
}
- public String toFormulaString()
+ public String toFormulaString(SheetReferences refs)
{
return " ";
}
package org.apache.poi.hssf.record.formula;
import java.util.List;
+import org.apache.poi.hssf.util.SheetReferences;
/**
- *
- * @author andy
+ * Implements the standard mathmatical multiplication - *
+ * @author Andrew C. Oliver (acoliver at apache dot org)
*/
public class MultiplyPtg
/** Creates new AddPtg */
- protected MultiplyPtg()
+ public MultiplyPtg()
{
}
}
- public String toFormulaString()
+ public String toFormulaString(SheetReferences refs)
{
return "*";
}
{
StringBuffer buffer = new StringBuffer();
- buffer.append(operands[ 0 ].toFormulaString());
+ buffer.append(operands[ 0 ].toFormulaString((SheetReferences)null));
buffer.append("*");
- buffer.append(operands[ 1 ].toFormulaString());
+ buffer.append(operands[ 1 ].toFormulaString((SheetReferences)null));
return buffer.toString();
}
StringBuffer buffer = new StringBuffer();
buffer.append(operands[ 0 ]);
- buffer.append(toFormulaString());
+ buffer.append(toFormulaString((SheetReferences)null));
buffer.append(operands[ 1 ]);
return buffer.toString();
}
package org.apache.poi.hssf.record.formula;
import org.apache.poi.util.LittleEndian;
+import org.apache.poi.hssf.util.SheetReferences;
/**
*
/** Creates new NamePtg */
- protected NamePtg(String name)
+ public NamePtg(String name)
{
//TODO
}
return SIZE;
}
- public String toFormulaString()
+ public String toFormulaString(SheetReferences refs)
{
return "NO IDEA - NAME";
}
package org.apache.poi.hssf.record.formula;
import org.apache.poi.util.LittleEndian;
-
+import org.apache.poi.hssf.util.SheetReferences;
/**
* Number
* Stores a floating point value in a formula
* that calls this method.
* @param value : String representation of a floating point number
*/
- protected NumberPtg(String value) {
+ public NumberPtg(String value) {
setValue(Double.parseDouble(value));
}
return SIZE;
}
- public String toFormulaString()
+ public String toFormulaString(SheetReferences refs)
{
return "" + getValue();
}
import java.util.List;
+import org.apache.poi.hssf.util.SheetReferences;
+
/**
* While formula tokens are stored in RPN order and thus do not need parenthesis for
* precedence reasons, Parenthesis tokens ARE written to ensure that user entered
private final static int SIZE = 1;
public final static byte sid = 0x15;
- protected ParenthesisPtg()
+ public ParenthesisPtg()
{
}
return 1;
}
- public String toFormulaString()
+ public String toFormulaString(SheetReferences refs)
{
return "()";
}
import java.util.List;
+import org.apache.poi.hssf.util.SheetReferences;
+
/**
*
* @author andy
/** Creates new AddPtg */
- protected PowerPtg()
+ public PowerPtg()
{
}
return 2;
}
- public String toFormulaString()
+ public String toFormulaString(SheetReferences refs)
{
return "^";
}
buffer.append(operands[ 0 ]);
- buffer.append(toFormulaString());
+ buffer.append(toFormulaString((SheetReferences)null));
buffer.append(operands[ 1 ]);
return buffer.toString();
}
import java.util.List;
import java.util.ArrayList;
+import org.apache.poi.hssf.util.SheetReferences;
+
/**
*
* @author andy
/**
* return a string representation of this token alone
*/
- public abstract String toFormulaString();
+ public abstract String toFormulaString(SheetReferences refs);
/**
* dump a debug representation (hexdump) to a strnig
*/
package org.apache.poi.hssf.record.formula;
import org.apache.poi.util.LittleEndian;
+
import org.apache.poi.hssf.util.RangeAddress;
import org.apache.poi.hssf.util.CellReference;
+import org.apache.poi.hssf.util.SheetReferences;
import org.apache.poi.util.BitField;
import org.apache.poi.hssf.model.Workbook;
field_3_column = LittleEndian.getShort(data, 4 + offset);
}
- protected Ref3DPtg(String cellref, short externIdx ) {
+ public Ref3DPtg(String cellref, short externIdx ) {
CellReference c= new CellReference(cellref);
setRow((short) c.getRow());
setColumn((short) c.getCol());
}
- public String toFormulaString() {
+ public String toFormulaString(SheetReferences refs) {
StringBuffer retval = new StringBuffer();
- Object book = Workbook.currentBook;
- if (book != null) {
- retval.append(((Workbook) book).findSheetNameFromExternSheet(this.field_1_index_extern_sheet));
+ if (refs != null) {
+ retval.append(refs.getSheetName((int)this.field_1_index_extern_sheet));
retval.append('!');
}
retval.append((new CellReference(getRow(),getColumn(),!isRowRelative(),!isColRelative())).toString());
import org.apache.poi.util.BitField;
import org.apache.poi.hssf.util.CellReference;
+import org.apache.poi.hssf.util.SheetReferences;
/**
* ReferencePtg - handles references (such as A1, A2, IA4)
* Takes in a String represnetation of a cell reference and fills out the
* numeric fields.
*/
- protected ReferencePtg(String cellref) {
+ public ReferencePtg(String cellref) {
CellReference c= new CellReference(cellref);
setRow((short) c.getRow());
setColumn((short) c.getCol());
return SIZE;
}
- public String toFormulaString()
+ public String toFormulaString(SheetReferences refs)
{
//TODO -- should we store a cellreference instance in this ptg?? but .. memory is an issue, i believe!
return (new CellReference(getRow(),getColumn(),!isRowRelative(),!isColRelative())).toString();
import org.apache.poi.util.LittleEndian;
+import org.apache.poi.hssf.util.SheetReferences;
+
/**
* Number
* Stores a String value in a formula value stored in the format <length 2 bytes>char[]
* that calls this method.
* @param value : String representation of a floating point number
*/
- protected StringPtg(String value) {
+ public StringPtg(String value) {
setValue(value);
}
return field_1_value.length() + 3;
}
- public String toFormulaString()
+ public String toFormulaString(SheetReferences refs)
{
return getValue();
}
package org.apache.poi.hssf.record.formula;
import java.util.List;
+import org.apache.poi.hssf.util.SheetReferences;
/**
*
public final static int SIZE = 1;
public final static byte sid = 0x04;
- protected SubtractPtg()
+ public SubtractPtg()
{
}
return 2;
}
- public String toFormulaString()
+ public String toFormulaString(SheetReferences refs)
{
return "-";
}
*/
package org.apache.poi.hssf.record.formula;
+import org.apache.poi.hssf.util.SheetReferences;
+
/**
*
* @author andy
return size;
}
- public String toFormulaString()
+ public String toFormulaString(SheetReferences refs)
{
return "UNKNOWN";
}
import org.apache.poi.hssf.model.Workbook;
import org.apache.poi.hssf.model.Sheet;
+import org.apache.poi.hssf.model.FormulaParser;
import org.apache.poi.hssf.record.CellValueRecordInterface;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.FormulaRecord;
import org.apache.poi.hssf.record.BoolErrRecord;
import org.apache.poi.hssf.record.ExtendedFormatRecord;
import org.apache.poi.hssf.record.formula.Ptg;
+import org.apache.poi.hssf.util.SheetReferences;
-import org.apache.poi.hssf.record.formula.FormulaParser;
+//import org.apache.poi.hssf.record.formula.FormulaParser;
import java.util.Date;
import java.util.Calendar;
}
public void setCellFormula(String formula) {
- Workbook.currentBook=book;
+ //Workbook.currentBook=book;
if (formula==null) {
setCellType(CELL_TYPE_BLANK,false);
} else {
rec.pushExpressionToken(ptg[ k ]);
}
rec.setExpressionLength(( short ) size);
- Workbook.currentBook = null;
+ //Workbook.currentBook = null;
}
}
public String getCellFormula() {
- Workbook.currentBook=book;
- String retval = FormulaParser.toFormulaString(((FormulaRecord)record).getParsedExpression());
- Workbook.currentBook=null;
+ //Workbook.currentBook=book;
+ SheetReferences refs = book.getSheetReferences();
+ String retval = FormulaParser.toFormulaString(refs, ((FormulaRecord)record).getParsedExpression());
+ //Workbook.currentBook=null;
return retval;
}
import java.util.Iterator;
import java.util.TreeMap;
import org.apache.poi.hssf.util.RangeAddress;
+import org.apache.poi.hssf.util.SheetReferences;
/**
* Title: High Level Represantion of Named Range <P>
*/
public String getReference() {
- Workbook.currentBook=book;
String result;
- result = name.getAreaReference();
- Workbook.currentBook=null;
+ SheetReferences refs = book.getSheetReferences();
+ result = name.getAreaReference(refs);
return result;
}