From ba9adbac758cb4566195ba494bb57300a84b2877 Mon Sep 17 00:00:00 2001 From: Avik Sengupta Date: Sat, 23 Aug 2003 19:40:05 +0000 Subject: [PATCH] Facility to use Macro functions in formulas. Major functionality upgrade, thanks to Paul Krause Some of the Named Range and Area3d functionality can now be enhanced using the features of this patch. git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/branches/REL_2_BRANCH@353315 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hssf/dev/FormulaViewer.java | 7 +- .../apache/poi/hssf/model/FormulaParser.java | 107 ++++++++++-------- .../apache/poi/hssf/record/NameRecord.java | 71 ++++++++++-- .../record/formula/AbstractFunctionPtg.java | 12 +- .../poi/hssf/record/formula/AddPtg.java | 4 +- .../poi/hssf/record/formula/Area3DPtg.java | 4 +- .../poi/hssf/record/formula/AreaPtg.java | 4 +- .../poi/hssf/record/formula/AttrPtg.java | 10 +- .../poi/hssf/record/formula/BoolPtg.java | 4 +- .../poi/hssf/record/formula/ConcatPtg.java | 4 +- .../poi/hssf/record/formula/DividePtg.java | 6 +- .../poi/hssf/record/formula/EqualPtg.java | 6 +- .../poi/hssf/record/formula/ExpPtg.java | 4 +- .../hssf/record/formula/GreaterEqualPtg.java | 6 +- .../hssf/record/formula/GreaterThanPtg.java | 6 +- .../poi/hssf/record/formula/IntPtg.java | 4 +- .../poi/hssf/record/formula/LessEqualPtg.java | 6 +- .../poi/hssf/record/formula/LessThanPtg.java | 7 +- .../poi/hssf/record/formula/MemErrPtg.java | 4 +- .../poi/hssf/record/formula/MemFuncPtg.java | 4 +- .../hssf/record/formula/MissingArgPtg.java | 6 +- .../poi/hssf/record/formula/MultiplyPtg.java | 10 +- .../poi/hssf/record/formula/NamePtg.java | 27 ++++- .../poi/hssf/record/formula/NameXPtg.java | 4 +- .../poi/hssf/record/formula/NotEqualPtg.java | 6 +- .../poi/hssf/record/formula/NumberPtg.java | 5 +- .../hssf/record/formula/ParenthesisPtg.java | 5 +- .../poi/hssf/record/formula/PowerPtg.java | 6 +- .../apache/poi/hssf/record/formula/Ptg.java | 4 +- .../poi/hssf/record/formula/Ref3DPtg.java | 4 +- .../poi/hssf/record/formula/ReferencePtg.java | 4 +- .../poi/hssf/record/formula/StringPtg.java | 5 +- .../poi/hssf/record/formula/SubtractPtg.java | 4 +- .../hssf/record/formula/UnaryMinusPtg.java | 4 +- .../poi/hssf/record/formula/UnaryPlusPtg.java | 4 +- .../poi/hssf/record/formula/UnionPtg.java | 4 +- .../poi/hssf/record/formula/UnknownPtg.java | 4 +- .../apache/poi/hssf/usermodel/HSSFCell.java | 3 +- .../apache/poi/hssf/usermodel/HSSFName.java | 4 +- .../poi/hssf/usermodel/HSSFWorkbook.java | 2 +- .../poi/hssf/model/TestFormulaParser.java | 16 ++- .../poi/hssf/usermodel/TestFormulas.java | 58 +++++++++- 42 files changed, 301 insertions(+), 168 deletions(-) diff --git a/src/java/org/apache/poi/hssf/dev/FormulaViewer.java b/src/java/org/apache/poi/hssf/dev/FormulaViewer.java index 1e7fd95965..3443fda1a6 100644 --- a/src/java/org/apache/poi/hssf/dev/FormulaViewer.java +++ b/src/java/org/apache/poi/hssf/dev/FormulaViewer.java @@ -77,7 +77,6 @@ import org.apache.poi.hssf.record.*; 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 @@ -144,7 +143,7 @@ public class FormulaViewer StringBuffer buf = new StringBuffer(); if (token instanceof ExpPtg) return; - buf.append(name=((OperationPtg) token).toFormulaString((SheetReferences)null)); + buf.append(name=((OperationPtg) token).toFormulaString((Workbook)null)); buf.append(sep); switch (token.getPtgClass()) { case Ptg.CLASS_REF : @@ -213,7 +212,7 @@ public class FormulaViewer StringBuffer buf = new StringBuffer(); for (int i=0;i0) { - o = (OperationPtg) ptgs[i]; + stack.push(ptgs[0].toFormulaString(book)); - if (o instanceof AttrPtg && ((AttrPtg)o).isOptimizedIf()) { - ifptg=(AttrPtg)o; - } else { + for (int i = 1; i < ptgs.length; i++) { + if (! (ptgs[i] instanceof OperationPtg)) { + stack.push(ptgs[i].toFormulaString(book)); + continue; + } - numOperands = o.getNumberOfOperands(); - operands = new String[numOperands]; + if (ptgs[i] instanceof AttrPtg && ((AttrPtg) ptgs[i]).isOptimizedIf()) { + ifptg = (AttrPtg) ptgs[i]; + continue; + } - for (int j=0;j 0; j--) { + //TODO: catch stack underflow and throw parse exception. + operands[j - 1] = (String) stack.pop(); } - if ( (o instanceof AbstractFunctionPtg) && - ((AbstractFunctionPtg)o).getName().equals("specialflag") && - ifptg != null - ) { + stack.push(o.toFormulaString(operands)); + if (!(o instanceof AbstractFunctionPtg)) continue; + + final AbstractFunctionPtg f = (AbstractFunctionPtg) o; + final String fname = f.getName(); + if (fname == null) continue; + + if ((ifptg != null) && (fname.equals("specialflag"))) { // this special case will be way different. - result = ifptg.toFormulaString( - new String[] {(o.toFormulaString(operands))} - ); - ifptg = null; - } else { - result = o.toFormulaString(operands); + stack.push(ifptg.toFormulaString(new String[]{(String) stack.pop()})); + continue; } - stack.push(result); + if (fname.equals("externalflag")) { + final String top = (String) stack.pop(); + final int paren = top.indexOf('('); + final int comma = top.indexOf(','); + if (comma == -1) { + final int rparen = top.indexOf(')'); + stack.push(top.substring(paren + 1, rparen) + "()"); } - - - } else { - stack.push(ptgs[i].toFormulaString(refs)); + else { + stack.push(top.substring(paren + 1, comma) + '(' + + top.substring(comma + 1)); } } - return (String) stack.pop(); //TODO: catch stack underflow and throw parse exception. } + // TODO: catch stack underflow and throw parse exception. + return (String) stack.pop(); + } + + /** Create a tree representation of the RPN token array *used to run the class(RVA) change algo */ @@ -890,11 +909,9 @@ end; * Useful for testing */ public String toString() { - SheetReferences refs = null; - if (book!=null) book.getSheetReferences(); StringBuffer buf = new StringBuffer(); for (int i=0;i> 4); + } + public void setEqualsToIndexToSheet(short value) { field_6_equals_to_index_to_sheet = value; @@ -409,11 +426,47 @@ public class NameRecord extends Record { return field_11_compressed_unicode_flag; } + /** + * @return true if name is hidden + */ + public boolean isHiddenName() { + return (field_1_option_flag & OPT_HIDDEN_NAME) != 0; + } + + /** + * @return true if name is a function + */ + public boolean isFunctionName() { + return (field_1_option_flag & OPT_FUNCTION_NAME) != 0; + } + + /** + * @return true if name is a command + */ + public boolean isCommandName() { + return (field_1_option_flag & OPT_COMMAND_NAME) != 0; + } + + /** + * @return true if function macro or command macro + */ + public boolean isMacro() { + return (field_1_option_flag & OPT_MACRO) != 0; + } + + /** + * @return true if array formula or user defined + */ + public boolean isComplexFunction() { + return (field_1_option_flag & OPT_COMPLEX) != 0; + } + + /**Convenience Function to determine if the name is a built-in name */ public boolean isBuiltInName() { - return ((this.getOptionFlag() & (short)0x20) != 0); + return ((this.getOptionFlag() & OPT_BUILTIN) != 0); } @@ -511,7 +564,7 @@ public class NameRecord extends Record { data[18 + offset] = getCompressedUnicodeFlag(); /* temp: gjs - if ( ( field_1_option_flag & (short) 0x20 ) != 0 ) + if (isBuiltInName()) { LittleEndian.putShort( data, 2 + offset, (short) ( 16 + field_13_raw_name_definition.length ) ); @@ -647,16 +700,16 @@ public class NameRecord extends Record { /** gets the reference , the area only (range) * @return area reference */ - public String getAreaReference(SheetReferences refs){ + public String getAreaReference(Workbook book){ if (field_13_name_definition == null) return "#REF!"; Ptg ptg = (Ptg) field_13_name_definition.peek(); String result = ""; if (ptg.getClass() == Area3DPtg.class){ - result = ptg.toFormulaString(refs); + result = ptg.toFormulaString(book); } else if (ptg.getClass() == Ref3DPtg.class){ - result = ptg.toFormulaString(refs); + result = ptg.toFormulaString(book); } return result; @@ -727,7 +780,7 @@ public class NameRecord extends Record { /* temp: gjs - if ( ( field_1_option_flag & (short)0x20 ) != 0 ) { + if (isBuiltInName()) { // DEBUG // System.out.println( "Built-in name" ); diff --git a/src/java/org/apache/poi/hssf/record/formula/AbstractFunctionPtg.java b/src/java/org/apache/poi/hssf/record/formula/AbstractFunctionPtg.java index da08a6dc3d..cd2660219e 100644 --- a/src/java/org/apache/poi/hssf/record/formula/AbstractFunctionPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/AbstractFunctionPtg.java @@ -54,9 +54,8 @@ package org.apache.poi.hssf.record.formula; import org.apache.poi.util.BinaryTree; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; -import java.util.Stack; /** * This class provides the base functionality for Excel sheet functions @@ -69,7 +68,7 @@ public abstract class AbstractFunctionPtg extends OperationPtg { //constant used allow a ptgAttr to be mapped properly for its functionPtg public static final String ATTR_NAME = "specialflag"; - + public static final short INDEX_EXTERNAL = 255; private static BinaryTree map = produceHash(); protected static Object[][] functionData = produceFunctionData(); @@ -104,7 +103,7 @@ public abstract class AbstractFunctionPtg extends OperationPtg { return lookupName(field_2_fnc_index); } - public String toFormulaString(SheetReferences refs) { + public String toFormulaString(Workbook book) { return getName(); } @@ -140,7 +139,9 @@ public abstract class AbstractFunctionPtg extends OperationPtg { } protected short lookupIndex(String name) { - return (short)((Integer)map.getKeyForValue(name)).intValue(); + Integer index = (Integer) map.getKeyForValue(name); + if (index != null) return index.shortValue(); + return INDEX_EXTERNAL; } /** @@ -389,6 +390,7 @@ public abstract class AbstractFunctionPtg extends OperationPtg { dmap.put(new Integer(252),"FREQUENCY"); dmap.put(new Integer(253),"ADDTOOLBAR"); dmap.put(new Integer(254),"DELETETOOLBAR"); + dmap.put(new Integer(255),"externalflag"); dmap.put(new Integer(256),"RESETTOOLBAR"); dmap.put(new Integer(257),"EVALUATE"); dmap.put(new Integer(258),"GETTOOLBAR"); diff --git a/src/java/org/apache/poi/hssf/record/formula/AddPtg.java b/src/java/org/apache/poi/hssf/record/formula/AddPtg.java index ead8840677..3a519dceb4 100644 --- a/src/java/org/apache/poi/hssf/record/formula/AddPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/AddPtg.java @@ -62,7 +62,7 @@ package org.apache.poi.hssf.record.formula; import java.util.List; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; /** * Addition operator PTG the "+" binomial operator. If you need more @@ -113,7 +113,7 @@ public class AddPtg } /** Implementation of method from Ptg */ - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { return "+"; } diff --git a/src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java b/src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java index 311ba89399..59c405c1e6 100644 --- a/src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java @@ -291,8 +291,9 @@ public class Area3DPtg extends Ptg } - public String toFormulaString( SheetReferences refs ) + public String toFormulaString(Workbook book) { + SheetReferences refs = book == null ? null : book.getSheetReferences(); StringBuffer retval = new StringBuffer(); if ( refs != null ) { @@ -352,4 +353,3 @@ public class Area3DPtg extends Ptg } - diff --git a/src/java/org/apache/poi/hssf/record/formula/AreaPtg.java b/src/java/org/apache/poi/hssf/record/formula/AreaPtg.java index 4f994151d6..3189a2c856 100644 --- a/src/java/org/apache/poi/hssf/record/formula/AreaPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/AreaPtg.java @@ -65,7 +65,7 @@ import org.apache.poi.util.BitField; 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; /** * Specifies a rectangular area of cells A1:A4 for instance. @@ -305,7 +305,7 @@ public class AreaPtg field_4_last_column = column; } - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { return (new CellReference(getFirstRow(),getFirstColumn(),!isFirstRowRelative(),!isFirstColRelative())).toString() + ":" + (new CellReference(getLastRow(),getLastColumn(),!isLastRowRelative(),!isLastColRelative())).toString(); 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 d389094d7e..ebbfcba877 100644 --- a/src/java/org/apache/poi/hssf/record/formula/AttrPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/AttrPtg.java @@ -60,7 +60,7 @@ */ package org.apache.poi.hssf.record.formula; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.BitField; @@ -207,11 +207,11 @@ public class AttrPtg if(space.isSet(field_1_options)) { return operands[ 0 ]; } else if (optiIf.isSet(field_1_options)) { - return toFormulaString((SheetReferences)null) + "(" + operands[ 0 ] +")"; + return toFormulaString((Workbook)null) + "(" + operands[ 0 ] +")"; } else if (optGoto.isSet(field_1_options)) { - return toFormulaString((SheetReferences)null) + operands[0]; //goto isn't a real formula element should not show up + return toFormulaString((Workbook)null) + operands[0]; //goto isn't a real formula element should not show up } else { - return toFormulaString((SheetReferences)null) + "(" + operands[ 0 ] + ")"; + return toFormulaString((Workbook)null) + "(" + operands[ 0 ] + ")"; } } @@ -226,7 +226,7 @@ public class AttrPtg return -1; } - public String toFormulaString(SheetReferences refs) { + public String toFormulaString(Workbook book) { if(semiVolatile.isSet(field_1_options)) { return "ATTR(semiVolatile)"; } diff --git a/src/java/org/apache/poi/hssf/record/formula/BoolPtg.java b/src/java/org/apache/poi/hssf/record/formula/BoolPtg.java index 954eb0f80c..84ad7cfca5 100644 --- a/src/java/org/apache/poi/hssf/record/formula/BoolPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/BoolPtg.java @@ -60,7 +60,7 @@ package org.apache.poi.hssf.record.formula; import org.apache.poi.util.LittleEndian; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; /** * Boolean (boolean) @@ -114,7 +114,7 @@ public class BoolPtg return SIZE; } - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { return field_1_value ? "TRUE" : "FALSE"; } diff --git a/src/java/org/apache/poi/hssf/record/formula/ConcatPtg.java b/src/java/org/apache/poi/hssf/record/formula/ConcatPtg.java index bed92e461a..e7249bfbc1 100644 --- a/src/java/org/apache/poi/hssf/record/formula/ConcatPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/ConcatPtg.java @@ -62,7 +62,7 @@ package org.apache.poi.hssf.record.formula; import java.util.List; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; /** * @@ -108,7 +108,7 @@ public class ConcatPtg return 2; } - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { return CONCAT; } diff --git a/src/java/org/apache/poi/hssf/record/formula/DividePtg.java b/src/java/org/apache/poi/hssf/record/formula/DividePtg.java index 124f25edef..6bb0d9420a 100644 --- a/src/java/org/apache/poi/hssf/record/formula/DividePtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/DividePtg.java @@ -62,7 +62,7 @@ package org.apache.poi.hssf.record.formula; import java.util.List; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; /** * This PTG implements the standard binomial divide "/" @@ -108,7 +108,7 @@ public class DividePtg return 2; } - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { return "/"; } @@ -117,7 +117,7 @@ public class DividePtg StringBuffer buffer = new StringBuffer(); buffer.append(operands[ 0 ]); - buffer.append(toFormulaString((SheetReferences)null)); + buffer.append(toFormulaString((Workbook)null)); buffer.append(operands[ 1 ]); return buffer.toString(); } diff --git a/src/java/org/apache/poi/hssf/record/formula/EqualPtg.java b/src/java/org/apache/poi/hssf/record/formula/EqualPtg.java index 1ddff9ac64..12438a3397 100644 --- a/src/java/org/apache/poi/hssf/record/formula/EqualPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/EqualPtg.java @@ -62,7 +62,7 @@ package org.apache.poi.hssf.record.formula; import java.util.List; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; /** * @@ -107,7 +107,7 @@ public class EqualPtg return 2; } - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { return "="; } @@ -117,7 +117,7 @@ public class EqualPtg buffer.append(operands[ 0 ]); - buffer.append(toFormulaString((SheetReferences)null)); + buffer.append(toFormulaString((Workbook)null)); buffer.append(operands[ 1 ]); return buffer.toString(); } diff --git a/src/java/org/apache/poi/hssf/record/formula/ExpPtg.java b/src/java/org/apache/poi/hssf/record/formula/ExpPtg.java index b100a1a762..d70e57968f 100644 --- a/src/java/org/apache/poi/hssf/record/formula/ExpPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/ExpPtg.java @@ -60,7 +60,7 @@ */ package org.apache.poi.hssf.record.formula; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; /** * @@ -102,7 +102,7 @@ public class ExpPtg return SIZE; } - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { return "NO IDEA SHARED FORMULA EXP PTG"; } diff --git a/src/java/org/apache/poi/hssf/record/formula/GreaterEqualPtg.java b/src/java/org/apache/poi/hssf/record/formula/GreaterEqualPtg.java index e81c1b7c81..b387a5e842 100755 --- a/src/java/org/apache/poi/hssf/record/formula/GreaterEqualPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/GreaterEqualPtg.java @@ -54,7 +54,7 @@ package org.apache.poi.hssf.record.formula; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; /** * PTG class to implement greater or equal to @@ -98,7 +98,7 @@ public class GreaterEqualPtg return 2; } - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { return ">="; } @@ -108,7 +108,7 @@ public class GreaterEqualPtg buffer.append(operands[ 0 ]); - buffer.append(toFormulaString((SheetReferences)null)); + buffer.append(toFormulaString((Workbook)null)); buffer.append(operands[ 1 ]); return buffer.toString(); } diff --git a/src/java/org/apache/poi/hssf/record/formula/GreaterThanPtg.java b/src/java/org/apache/poi/hssf/record/formula/GreaterThanPtg.java index 5f7d6450e0..04fbcac2ef 100644 --- a/src/java/org/apache/poi/hssf/record/formula/GreaterThanPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/GreaterThanPtg.java @@ -61,7 +61,7 @@ package org.apache.poi.hssf.record.formula; import java.util.List; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; /** * Greater than operator PTG ">" @@ -133,7 +133,7 @@ public class GreaterThanPtg * Implementation of method from Ptg * @param refs the Sheet References */ - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { return this.GREATERTHAN; } @@ -171,5 +171,3 @@ public class GreaterThanPtg return new GreaterThanPtg(); } } - - diff --git a/src/java/org/apache/poi/hssf/record/formula/IntPtg.java b/src/java/org/apache/poi/hssf/record/formula/IntPtg.java index 200f5dc651..e06f1ecd25 100644 --- a/src/java/org/apache/poi/hssf/record/formula/IntPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/IntPtg.java @@ -61,7 +61,7 @@ package org.apache.poi.hssf.record.formula; import org.apache.poi.util.LittleEndian; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; /** * Integer (short intger) @@ -116,7 +116,7 @@ public class IntPtg return SIZE; } - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { return "" + getValue(); } diff --git a/src/java/org/apache/poi/hssf/record/formula/LessEqualPtg.java b/src/java/org/apache/poi/hssf/record/formula/LessEqualPtg.java index 271170be45..c1c7570029 100755 --- a/src/java/org/apache/poi/hssf/record/formula/LessEqualPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/LessEqualPtg.java @@ -53,7 +53,7 @@ */ package org.apache.poi.hssf.record.formula; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; /** @@ -99,7 +99,7 @@ public class LessEqualPtg return 2; } - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { return "<="; } @@ -109,7 +109,7 @@ public class LessEqualPtg buffer.append(operands[ 0 ]); - buffer.append(toFormulaString((SheetReferences)null)); + buffer.append(toFormulaString((Workbook)null)); buffer.append(operands[ 1 ]); return buffer.toString(); } diff --git a/src/java/org/apache/poi/hssf/record/formula/LessThanPtg.java b/src/java/org/apache/poi/hssf/record/formula/LessThanPtg.java index cab99be9c3..a856b93bde 100644 --- a/src/java/org/apache/poi/hssf/record/formula/LessThanPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/LessThanPtg.java @@ -63,7 +63,7 @@ package org.apache.poi.hssf.record.formula; import java.util.List; //POI -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; /** * Less than operator PTG "<". The SID is taken from the @@ -142,7 +142,7 @@ public class LessThanPtg * Implementation of method from Ptg * @param refs the Sheet References */ - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { return this.LESSTHAN; } @@ -180,6 +180,3 @@ public class LessThanPtg } } - - - diff --git a/src/java/org/apache/poi/hssf/record/formula/MemErrPtg.java b/src/java/org/apache/poi/hssf/record/formula/MemErrPtg.java index c48ad8c3b1..31a1bcafec 100644 --- a/src/java/org/apache/poi/hssf/record/formula/MemErrPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/MemErrPtg.java @@ -61,7 +61,7 @@ package org.apache.poi.hssf.record.formula; import org.apache.poi.util.LittleEndian; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; /** * @@ -118,7 +118,7 @@ public class MemErrPtg return SIZE; } - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { return "ERR#"; } diff --git a/src/java/org/apache/poi/hssf/record/formula/MemFuncPtg.java b/src/java/org/apache/poi/hssf/record/formula/MemFuncPtg.java index 2d422b1c7e..a8b63609c4 100644 --- a/src/java/org/apache/poi/hssf/record/formula/MemFuncPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/MemFuncPtg.java @@ -60,7 +60,7 @@ package org.apache.poi.hssf.record.formula; import org.apache.poi.util.LittleEndian; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; /** * @author Glen Stampoultzis (glens at apache.org) @@ -96,7 +96,7 @@ public class MemFuncPtg extends ControlPtg LittleEndian.putShort( array, offset + 1, (short)field_1_len_ref_subexpression ); } - public String toFormulaString( SheetReferences refs ) + public String toFormulaString(Workbook book) { return ""; } diff --git a/src/java/org/apache/poi/hssf/record/formula/MissingArgPtg.java b/src/java/org/apache/poi/hssf/record/formula/MissingArgPtg.java index 3f88ded3b8..c65ab41f3d 100644 --- a/src/java/org/apache/poi/hssf/record/formula/MissingArgPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/MissingArgPtg.java @@ -54,7 +54,7 @@ package org.apache.poi.hssf.record.formula; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; /** * Missing Function Arguments @@ -91,7 +91,7 @@ public class MissingArgPtg } - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { return " "; } @@ -103,5 +103,3 @@ public class MissingArgPtg } } - - diff --git a/src/java/org/apache/poi/hssf/record/formula/MultiplyPtg.java b/src/java/org/apache/poi/hssf/record/formula/MultiplyPtg.java index 143702ce16..6b33e619b6 100644 --- a/src/java/org/apache/poi/hssf/record/formula/MultiplyPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/MultiplyPtg.java @@ -61,7 +61,7 @@ package org.apache.poi.hssf.record.formula; import java.util.List; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; /** * Implements the standard mathmatical multiplication - * @@ -114,7 +114,7 @@ public class MultiplyPtg } - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { return "*"; } @@ -123,9 +123,9 @@ public class MultiplyPtg { StringBuffer buffer = new StringBuffer(); - buffer.append(operands[ 0 ].toFormulaString((SheetReferences)null)); + buffer.append(operands[ 0 ].toFormulaString((Workbook)null)); buffer.append("*"); - buffer.append(operands[ 1 ].toFormulaString((SheetReferences)null)); + buffer.append(operands[ 1 ].toFormulaString((Workbook)null)); return buffer.toString(); } @@ -133,7 +133,7 @@ public class MultiplyPtg StringBuffer buffer = new StringBuffer(); buffer.append(operands[ 0 ]); - buffer.append(toFormulaString((SheetReferences)null)); + buffer.append(toFormulaString((Workbook)null)); buffer.append(operands[ 1 ]); return buffer.toString(); } diff --git a/src/java/org/apache/poi/hssf/record/formula/NamePtg.java b/src/java/org/apache/poi/hssf/record/formula/NamePtg.java index 3786cdd694..6cdee6af03 100644 --- a/src/java/org/apache/poi/hssf/record/formula/NamePtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/NamePtg.java @@ -61,7 +61,8 @@ package org.apache.poi.hssf.record.formula; import org.apache.poi.util.LittleEndian; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; +import org.apache.poi.hssf.record.NameRecord; /** * @@ -85,9 +86,22 @@ public class NamePtg /** Creates new NamePtg */ - public NamePtg(String name) + public NamePtg(String name, Workbook book) { - //TODO + final short n = (short) (book.getNumNames() + 1); + NameRecord rec; + for (short i = 1; i < n; i++) { + rec = book.getNameRecord(i - 1); + if (name.equals(rec.getNameText())) { + field_1_label_index = i; + return; + } + } + rec = new NameRecord(); + rec.setNameText(name); + rec.setNameTextLength((byte) name.length()); + book.addName(rec); + field_1_label_index = n; } /** Creates new NamePtg */ @@ -113,12 +127,13 @@ public class NamePtg return SIZE; } - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { - return "NAMED RANGE"; + NameRecord rec = book.getNameRecord(field_1_label_index - 1); + return rec.getNameText(); } - public byte getDefaultOperandClass() {return Ptg.CLASS_VALUE;} + public byte getDefaultOperandClass() {return Ptg.CLASS_REF;} public Object clone() { NamePtg ptg = new NamePtg(); diff --git a/src/java/org/apache/poi/hssf/record/formula/NameXPtg.java b/src/java/org/apache/poi/hssf/record/formula/NameXPtg.java index 58fa096866..9b6225d809 100644 --- a/src/java/org/apache/poi/hssf/record/formula/NameXPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/NameXPtg.java @@ -61,7 +61,7 @@ package org.apache.poi.hssf.record.formula; import org.apache.poi.util.LittleEndian; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; /** * @@ -113,7 +113,7 @@ public class NameXPtg extends Ptg return SIZE; } - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { return "NO IDEA - NAME"; } diff --git a/src/java/org/apache/poi/hssf/record/formula/NotEqualPtg.java b/src/java/org/apache/poi/hssf/record/formula/NotEqualPtg.java index 81dffd6eb0..b12b16ae97 100755 --- a/src/java/org/apache/poi/hssf/record/formula/NotEqualPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/NotEqualPtg.java @@ -56,7 +56,7 @@ package org.apache.poi.hssf.record.formula; import java.util.List; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; /** * Ptg class to implement not equal @@ -101,7 +101,7 @@ public class NotEqualPtg return 2; } - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { return "<>"; } @@ -111,7 +111,7 @@ public class NotEqualPtg buffer.append(operands[ 0 ]); - buffer.append(toFormulaString((SheetReferences)null)); + buffer.append(toFormulaString((Workbook)null)); buffer.append(operands[ 1 ]); return buffer.toString(); } diff --git a/src/java/org/apache/poi/hssf/record/formula/NumberPtg.java b/src/java/org/apache/poi/hssf/record/formula/NumberPtg.java index e071420bc5..b43c5536eb 100644 --- a/src/java/org/apache/poi/hssf/record/formula/NumberPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/NumberPtg.java @@ -55,7 +55,7 @@ package org.apache.poi.hssf.record.formula; import org.apache.poi.util.LittleEndian; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; /** * Number * Stores a floating point value in a formula @@ -113,7 +113,7 @@ public class NumberPtg return SIZE; } - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { return "" + getValue(); } @@ -125,4 +125,3 @@ public class NumberPtg return ptg; } } - diff --git a/src/java/org/apache/poi/hssf/record/formula/ParenthesisPtg.java b/src/java/org/apache/poi/hssf/record/formula/ParenthesisPtg.java index c2118f151f..2abff9e73a 100644 --- a/src/java/org/apache/poi/hssf/record/formula/ParenthesisPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/ParenthesisPtg.java @@ -57,7 +57,7 @@ package org.apache.poi.hssf.record.formula; import java.util.List; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; /** * While formula tokens are stored in RPN order and thus do not need parenthesis for @@ -107,7 +107,7 @@ public class ParenthesisPtg return 1; } - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { return "()"; } @@ -124,4 +124,3 @@ public class ParenthesisPtg } } - diff --git a/src/java/org/apache/poi/hssf/record/formula/PowerPtg.java b/src/java/org/apache/poi/hssf/record/formula/PowerPtg.java index ca7fa64863..161918abef 100644 --- a/src/java/org/apache/poi/hssf/record/formula/PowerPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/PowerPtg.java @@ -62,7 +62,7 @@ package org.apache.poi.hssf.record.formula; import java.util.List; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; /** * @@ -108,7 +108,7 @@ public class PowerPtg return 2; } - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { return "^"; } @@ -118,7 +118,7 @@ public class PowerPtg buffer.append(operands[ 0 ]); - buffer.append(toFormulaString((SheetReferences)null)); + buffer.append(toFormulaString((Workbook)null)); buffer.append(operands[ 1 ]); return buffer.toString(); } 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 70b0606134..cdf584ed1a 100644 --- a/src/java/org/apache/poi/hssf/record/formula/Ptg.java +++ b/src/java/org/apache/poi/hssf/record/formula/Ptg.java @@ -63,7 +63,7 @@ package org.apache.poi.hssf.record.formula; import java.util.List; import java.util.ArrayList; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; /** * @@ -347,7 +347,7 @@ public abstract class Ptg /** * return a string representation of this token alone */ - public abstract String toFormulaString(SheetReferences refs); + public abstract String toFormulaString(Workbook book); /** * dump a debug representation (hexdump) to a string */ diff --git a/src/java/org/apache/poi/hssf/record/formula/Ref3DPtg.java b/src/java/org/apache/poi/hssf/record/formula/Ref3DPtg.java index 13ae2ef238..4e175e85be 100644 --- a/src/java/org/apache/poi/hssf/record/formula/Ref3DPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/Ref3DPtg.java @@ -61,6 +61,7 @@ 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.hssf.model.Workbook; import org.apache.poi.util.BitField; import org.apache.poi.hssf.model.Workbook; @@ -193,8 +194,9 @@ public class Ref3DPtg extends Ptg { } - public String toFormulaString(SheetReferences refs) { + public String toFormulaString(Workbook book) { StringBuffer retval = new StringBuffer(); + SheetReferences refs = book == null ? null : book.getSheetReferences(); if (refs != null) { retval.append(refs.getSheetName((int)this.field_1_index_extern_sheet)); retval.append('!'); diff --git a/src/java/org/apache/poi/hssf/record/formula/ReferencePtg.java b/src/java/org/apache/poi/hssf/record/formula/ReferencePtg.java index 2ec380a4ad..4c14aaf55f 100644 --- a/src/java/org/apache/poi/hssf/record/formula/ReferencePtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/ReferencePtg.java @@ -64,7 +64,7 @@ import org.apache.poi.util.LittleEndian; import org.apache.poi.util.BitField; import org.apache.poi.hssf.util.CellReference; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; /** * ReferencePtg - handles references (such as A1, A2, IA4) @@ -179,7 +179,7 @@ public class ReferencePtg extends Ptg return SIZE; } - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { //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(); diff --git a/src/java/org/apache/poi/hssf/record/formula/StringPtg.java b/src/java/org/apache/poi/hssf/record/formula/StringPtg.java index a66861914e..1ad2789d47 100644 --- a/src/java/org/apache/poi/hssf/record/formula/StringPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/StringPtg.java @@ -56,7 +56,7 @@ 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.SheetReferences; +import org.apache.poi.hssf.model.Workbook; import org.apache.poi.util.StringUtil; /** @@ -145,7 +145,7 @@ public class StringPtg } } - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { return "\""+getValue()+"\""; } @@ -162,4 +162,3 @@ public class StringPtg } } - diff --git a/src/java/org/apache/poi/hssf/record/formula/SubtractPtg.java b/src/java/org/apache/poi/hssf/record/formula/SubtractPtg.java index 2c5588f3dd..cd9fa5fd78 100644 --- a/src/java/org/apache/poi/hssf/record/formula/SubtractPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/SubtractPtg.java @@ -61,7 +61,7 @@ package org.apache.poi.hssf.record.formula; import java.util.List; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; /** * @@ -105,7 +105,7 @@ public class SubtractPtg return 2; } - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { return "-"; } diff --git a/src/java/org/apache/poi/hssf/record/formula/UnaryMinusPtg.java b/src/java/org/apache/poi/hssf/record/formula/UnaryMinusPtg.java index 6cbe9b64d9..bb60dd09bc 100644 --- a/src/java/org/apache/poi/hssf/record/formula/UnaryMinusPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/UnaryMinusPtg.java @@ -56,7 +56,7 @@ package org.apache.poi.hssf.record.formula; import java.util.List; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; /** * Unary Plus operator @@ -105,7 +105,7 @@ public class UnaryMinusPtg extends OperationPtg } /** Implementation of method from Ptg */ - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { return "+"; } diff --git a/src/java/org/apache/poi/hssf/record/formula/UnaryPlusPtg.java b/src/java/org/apache/poi/hssf/record/formula/UnaryPlusPtg.java index cf2a5e8b99..c7ec1e0e99 100644 --- a/src/java/org/apache/poi/hssf/record/formula/UnaryPlusPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/UnaryPlusPtg.java @@ -56,7 +56,7 @@ package org.apache.poi.hssf.record.formula; import java.util.List; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; /** * Unary Plus operator @@ -105,7 +105,7 @@ public class UnaryPlusPtg extends OperationPtg } /** Implementation of method from Ptg */ - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { return "+"; } diff --git a/src/java/org/apache/poi/hssf/record/formula/UnionPtg.java b/src/java/org/apache/poi/hssf/record/formula/UnionPtg.java index f84235055e..dea5997e05 100644 --- a/src/java/org/apache/poi/hssf/record/formula/UnionPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/UnionPtg.java @@ -54,7 +54,7 @@ package org.apache.poi.hssf.record.formula; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; /** * @author Glen Stampoultzis (glens at apache.org) @@ -95,7 +95,7 @@ public class UnionPtg extends OperationPtg } /** Implementation of method from Ptg */ - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { return ","; } diff --git a/src/java/org/apache/poi/hssf/record/formula/UnknownPtg.java b/src/java/org/apache/poi/hssf/record/formula/UnknownPtg.java index 2dd064973c..56d03f60c1 100644 --- a/src/java/org/apache/poi/hssf/record/formula/UnknownPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/UnknownPtg.java @@ -60,7 +60,7 @@ */ package org.apache.poi.hssf.record.formula; -import org.apache.poi.hssf.util.SheetReferences; +import org.apache.poi.hssf.model.Workbook; /** * @@ -94,7 +94,7 @@ public class UnknownPtg return size; } - public String toFormulaString(SheetReferences refs) + public String toFormulaString(Workbook book) { return "UNKNOWN"; } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java index 5986b84635..c9a31854dc 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java @@ -726,8 +726,7 @@ public class HSSFCell public String getCellFormula() { //Workbook.currentBook=book; - SheetReferences refs = book.getSheetReferences(); - String retval = FormulaParser.toFormulaString(refs, ((FormulaRecordAggregate)record).getFormulaRecord().getParsedExpression()); + String retval = FormulaParser.toFormulaString(book, ((FormulaRecordAggregate)record).getFormulaRecord().getParsedExpression()); //Workbook.currentBook=null; return retval; } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFName.java b/src/java/org/apache/poi/hssf/usermodel/HSSFName.java index 0f7178225e..8c18634025 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFName.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFName.java @@ -123,8 +123,7 @@ public class HSSFName { public String getReference() { String result; - SheetReferences refs = book.getSheetReferences(); - result = name.getAreaReference(refs); + result = name.getAreaReference(book); return result; } @@ -167,4 +166,3 @@ public class HSSFName { } } - diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index 69f2c99e95..32f4c2c412 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -898,7 +898,7 @@ public class HSSFWorkbook if (name == null) return null; //adding one here because 0 indicates a global named region; doesnt make sense for print areas - return name.getAreaReference(workbook.getSheetReferences()); + return name.getAreaReference(workbook); } /** diff --git a/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java b/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java index c4722a9a73..dadab580c8 100644 --- a/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java +++ b/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java @@ -56,7 +56,6 @@ package org.apache.poi.hssf.model; import junit.framework.TestCase; import org.apache.poi.hssf.record.formula.*; -import org.apache.poi.hssf.util.SheetReferences; /** * Test the low level formula parser functionality. High level tests are to @@ -145,7 +144,7 @@ public class TestFormulaParser extends TestCase { assertEquals(true, flag.getValue()); assertEquals("Y", y.getValue()); assertEquals("N", n.getValue()); - assertEquals("IF", funif.toFormulaString(new SheetReferences())); + assertEquals("IF", funif.toFormulaString((Workbook) null)); assertTrue("Goto ptg exists", goto1.isGoto()); } @@ -285,6 +284,19 @@ public class TestFormulaParser extends TestCase { } + public void testMacroFunction() { + Workbook w = new Workbook(); + FormulaParser fp = new FormulaParser("FOO()", w); + fp.parse(); + Ptg[] ptg = fp.getRPNPtg(); + + AbstractFunctionPtg tfunc = (AbstractFunctionPtg) ptg[0]; + assertEquals("externalflag", tfunc.getName()); + + NamePtg tname = (NamePtg) ptg[1]; + assertEquals("FOO", tname.toFormulaString(w)); + } + public static void main(String [] args) { System.out.println("Testing org.apache.poi.hssf.record.formula.FormulaParser"); junit.textui.TestRunner.run(TestFormulaParser.class); diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java b/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java index 450c7325d9..0baf1a3b67 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java @@ -744,7 +744,7 @@ extends TestCase { } public void testSheetFunctions() - throws java.io.IOException + throws IOException { String filename = System.getProperty("HSSF.testdata.path"); @@ -818,7 +818,7 @@ extends TestCase { } public void testStringFormulas() - throws java.io.IOException + throws IOException { String readFilename = System.getProperty("HSSF.testdata.path"); @@ -852,7 +852,7 @@ extends TestCase { public void testLogicalFormulas() - throws java.io.IOException + throws IOException { File file = File.createTempFile("testLogicalFormula",".xls"); @@ -880,7 +880,7 @@ extends TestCase { } public void testDateFormulas() - throws java.io.IOException + throws IOException { String readFilename = System.getProperty("HSSF.testdata.path"); @@ -918,7 +918,7 @@ extends TestCase { public void testIfFormulas() - throws java.io.IOException + throws IOException { String readFilename = System.getProperty("HSSF.testdata.path"); @@ -1008,7 +1008,7 @@ extends TestCase { } public void testSumIf() - throws java.io.IOException + throws IOException { String readFilename = System.getProperty("HSSF.testdata.path"); String function ="SUMIF(A1:A5,\">4000\",B1:B5)"; @@ -1060,6 +1060,52 @@ extends TestCase { assertTrue("sumif == 0 bytes", file.length() > 0); } + public void testSquareMacro() throws IOException { + File dir = new File(System.getProperty("HSSF.testdata.path")); + File xls = new File(dir, "SquareMacro.xls"); + FileInputStream in = new FileInputStream(xls); + HSSFWorkbook w; + try { + w = new HSSFWorkbook(in); + } finally { + in.close(); + } + HSSFSheet s0 = w.getSheetAt(0); + HSSFRow[] r = {s0.getRow(0), s0.getRow(1)}; + + HSSFCell a1 = r[0].getCell((short) 0); + assertEquals("square(1)", a1.getCellFormula()); + assertEquals(1d, a1.getNumericCellValue(), 1e-9); + + HSSFCell a2 = r[1].getCell((short) 0); + assertEquals("square(2)", a2.getCellFormula()); + assertEquals(4d, a2.getNumericCellValue(), 1e-9); + + HSSFCell b1 = r[0].getCell((short) 1); + assertEquals("IF(TRUE,square(1))", b1.getCellFormula()); + assertEquals(1d, b1.getNumericCellValue(), 1e-9); + + HSSFCell b2 = r[1].getCell((short) 1); + assertEquals("IF(TRUE,square(2))", b2.getCellFormula()); + assertEquals(4d, b2.getNumericCellValue(), 1e-9); + + HSSFCell c1 = r[0].getCell((short) 2); + assertEquals("square(square(1))", c1.getCellFormula()); + assertEquals(1d, c1.getNumericCellValue(), 1e-9); + + HSSFCell c2 = r[1].getCell((short) 2); + assertEquals("square(square(2))", c2.getCellFormula()); + assertEquals(16d, c2.getNumericCellValue(), 1e-9); + + HSSFCell d1 = r[0].getCell((short) 3); + assertEquals("square(one())", d1.getCellFormula()); + assertEquals(1d, d1.getNumericCellValue(), 1e-9); + + HSSFCell d2 = r[1].getCell((short) 3); + assertEquals("square(two())", d2.getCellFormula()); + assertEquals(4d, d2.getNumericCellValue(), 1e-9); + } + public static void main(String [] args) { System.out .println("Testing org.apache.poi.hssf.usermodel.TestFormulas"); -- 2.39.5