From e785cbdd46396e38d4d150e030a7399a997bab90 Mon Sep 17 00:00:00 2001 From: Avik Sengupta Date: Sun, 31 Aug 2003 06:16:57 +0000 Subject: [PATCH] Syncing to REL_2_BR: Macro functions and related fixes, by Paul Krause bug id:21444 git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353326 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 +- .../org/apache/poi/hssf/data/SquareMacro.xls | Bin 0 -> 25600 bytes .../poi/hssf/model/TestFormulaParser.java | 16 ++- .../poi/hssf/usermodel/TestFormulas.java | 58 +++++++++- 43 files changed, 301 insertions(+), 168 deletions(-) create mode 100644 src/testcases/org/apache/poi/hssf/data/SquareMacro.xls diff --git a/src/java/org/apache/poi/hssf/dev/FormulaViewer.java b/src/java/org/apache/poi/hssf/dev/FormulaViewer.java index 1e7fd9596..3443fda1a 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 da08a6dc3..cd2660219 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 ead884067..3a519dceb 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 e8a6317bd..89401a981 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 4f994151d..3189a2c85 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 d389094d7..ebbfcba87 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 954eb0f80..84ad7cfca 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 bed92e461..e7249bfbc 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 124f25ede..6bb0d9420 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 1ddff9ac6..12438a339 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 b100a1a76..d70e57968 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 2763fe0b3..b06eaffbd 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 5f7d6450e..04fbcac2e 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 200f5dc65..e06f1ecd2 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 8c1f14bc7..98283745c 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 cab99be9c..a856b93bd 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 c48ad8c3b..31a1bcafe 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 2d422b1c7..a8b63609c 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 3f88ded3b..c65ab41f3 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 143702ce1..6b33e619b 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 3786cdd69..6cdee6af0 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 58fa09686..9b6225d80 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 6965c98cd..7392adf81 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 e071420bc..b43c5536e 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 c2118f151..2abff9e73 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 ca7fa6486..161918abe 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 70b060613..cdf584ed1 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 13ae2ef23..4e175e85b 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 2ec380a4a..4c14aaf55 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 5bccc034f..bda122ed3 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 2c5588f3d..cd9fa5fd7 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 6cbe9b64d..bb60dd09b 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 cf2a5e8b9..c7ec1e0e9 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 f84235055..dea5997e0 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 2dd064973..56d03f60c 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 c989753a6..394c60c7d 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java @@ -748,8 +748,7 @@ public class HSSFCell public String getCellFormula() { //Workbook.currentBook=book; - SheetReferences refs = book.getSheetReferences(); - String retval = FormulaParser.toFormulaString(refs, + String retval = FormulaParser.toFormulaString(book, ((FormulaRecordAggregate)sheet.getValueRecord(row,cellNum)).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 0f7178225..8c1863402 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 70e08ced2..f26df3d9e 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -891,7 +891,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/data/SquareMacro.xls b/src/testcases/org/apache/poi/hssf/data/SquareMacro.xls new file mode 100644 index 0000000000000000000000000000000000000000..54beeeb37ebd32597f52db4463f06b6a3c1cb093 GIT binary patch literal 25600 zcmeHP3t(JDxt?=&lWx+a-KOa?ZCR2+)3izVk)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NS9z)dGa{d&34)kQ;D+AQwz_rM>iUa39f8)C zme!SUw5$lMZf%DHS{YG|N3fbJ5lhR80}+MGeI=nmSa0l-Q&dJ`n`I}f9F%1$PgY!TVhFuZ>73LJyO}@RPmw49#~BR;uw)S) zi?+?O+Z2oSWaiEL-Yoyi=|#?Oyo{@ujXzt>Dxt2Fah0^RD^)=Yq-sjGQIbHa!KPT9 zd52|pCJCg|ko%e->L))>zs8b$6!^K6HY6?er%^~t{bd%?Qh!o~w1P%eStCl4CgduI zlU8Q56n+(sr|yc_k7*}%WK8SXG_s|B_DsIiThFAWel88EWbc@gMn3lT*jAB|Q@@QO zE%o~;(o*k0la_iNu|4n%N)rz+z=w@Ry{Har&H|D18B zFwRB-gPS(vkfggm030C^b-R^pKpw{DYyQ}KA9}g|( z_*g8rOj*wF_@S8)5=?B8I`?8%>_3p7s~RIM)ti>;OQoi({JP4@#V*2ITea9#)3^1pc5NRH4#<70XIIZ7_aO>CuTAtM6WS)^Fzx~>k_&sw! z!Xx&p!I1e**LWB^|Z+ literal 0 HcmV?d00001 diff --git a/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java b/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java index c4722a9a7..dadab580c 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 450c7325d..0baf1a3b6 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");