From 453fe100c58082695d3faf14fd874b5cc376c44b Mon Sep 17 00:00:00 2001 From: "Andrew C. Oliver" Date: Tue, 30 Apr 2002 01:05:49 +0000 Subject: [PATCH] got SUM working and some debug stuff. git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352563 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hssf/record/FormulaRecord.java | 5 +- .../poi/hssf/record/formula/AttrPtg.java | 12 ++-- .../hssf/record/formula/FormulaParser.java | 15 ++++- .../apache/poi/hssf/record/formula/Ptg.java | 14 ++++ src/java/org/apache/poi/util/HexDump.java | 65 +++++++++++++++++++ 5 files changed, 104 insertions(+), 7 deletions(-) diff --git a/src/java/org/apache/poi/hssf/record/FormulaRecord.java b/src/java/org/apache/poi/hssf/record/FormulaRecord.java index db8d0bb0b..a166e21e0 100644 --- a/src/java/org/apache/poi/hssf/record/FormulaRecord.java +++ b/src/java/org/apache/poi/hssf/record/FormulaRecord.java @@ -534,8 +534,9 @@ public class FormulaRecord for (int k = 0; k < field_8_parsed_expr.size(); k++ ) { - buffer.append("formula ").append(k).append(" ") - .append(((Ptg)field_8_parsed_expr.get(k)).toFormulaString()); +/* buffer.append("formula ").append(k).append(" ") + .append(((Ptg)field_8_parsed_expr.get(k)).toFormulaString());*/ + ((Ptg)field_8_parsed_expr.get(k)).toDebugString(); } diff --git a/src/java/org/apache/poi/hssf/record/formula/AttrPtg.java b/src/java/org/apache/poi/hssf/record/formula/AttrPtg.java index fba66eda1..1bd1b68c2 100644 --- a/src/java/org/apache/poi/hssf/record/formula/AttrPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/AttrPtg.java @@ -138,6 +138,10 @@ public class AttrPtg { return sum.isSet(getOptions()); } + + public void setSum(boolean bsum) { + field_1_options=sum.setByteBoolean(field_1_options,bsum); + } // lets hope no one uses this anymore public boolean isBaxcel() @@ -180,6 +184,9 @@ public class AttrPtg public void writeBytes(byte [] array, int offset) { + array[offset]=sid; + array[offset+1]=field_1_options; + LittleEndian.putShort(array,offset+2,field_2_data); } public int getSize() @@ -206,10 +213,7 @@ public class AttrPtg { return -1; } - - public void manipulate(List source, List results, int pos) { - } - + public String toFormulaString(String[] operands) { return "SUM(" + operands[ 0 ] + ")"; } 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 54122c0d3..a0f52a50a 100644 --- a/src/java/org/apache/poi/hssf/record/formula/FormulaParser.java +++ b/src/java/org/apache/poi/hssf/record/formula/FormulaParser.java @@ -244,7 +244,7 @@ public class FormulaParser { int numArgs = Arguments(); Match(')'); //this is the end of the function - tokens.add(new FunctionPtg(name,(byte)numArgs)); + tokens.add(function(name,(byte)numArgs)); } else if (Look == ':') { // this is a AreaReference String first = name; Match(':'); @@ -261,6 +261,19 @@ public class FormulaParser { } } + private Ptg function(String name,byte numArgs) { + Ptg retval = null; + + if (numArgs == 1 && name.equals("SUM")) { + AttrPtg ptg = new AttrPtg(); + ptg.setData((short)1); //sums don't care but this is what excel does. + ptg.setSum(true); + retval = ptg; + } + + return retval; + } + /** get arguments to a function */ private int Arguments() { int numArgs = 0; diff --git a/src/java/org/apache/poi/hssf/record/formula/Ptg.java b/src/java/org/apache/poi/hssf/record/formula/Ptg.java index 202efbb2d..da1dcc990 100644 --- a/src/java/org/apache/poi/hssf/record/formula/Ptg.java +++ b/src/java/org/apache/poi/hssf/record/formula/Ptg.java @@ -296,5 +296,19 @@ public abstract class Ptg return 0; } + /** + * dump a debug representation (hexdump) to a strnig + */ + public void toDebugString() { + byte[] ba = new byte[getSize()]; + String retval=null; + writeBytes(ba,0); + try { + retval = org.apache.poi.util.HexDump.dump(ba,0,0); + } catch (Exception e) { + e.printStackTrace(); + } + } + } diff --git a/src/java/org/apache/poi/util/HexDump.java b/src/java/org/apache/poi/util/HexDump.java index 4e0cfd35a..49e1c8352 100644 --- a/src/java/org/apache/poi/util/HexDump.java +++ b/src/java/org/apache/poi/util/HexDump.java @@ -148,6 +148,71 @@ public class HexDump } } + /** + * dump an array of bytes to a String + * + * @param data the byte array to be dumped + * @param offset its offset, whatever that might mean + * @param index initial index into the byte array + * + * @exception IOException is thrown if anything goes wrong writing + * the data to stream + * @exception ArrayIndexOutOfBoundsException if the index is + * outside the data array's bounds + * @return output string + */ + + public static String dump(final byte [] data, final long offset, + final int index) { + StringBuffer buffer; + if ((index < 0) || (index >= data.length)) + { + throw new ArrayIndexOutOfBoundsException( + "illegal index: " + index + " into array of length " + + data.length); + } + long display_offset = offset + index; + buffer = new StringBuffer(74); + + for (int j = index; j < data.length; j += 16) + { + int chars_read = data.length - j; + + if (chars_read > 16) + { + chars_read = 16; + } + buffer.append(dump(display_offset)).append(' '); + for (int k = 0; k < 16; k++) + { + if (k < chars_read) + { + buffer.append(dump(data[ k + j ])); + } + else + { + buffer.append(" "); + } + buffer.append(' '); + } + for (int k = 0; k < chars_read; k++) + { + if ((data[ k + j ] >= ' ') && (data[ k + j ] < 127)) + { + buffer.append(( char ) data[ k + j ]); + } + else + { + buffer.append('.'); + } + } + buffer.append(EOL); + display_offset += chars_read; + } + return buffer.toString(); + } + + public static final String EOL = System.getProperty("line.separator"); private static final StringBuffer _lbuffer = new StringBuffer(8);