View | Details | Raw Unified | Return to bug 21444
Collapse All | Expand All

(-)jakarta-poi/src/java/org/apache/poi/hssf/dev/FormulaViewer.java (-4 / +3 lines)
Lines 77-83 Link Here
77
import org.apache.poi.hssf.record.formula.*;
77
import org.apache.poi.hssf.record.formula.*;
78
import org.apache.poi.hssf.model.*;
78
import org.apache.poi.hssf.model.*;
79
import org.apache.poi.hssf.usermodel.*;
79
import org.apache.poi.hssf.usermodel.*;
80
import org.apache.poi.hssf.util.SheetReferences;
81
80
82
/**
81
/**
83
 * FormulaViewer - finds formulas in a BIFF8 file and attempts to read them/display
82
 * FormulaViewer - finds formulas in a BIFF8 file and attempts to read them/display
Lines 144-150 Link Here
144
            StringBuffer buf = new StringBuffer();
143
            StringBuffer buf = new StringBuffer();
145
            
144
            
146
            if (token instanceof ExpPtg) return;
145
            if (token instanceof ExpPtg) return;
147
            buf.append(name=((OperationPtg) token).toFormulaString((SheetReferences)null));
146
            buf.append(name=((OperationPtg) token).toFormulaString((Workbook)null));
148
            buf.append(sep);
147
            buf.append(sep);
149
            switch (token.getPtgClass()) {
148
            switch (token.getPtgClass()) {
150
                case Ptg.CLASS_REF :
149
                case Ptg.CLASS_REF :
Lines 213-219 Link Here
213
        StringBuffer buf = new StringBuffer();
212
        StringBuffer buf = new StringBuffer();
214
           for (int i=0;i<numptgs;i++) {
213
           for (int i=0;i<numptgs;i++) {
215
           token = (Ptg) tokens.get(i);
214
           token = (Ptg) tokens.get(i);
216
            buf.append( token.toFormulaString((SheetReferences)null));
215
            buf.append( token.toFormulaString((Workbook)null));
217
            switch (token.getPtgClass()) {
216
            switch (token.getPtgClass()) {
218
                case Ptg.CLASS_REF :
217
                case Ptg.CLASS_REF :
219
                    buf.append("(R)");
218
                    buf.append("(R)");
Lines 233-239 Link Here
233
    
232
    
234
    private String composeFormula(FormulaRecord record)
233
    private String composeFormula(FormulaRecord record)
235
    {
234
    {
236
       return  org.apache.poi.hssf.model.FormulaParser.toFormulaString((SheetReferences)null,record.getParsedExpression());
235
       return  org.apache.poi.hssf.model.FormulaParser.toFormulaString((Workbook)null,record.getParsedExpression());
237
    }
236
    }
238
237
239
    /**
238
    /**
(-)jakarta-poi/src/java/org/apache/poi/hssf/model/FormulaParser.java (-45 / +62 lines)
Lines 64-70 Link Here
64
//import PTG's .. since we need everything, import *
64
//import PTG's .. since we need everything, import *
65
import org.apache.poi.hssf.record.formula.*;
65
import org.apache.poi.hssf.record.formula.*;
66
66
67
import org.apache.poi.hssf.util.SheetReferences;
68
67
69
68
70
/**
69
/**
Lines 335-344 Link Here
335
        int numArgs = Arguments();
334
        int numArgs = Arguments();
336
        Match(')');
335
        Match(')');
337
                
336
                
338
        Ptg functionPtg = getFunction(name,(byte)numArgs);
337
        AbstractFunctionPtg functionPtg = getFunction(name,(byte)numArgs);
339
        
338
        
340
		tokens.add(functionPtg);
339
		tokens.add(functionPtg);
341
 
340
 
341
        if (functionPtg.getName().equals("externalflag")) {
342
            tokens.add(new NamePtg(name, this.book));
343
        }
344
342
 		//remove what we just put in
345
 		//remove what we just put in
343
		this.functionTokens.remove(0);
346
		this.functionTokens.remove(0);
344
    }
347
    }
Lines 382-389 Link Here
382
     * @param numArgs
385
     * @param numArgs
383
     * @return Ptg a null is returned if we're in an IF formula, it needs extreme manipulation and is handled in this function
386
     * @return Ptg a null is returned if we're in an IF formula, it needs extreme manipulation and is handled in this function
384
     */
387
     */
385
    private Ptg getFunction(String name,byte numArgs) {
388
    private AbstractFunctionPtg getFunction(String name, byte numArgs) {
386
        Ptg retval = null;
389
        AbstractFunctionPtg retval = null;
387
        
390
        
388
        if (name.equals("IF")) {
391
        if (name.equals("IF")) {
389
            retval = new FuncVarPtg(AbstractFunctionPtg.ATTR_NAME, numArgs);
392
            retval = new FuncVarPtg(AbstractFunctionPtg.ATTR_NAME, numArgs);
Lines 795-863 Link Here
795
    /**
798
    /**
796
     * Convience method which takes in a list then passes it to the other toFormulaString
799
     * Convience method which takes in a list then passes it to the other toFormulaString
797
     * signature. 
800
     * signature. 
798
     * @param lptgs - list of ptgs, can be null
801
     * @param book   workbook for 3D and named references
802
     * @param lptgs  list of Ptg, can be null or empty
803
     * @return a human readable String
799
     */
804
     */
800
    public static String toFormulaString(SheetReferences refs, List lptgs) {
805
    public static String toFormulaString(Workbook book, List lptgs) {
801
        String retval = null;
806
        String retval = null;
802
        if (lptgs == null || lptgs.size() == 0) return "#NAME";
807
        if (lptgs == null || lptgs.size() == 0) return "#NAME";
803
        Ptg[] ptgs = new Ptg[lptgs.size()];
808
        Ptg[] ptgs = new Ptg[lptgs.size()];
804
        ptgs = (Ptg[])lptgs.toArray(ptgs);
809
        ptgs = (Ptg[])lptgs.toArray(ptgs);
805
        retval = toFormulaString(refs, ptgs);
810
        retval = toFormulaString(book, ptgs);
806
        return retval;
811
        return retval;
807
    }
812
    }
808
    
813
    
809
    /** Static method to convert an array of Ptgs in RPN order 
814
    /**
810
     *  to a human readable string format in infix mode
815
     * Static method to convert an array of Ptgs in RPN order
811
     *  @param ptgs - array of ptgs, can be null or empty
816
     * to a human readable string format in infix mode.
817
     * @param book  workbook for named and 3D references
818
     * @param ptgs  array of Ptg, can be null or empty
819
     * @return a human readable String
812
     */
820
     */
813
    public static String toFormulaString(SheetReferences refs, Ptg[] ptgs) {
821
    public static String toFormulaString(Workbook book, Ptg[] ptgs) {
814
        if (ptgs == null || ptgs.length == 0) return "#NAME";
822
        if (ptgs == null || ptgs.length == 0) return "#NAME";
815
        java.util.Stack stack = new java.util.Stack();
823
        java.util.Stack stack = new java.util.Stack();
816
        int numPtgs = ptgs.length;
817
        OperationPtg o;
818
        int numOperands;
819
        String result=null;
820
        String[] operands;
821
        AttrPtg ifptg = null;
824
        AttrPtg ifptg = null;
822
        for (int i=0;i<numPtgs;i++) {
825
823
           // Excel allows to have AttrPtg at position 0 (such as Blanks) which
826
           // Excel allows to have AttrPtg at position 0 (such as Blanks) which
824
           // do not have any operands. Skip them.
827
           // do not have any operands. Skip them.
825
            if (ptgs[i] instanceof OperationPtg && i>0) {
828
        stack.push(ptgs[0].toFormulaString(book));
826
                  o = (OperationPtg) ptgs[i];
827
                  
829
                  
828
                  if (o instanceof AttrPtg && ((AttrPtg)o).isOptimizedIf()) {
830
        for (int i = 1; i < ptgs.length; i++) {
829
                        ifptg=(AttrPtg)o;
831
            if (! (ptgs[i] instanceof OperationPtg)) {
830
                  } else {
832
                stack.push(ptgs[i].toFormulaString(book));
833
                continue;
834
            }
831
                      
835
                      
832
                      numOperands = o.getNumberOfOperands();
836
            if (ptgs[i] instanceof AttrPtg && ((AttrPtg) ptgs[i]).isOptimizedIf()) {
833
                      operands = new String[numOperands];
837
                ifptg = (AttrPtg) ptgs[i];
838
                continue;
839
            }
834
                      
840
                      
835
                      for (int j=0;j<numOperands;j++) {
841
            final OperationPtg o = (OperationPtg) ptgs[i];
836
                          operands[numOperands-j-1] = (String) stack.pop(); //TODO: catch stack underflow and throw parse exception. 
842
            final String[] operands = new String[o.getNumberOfOperands()];
843
844
            for (int j = operands.length; j > 0; j--) {
845
                //TODO: catch stack underflow and throw parse exception.
846
                operands[j - 1] = (String) stack.pop();
837
                      }  
847
                      }  
838
848
839
                      if ( (o instanceof AbstractFunctionPtg) && 
849
            stack.push(o.toFormulaString(operands));
840
                            ((AbstractFunctionPtg)o).getName().equals("specialflag") &&
850
            if (!(o instanceof AbstractFunctionPtg)) continue;
841
                            ifptg != null
851
842
                            ) {
852
            final AbstractFunctionPtg f = (AbstractFunctionPtg) o;
853
            final String fname = f.getName();
854
            if (fname == null) continue;
855
856
            if ((ifptg != null) && (fname.equals("specialflag"))) {
843
                             // this special case will be way different.
857
                             // this special case will be way different.
844
                             result = ifptg.toFormulaString(
858
                stack.push(ifptg.toFormulaString(new String[]{(String) stack.pop()}));
845
                                  new String[] {(o.toFormulaString(operands))}
859
                continue;
846
                                                           );
847
                             ifptg = null;
848
                      } else {                      
849
                        result = o.toFormulaString(operands);                                              
850
                      }
860
                      }
851
                      stack.push(result);                                        
861
            if (fname.equals("externalflag")) {
862
                final String top = (String) stack.pop();
863
                final int paren = top.indexOf('(');
864
                final int comma = top.indexOf(',');
865
                if (comma == -1) {
866
                    final int rparen = top.indexOf(')');
867
                    stack.push(top.substring(paren + 1, rparen) + "()");
852
                  }
868
                  }
853
                      
869
                else {
854
                  
870
                    stack.push(top.substring(paren + 1, comma) + '(' +
855
            } else {
871
                               top.substring(comma + 1));
856
                stack.push(ptgs[i].toFormulaString(refs));
857
            }
872
            }
858
        }
873
        }
859
        return (String) stack.pop(); //TODO: catch stack underflow and throw parse exception. 
860
    }
874
    }
875
        // TODO: catch stack underflow and throw parse exception.
876
        return (String) stack.pop();
877
    }
878
879
861
    /** Create a tree representation of the RPN token array
880
    /** Create a tree representation of the RPN token array
862
     *used to run the class(RVA) change algo
881
     *used to run the class(RVA) change algo
863
     */
882
     */
Lines 890-900 Link Here
890
     *   Useful for testing
909
     *   Useful for testing
891
     */
910
     */
892
    public String toString() {
911
    public String toString() {
893
        SheetReferences refs = null;
894
        if (book!=null)  book.getSheetReferences();
895
        StringBuffer buf = new StringBuffer();
912
        StringBuffer buf = new StringBuffer();
896
           for (int i=0;i<tokens.size();i++) {
913
           for (int i=0;i<tokens.size();i++) {
897
            buf.append( ( (Ptg)tokens.get(i)).toFormulaString(refs));
914
            buf.append( ( (Ptg)tokens.get(i)).toFormulaString(book));
898
            buf.append(' ');
915
            buf.append(' ');
899
        } 
916
        } 
900
        return buf.toString();
917
        return buf.toString();
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/NameRecord.java (-9 / +62 lines)
Lines 58-68 Link Here
58
import java.util.List;
58
import java.util.List;
59
import java.util.Stack;
59
import java.util.Stack;
60
60
61
import org.apache.poi.hssf.model.Workbook;
61
import org.apache.poi.hssf.record.formula.Area3DPtg;
62
import org.apache.poi.hssf.record.formula.Area3DPtg;
62
import org.apache.poi.hssf.record.formula.Ptg;
63
import org.apache.poi.hssf.record.formula.Ptg;
63
import org.apache.poi.hssf.record.formula.Ref3DPtg;
64
import org.apache.poi.hssf.record.formula.Ref3DPtg;
64
import org.apache.poi.hssf.util.RangeAddress;
65
import org.apache.poi.hssf.util.RangeAddress;
65
import org.apache.poi.hssf.util.SheetReferences;
66
import org.apache.poi.util.HexDump;
66
import org.apache.poi.util.HexDump;
67
import org.apache.poi.util.LittleEndian;
67
import org.apache.poi.util.LittleEndian;
68
import org.apache.poi.util.StringUtil;
68
import org.apache.poi.util.StringUtil;
Lines 127-132 Link Here
127
	 */
127
	 */
128
	public final static byte  BUILTIN_SHEET_TITLE           = (byte)12;
128
	public final static byte  BUILTIN_SHEET_TITLE           = (byte)12;
129
    
129
    
130
    public static final short OPT_HIDDEN_NAME =   (short) 0x0001;
131
    public static final short OPT_FUNCTION_NAME = (short) 0x0002;
132
    public static final short OPT_COMMAND_NAME =  (short) 0x0004;
133
    public static final short OPT_MACRO =         (short) 0x0008;
134
    public static final short OPT_COMPLEX =       (short) 0x0010;
135
    public static final short OPT_BUILTIN =       (short) 0x0020;
136
    public static final short OPT_BINDATA =       (short) 0x1000;
137
130
    
138
    
131
    private short             field_1_option_flag;
139
    private short             field_1_option_flag;
132
    private byte              field_2_keyboard_shortcut;
140
    private byte              field_2_keyboard_shortcut;
Lines 192-198 Link Here
192
	{
200
	{
193
	    this();	    
201
	    this();	    
194
	    this.field_12_builtIn_name = builtin;
202
	    this.field_12_builtIn_name = builtin;
195
	    this.setOptionFlag((short)(this.getOptionFlag() | (short)0x20));
203
	    this.setOptionFlag((short)(this.getOptionFlag() | OPT_BUILTIN));
196
	    this.setNameTextLength((byte)1);
204
	    this.setNameTextLength((byte)1);
197
	    this.setEqualsToIndexToSheet(index); //the extern sheets are set through references
205
	    this.setEqualsToIndexToSheet(index); //the extern sheets are set through references
198
	    
206
	    
Lines 252-264 Link Here
252
260
253
	/**
261
	/**
254
	 * Convenience method to retrieve the index the name refers to.
262
	 * Convenience method to retrieve the index the name refers to.
255
	 * @see getEqualsToIndexToSheet()
263
	 * @see #getEqualsToIndexToSheet()
256
	 * @return short
264
	 * @return short
257
	 */
265
	 */
258
	public short getIndexToSheet() {
266
	public short getIndexToSheet() {
259
		return getEqualsToIndexToSheet();
267
		return getEqualsToIndexToSheet();
260
	}
268
	}
261
269
270
    /**
271
     * @return function group
272
     * @see FnGroupCountRecord
273
     */
274
    public byte getFnGroup() {
275
        int masked = field_1_option_flag & 0x0fc0;
276
        return (byte) (masked >> 4);
277
    }
278
262
    public void setEqualsToIndexToSheet(short value)
279
    public void setEqualsToIndexToSheet(short value)
263
    {
280
    {
264
        field_6_equals_to_index_to_sheet = value;
281
        field_6_equals_to_index_to_sheet = value;
Lines 409-419 Link Here
409
        return field_11_compressed_unicode_flag;
426
        return field_11_compressed_unicode_flag;
410
    }
427
    }
411
428
429
    /**
430
     * @return true if name is hidden
431
     */
432
    public boolean isHiddenName() {
433
        return (field_1_option_flag & OPT_HIDDEN_NAME) != 0;
434
    }
435
436
    /**
437
     * @return true if name is a function
438
     */
439
    public boolean isFunctionName() {
440
        return (field_1_option_flag & OPT_FUNCTION_NAME) != 0;
441
    }
442
443
    /**
444
     * @return true if name is a command
445
     */
446
    public boolean isCommandName() {
447
        return (field_1_option_flag & OPT_COMMAND_NAME) != 0;
448
    }
449
450
    /**
451
     * @return true if function macro or command macro
452
     */
453
    public boolean isMacro() {
454
        return (field_1_option_flag & OPT_MACRO) != 0;
455
    }
456
457
    /**
458
     * @return true if array formula or user defined
459
     */
460
    public boolean isComplexFunction() {
461
        return (field_1_option_flag & OPT_COMPLEX) != 0;
462
    }
463
464
412
	/**Convenience Function to determine if the name is a built-in name
465
	/**Convenience Function to determine if the name is a built-in name
413
	 */
466
	 */
414
	public boolean isBuiltInName()
467
	public boolean isBuiltInName()
415
	{
468
	{
416
	    return ((this.getOptionFlag() & (short)0x20) != 0);
469
	    return ((this.getOptionFlag() & OPT_BUILTIN) != 0);
417
	}
470
	}
418
471
419
472
Lines 511-517 Link Here
511
        data[18 + offset] = getCompressedUnicodeFlag();
564
        data[18 + offset] = getCompressedUnicodeFlag();
512
565
513
        /* temp: gjs
566
        /* temp: gjs
514
        if ( ( field_1_option_flag & (short) 0x20 ) != 0 )
567
        if (isBuiltInName())
515
        {
568
        {
516
            LittleEndian.putShort( data, 2 + offset, (short) ( 16 + field_13_raw_name_definition.length ) );
569
            LittleEndian.putShort( data, 2 + offset, (short) ( 16 + field_13_raw_name_definition.length ) );
517
570
Lines 647-662 Link Here
647
    /** gets the reference , the area only (range)
700
    /** gets the reference , the area only (range)
648
     * @return area reference
701
     * @return area reference
649
     */
702
     */
650
    public String getAreaReference(SheetReferences refs){
703
    public String getAreaReference(Workbook book){
651
        if (field_13_name_definition == null) return "#REF!";
704
        if (field_13_name_definition == null) return "#REF!";
652
        Ptg ptg = (Ptg) field_13_name_definition.peek();
705
        Ptg ptg = (Ptg) field_13_name_definition.peek();
653
        String result = "";
706
        String result = "";
654
707
655
        if (ptg.getClass() == Area3DPtg.class){
708
        if (ptg.getClass() == Area3DPtg.class){
656
            result = ptg.toFormulaString(refs);
709
            result = ptg.toFormulaString(book);
657
710
658
        } else if (ptg.getClass() == Ref3DPtg.class){
711
        } else if (ptg.getClass() == Ref3DPtg.class){
659
            result = ptg.toFormulaString(refs);
712
            result = ptg.toFormulaString(book);
660
        }
713
        }
661
714
662
        return result;
715
        return result;
Lines 727-733 Link Here
727
780
728
        /*
781
        /*
729
        temp: gjs
782
        temp: gjs
730
        if ( ( field_1_option_flag & (short)0x20 ) != 0 ) {
783
        if (isBuiltInName()) {
731
            // DEBUG
784
            // DEBUG
732
            // System.out.println( "Built-in name" );
785
            // System.out.println( "Built-in name" );
733
786
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/AbstractFunctionPtg.java (-5 / +7 lines)
Lines 54-62 Link Here
54
package org.apache.poi.hssf.record.formula;
54
package org.apache.poi.hssf.record.formula;
55
55
56
import org.apache.poi.util.BinaryTree;
56
import org.apache.poi.util.BinaryTree;
57
import org.apache.poi.hssf.util.SheetReferences;
57
import org.apache.poi.hssf.model.Workbook;
58
58
59
import java.util.Stack;
60
59
61
/**
60
/**
62
 * This class provides the base functionality for Excel sheet functions 
61
 * This class provides the base functionality for Excel sheet functions 
Lines 69-75 Link Here
69
	//constant used allow a ptgAttr to be mapped properly for its functionPtg
68
	//constant used allow a ptgAttr to be mapped properly for its functionPtg
70
	public static final String ATTR_NAME = "specialflag";
69
	public static final String ATTR_NAME = "specialflag";
71
	    
70
	    
72
      
71
    public static final short INDEX_EXTERNAL = 255;
73
    
72
    
74
    private static BinaryTree map = produceHash(); 
73
    private static BinaryTree map = produceHash(); 
75
    protected static Object[][] functionData = produceFunctionData();
74
    protected static Object[][] functionData = produceFunctionData();
Lines 104-110 Link Here
104
        return lookupName(field_2_fnc_index);
103
        return lookupName(field_2_fnc_index);
105
    }
104
    }
106
    
105
    
107
    public String toFormulaString(SheetReferences refs) {
106
    public String toFormulaString(Workbook book) {
108
        return getName();
107
        return getName();
109
    }
108
    }
110
    
109
    
Lines 140-146 Link Here
140
    }
139
    }
141
    
140
    
142
    protected short lookupIndex(String name) {
141
    protected short lookupIndex(String name) {
143
        return (short)((Integer)map.getKeyForValue(name)).intValue();
142
        Integer index = (Integer) map.getKeyForValue(name);
143
        if (index != null) return index.shortValue();
144
        return INDEX_EXTERNAL;
144
    }
145
    }
145
    
146
    
146
    /**
147
    /**
Lines 389-394 Link Here
389
        dmap.put(new Integer(252),"FREQUENCY");
390
        dmap.put(new Integer(252),"FREQUENCY");
390
        dmap.put(new Integer(253),"ADDTOOLBAR");
391
        dmap.put(new Integer(253),"ADDTOOLBAR");
391
        dmap.put(new Integer(254),"DELETETOOLBAR");
392
        dmap.put(new Integer(254),"DELETETOOLBAR");
393
        dmap.put(new Integer(255),"externalflag");
392
        dmap.put(new Integer(256),"RESETTOOLBAR");
394
        dmap.put(new Integer(256),"RESETTOOLBAR");
393
        dmap.put(new Integer(257),"EVALUATE");
395
        dmap.put(new Integer(257),"EVALUATE");
394
        dmap.put(new Integer(258),"GETTOOLBAR");
396
        dmap.put(new Integer(258),"GETTOOLBAR");
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/AddPtg.java (-2 / +2 lines)
Lines 62-68 Link Here
62
62
63
import java.util.List;
63
import java.util.List;
64
64
65
import org.apache.poi.hssf.util.SheetReferences;
65
import org.apache.poi.hssf.model.Workbook;
66
66
67
/**
67
/**
68
 * Addition operator PTG the "+" binomial operator.  If you need more 
68
 * Addition operator PTG the "+" binomial operator.  If you need more 
Lines 113-119 Link Here
113
    }
113
    }
114
    
114
    
115
    /** Implementation of method from Ptg */
115
    /** Implementation of method from Ptg */
116
    public String toFormulaString(SheetReferences refs)
116
    public String toFormulaString(Workbook book)
117
    {
117
    {
118
        return "+";
118
        return "+";
119
    }
119
    }
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java (-2 / +2 lines)
Lines 291-298 Link Here
291
291
292
	}
292
	}
293
293
294
	public String toFormulaString( SheetReferences refs )
294
	public String toFormulaString(Workbook book)
295
	{
295
	{
296
		SheetReferences refs = book == null ? null : book.getSheetReferences();
296
		StringBuffer retval = new StringBuffer();
297
		StringBuffer retval = new StringBuffer();
297
		if ( refs != null )
298
		if ( refs != null )
298
		{
299
		{
Lines 352-355 Link Here
352
353
353
354
354
}
355
}
355
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/AreaPtg.java (-2 / +2 lines)
Lines 65-71 Link Here
65
65
66
import org.apache.poi.hssf.util.AreaReference;
66
import org.apache.poi.hssf.util.AreaReference;
67
import org.apache.poi.hssf.util.CellReference;
67
import org.apache.poi.hssf.util.CellReference;
68
import org.apache.poi.hssf.util.SheetReferences;
68
import org.apache.poi.hssf.model.Workbook;
69
69
70
/**
70
/**
71
 * Specifies a rectangular area of cells A1:A4 for instance.
71
 * Specifies a rectangular area of cells A1:A4 for instance.
Lines 305-311 Link Here
305
        field_4_last_column = column;
305
        field_4_last_column = column;
306
    }
306
    }
307
307
308
    public String toFormulaString(SheetReferences refs)
308
    public String toFormulaString(Workbook book)
309
    {
309
    {
310
         return (new CellReference(getFirstRow(),getFirstColumn(),!isFirstRowRelative(),!isFirstColRelative())).toString() + ":" +
310
         return (new CellReference(getFirstRow(),getFirstColumn(),!isFirstRowRelative(),!isFirstColRelative())).toString() + ":" +
311
                (new CellReference(getLastRow(),getLastColumn(),!isLastRowRelative(),!isLastColRelative())).toString();
311
                (new CellReference(getLastRow(),getLastColumn(),!isLastRowRelative(),!isLastColRelative())).toString();
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/AttrPtg.java (-5 / +5 lines)
Lines 60-66 Link Here
60
 */
60
 */
61
package org.apache.poi.hssf.record.formula;
61
package org.apache.poi.hssf.record.formula;
62
62
63
import org.apache.poi.hssf.util.SheetReferences;
63
import org.apache.poi.hssf.model.Workbook;
64
64
65
import org.apache.poi.util.LittleEndian;
65
import org.apache.poi.util.LittleEndian;
66
import org.apache.poi.util.BitField;
66
import org.apache.poi.util.BitField;
Lines 207-217 Link Here
207
        if(space.isSet(field_1_options)) {
207
        if(space.isSet(field_1_options)) {
208
            return operands[ 0 ];
208
            return operands[ 0 ];
209
        } else if (optiIf.isSet(field_1_options)) {
209
        } else if (optiIf.isSet(field_1_options)) {
210
            return toFormulaString((SheetReferences)null) + "(" + operands[ 0 ]             +")"; 
210
            return toFormulaString((Workbook)null) + "(" + operands[ 0 ]             +")";
211
        } else if (optGoto.isSet(field_1_options)) {
211
        } else if (optGoto.isSet(field_1_options)) {
212
            return toFormulaString((SheetReferences)null) + operands[0];   //goto isn't a real formula element should not show up
212
            return toFormulaString((Workbook)null) + operands[0];   //goto isn't a real formula element should not show up
213
        } else {
213
        } else {
214
            return toFormulaString((SheetReferences)null) + "(" + operands[ 0 ] + ")";
214
            return toFormulaString((Workbook)null) + "(" + operands[ 0 ] + ")";
215
        }
215
        }
216
    }
216
    }
217
  
217
  
Lines 226-232 Link Here
226
        return -1;
226
        return -1;
227
    }
227
    }
228
        
228
        
229
   public String toFormulaString(SheetReferences refs) {
229
   public String toFormulaString(Workbook book) {
230
      if(semiVolatile.isSet(field_1_options)) {
230
      if(semiVolatile.isSet(field_1_options)) {
231
        return "ATTR(semiVolatile)";
231
        return "ATTR(semiVolatile)";
232
      }
232
      }
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/BoolPtg.java (-2 / +2 lines)
Lines 60-66 Link Here
60
package org.apache.poi.hssf.record.formula;
60
package org.apache.poi.hssf.record.formula;
61
61
62
import org.apache.poi.util.LittleEndian;
62
import org.apache.poi.util.LittleEndian;
63
import org.apache.poi.hssf.util.SheetReferences;
63
import org.apache.poi.hssf.model.Workbook;
64
64
65
/**
65
/**
66
 * Boolean (boolean)
66
 * Boolean (boolean)
Lines 114-120 Link Here
114
        return SIZE;
114
        return SIZE;
115
    }
115
    }
116
116
117
    public String toFormulaString(SheetReferences refs)
117
    public String toFormulaString(Workbook book)
118
    {
118
    {
119
        return field_1_value ? "TRUE" : "FALSE";
119
        return field_1_value ? "TRUE" : "FALSE";
120
    }
120
    }
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/ConcatPtg.java (-2 / +2 lines)
Lines 62-68 Link Here
62
62
63
import java.util.List;
63
import java.util.List;
64
64
65
import org.apache.poi.hssf.util.SheetReferences;
65
import org.apache.poi.hssf.model.Workbook;
66
66
67
/**
67
/**
68
 *
68
 *
Lines 108-114 Link Here
108
        return 2;
108
        return 2;
109
    }
109
    }
110
110
111
    public String toFormulaString(SheetReferences refs)
111
    public String toFormulaString(Workbook book)
112
    {
112
    {
113
        return CONCAT;
113
        return CONCAT;
114
    }    
114
    }    
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/DividePtg.java (-3 / +3 lines)
Lines 62-68 Link Here
62
62
63
import java.util.List;
63
import java.util.List;
64
64
65
import org.apache.poi.hssf.util.SheetReferences;
65
import org.apache.poi.hssf.model.Workbook;
66
66
67
/**
67
/**
68
 * This PTG implements the standard binomial divide "/"
68
 * This PTG implements the standard binomial divide "/"
Lines 108-114 Link Here
108
        return 2;
108
        return 2;
109
    }
109
    }
110
110
111
    public String toFormulaString(SheetReferences refs)
111
    public String toFormulaString(Workbook book)
112
    {
112
    {
113
        return "/";
113
        return "/";
114
    }
114
    }
Lines 117-123 Link Here
117
        StringBuffer buffer = new StringBuffer();
117
        StringBuffer buffer = new StringBuffer();
118
118
119
        buffer.append(operands[ 0 ]);
119
        buffer.append(operands[ 0 ]);
120
        buffer.append(toFormulaString((SheetReferences)null));
120
        buffer.append(toFormulaString((Workbook)null));
121
        buffer.append(operands[ 1 ]);
121
        buffer.append(operands[ 1 ]);
122
        return buffer.toString();
122
        return buffer.toString();
123
    }      
123
    }      
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/EqualPtg.java (-3 / +3 lines)
Lines 62-68 Link Here
62
62
63
import java.util.List;
63
import java.util.List;
64
64
65
import org.apache.poi.hssf.util.SheetReferences;
65
import org.apache.poi.hssf.model.Workbook;
66
66
67
/**
67
/**
68
 *
68
 *
Lines 107-113 Link Here
107
        return 2;
107
        return 2;
108
    }
108
    }
109
109
110
    public String toFormulaString(SheetReferences refs)
110
    public String toFormulaString(Workbook book)
111
    {
111
    {
112
        return "=";
112
        return "=";
113
    }
113
    }
Lines 117-123 Link Here
117
117
118
        
118
        
119
        buffer.append(operands[ 0 ]);
119
        buffer.append(operands[ 0 ]);
120
        buffer.append(toFormulaString((SheetReferences)null));
120
        buffer.append(toFormulaString((Workbook)null));
121
        buffer.append(operands[ 1 ]);
121
        buffer.append(operands[ 1 ]);
122
        return buffer.toString();
122
        return buffer.toString();
123
    }       
123
    }       
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/ExpPtg.java (-2 / +2 lines)
Lines 60-66 Link Here
60
 */
60
 */
61
package org.apache.poi.hssf.record.formula;
61
package org.apache.poi.hssf.record.formula;
62
62
63
import org.apache.poi.hssf.util.SheetReferences;
63
import org.apache.poi.hssf.model.Workbook;
64
64
65
/**
65
/**
66
 *
66
 *
Lines 102-108 Link Here
102
        return SIZE;
102
        return SIZE;
103
    }
103
    }
104
104
105
    public String toFormulaString(SheetReferences refs)
105
    public String toFormulaString(Workbook book)
106
    {
106
    {
107
        return "NO IDEA SHARED FORMULA EXP PTG";
107
        return "NO IDEA SHARED FORMULA EXP PTG";
108
    }
108
    }
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/GreaterEqualPtg.java (-3 / +3 lines)
Lines 54-60 Link Here
54
package org.apache.poi.hssf.record.formula;
54
package org.apache.poi.hssf.record.formula;
55
55
56
56
57
import org.apache.poi.hssf.util.SheetReferences;
57
import org.apache.poi.hssf.model.Workbook;
58
58
59
/**
59
/**
60
 * PTG class to implement greater or equal to
60
 * PTG class to implement greater or equal to
Lines 98-104 Link Here
98
        return 2;
98
        return 2;
99
    }
99
    }
100
100
101
    public String toFormulaString(SheetReferences refs)
101
    public String toFormulaString(Workbook book)
102
    {
102
    {
103
        return ">=";
103
        return ">=";
104
    }
104
    }
Lines 108-114 Link Here
108
108
109
        
109
        
110
        buffer.append(operands[ 0 ]);
110
        buffer.append(operands[ 0 ]);
111
        buffer.append(toFormulaString((SheetReferences)null));
111
        buffer.append(toFormulaString((Workbook)null));
112
        buffer.append(operands[ 1 ]);
112
        buffer.append(operands[ 1 ]);
113
        return buffer.toString();
113
        return buffer.toString();
114
    }       
114
    }       
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/GreaterThanPtg.java (-4 / +2 lines)
Lines 61-67 Link Here
61
61
62
import java.util.List;
62
import java.util.List;
63
63
64
import org.apache.poi.hssf.util.SheetReferences;
64
import org.apache.poi.hssf.model.Workbook;
65
65
66
/**
66
/**
67
 * Greater than operator PTG ">"
67
 * Greater than operator PTG ">"
Lines 133-139 Link Here
133
     * Implementation of method from Ptg 
133
     * Implementation of method from Ptg 
134
     * @param refs the Sheet References
134
     * @param refs the Sheet References
135
     */
135
     */
136
    public String toFormulaString(SheetReferences refs)
136
    public String toFormulaString(Workbook book)
137
    {
137
    {
138
        return this.GREATERTHAN;
138
        return this.GREATERTHAN;
139
    }
139
    }
Lines 171-175 Link Here
171
        return new GreaterThanPtg();
171
        return new GreaterThanPtg();
172
    }
172
    }
173
}
173
}
174
175
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/IntPtg.java (-2 / +2 lines)
Lines 61-67 Link Here
61
package org.apache.poi.hssf.record.formula;
61
package org.apache.poi.hssf.record.formula;
62
62
63
import org.apache.poi.util.LittleEndian;
63
import org.apache.poi.util.LittleEndian;
64
import org.apache.poi.hssf.util.SheetReferences;
64
import org.apache.poi.hssf.model.Workbook;
65
65
66
/**
66
/**
67
 * Integer (short intger)
67
 * Integer (short intger)
Lines 116-122 Link Here
116
        return SIZE;
116
        return SIZE;
117
    }
117
    }
118
118
119
    public String toFormulaString(SheetReferences refs)
119
    public String toFormulaString(Workbook book)
120
    {
120
    {
121
        return "" + getValue();
121
        return "" + getValue();
122
    }
122
    }
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/LessEqualPtg.java (-3 / +3 lines)
Lines 53-59 Link Here
53
 */
53
 */
54
package org.apache.poi.hssf.record.formula;
54
package org.apache.poi.hssf.record.formula;
55
55
56
import org.apache.poi.hssf.util.SheetReferences;
56
import org.apache.poi.hssf.model.Workbook;
57
57
58
58
59
/**
59
/**
Lines 99-105 Link Here
99
		  return 2;
99
		  return 2;
100
	 }
100
	 }
101
101
102
	 public String toFormulaString(SheetReferences refs)
102
    public String toFormulaString(Workbook book)
103
	 {
103
	 {
104
		  return "<=";
104
		  return "<=";
105
	 }
105
	 }
Lines 109-115 Link Here
109
109
110
        
110
        
111
		  buffer.append(operands[ 0 ]);
111
		  buffer.append(operands[ 0 ]);
112
		  buffer.append(toFormulaString((SheetReferences)null));
112
		  buffer.append(toFormulaString((Workbook)null));
113
		  buffer.append(operands[ 1 ]);
113
		  buffer.append(operands[ 1 ]);
114
		  return buffer.toString();
114
		  return buffer.toString();
115
	 }       
115
	 }       
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/LessThanPtg.java (-5 / +2 lines)
Lines 63-69 Link Here
63
import java.util.List;
63
import java.util.List;
64
64
65
//POI
65
//POI
66
import org.apache.poi.hssf.util.SheetReferences;
66
import org.apache.poi.hssf.model.Workbook;
67
67
68
/**
68
/**
69
 * Less than operator PTG "<". The SID is taken from the 
69
 * Less than operator PTG "<". The SID is taken from the 
Lines 142-148 Link Here
142
     * Implementation of method from Ptg 
142
     * Implementation of method from Ptg 
143
     * @param refs the Sheet References
143
     * @param refs the Sheet References
144
     */
144
     */
145
    public String toFormulaString(SheetReferences refs)
145
    public String toFormulaString(Workbook book)
146
    {
146
    {
147
        return this.LESSTHAN;
147
        return this.LESSTHAN;
148
    }
148
    }
Lines 180-185 Link Here
180
    }
180
    }
181
181
182
}
182
}
183
184
185
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/MemErrPtg.java (-2 / +2 lines)
Lines 61-67 Link Here
61
package org.apache.poi.hssf.record.formula;
61
package org.apache.poi.hssf.record.formula;
62
62
63
import org.apache.poi.util.LittleEndian;
63
import org.apache.poi.util.LittleEndian;
64
import org.apache.poi.hssf.util.SheetReferences;
64
import org.apache.poi.hssf.model.Workbook;
65
65
66
/**
66
/**
67
 *
67
 *
Lines 118-124 Link Here
118
        return SIZE;
118
        return SIZE;
119
    }
119
    }
120
120
121
    public String toFormulaString(SheetReferences refs)
121
    public String toFormulaString(Workbook book)
122
    {
122
    {
123
        return "ERR#";
123
        return "ERR#";
124
    }
124
    }
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/MemFuncPtg.java (-2 / +2 lines)
Lines 60-66 Link Here
60
package org.apache.poi.hssf.record.formula;
60
package org.apache.poi.hssf.record.formula;
61
61
62
import org.apache.poi.util.LittleEndian;
62
import org.apache.poi.util.LittleEndian;
63
import org.apache.poi.hssf.util.SheetReferences;
63
import org.apache.poi.hssf.model.Workbook;
64
64
65
/**
65
/**
66
 * @author Glen Stampoultzis (glens at apache.org)
66
 * @author Glen Stampoultzis (glens at apache.org)
Lines 96-102 Link Here
96
        LittleEndian.putShort( array, offset + 1, (short)field_1_len_ref_subexpression );
96
        LittleEndian.putShort( array, offset + 1, (short)field_1_len_ref_subexpression );
97
    }
97
    }
98
98
99
    public String toFormulaString( SheetReferences refs )
99
    public String toFormulaString(Workbook book)
100
    {
100
    {
101
        return "";
101
        return "";
102
    }
102
    }
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/MissingArgPtg.java (-4 / +2 lines)
Lines 54-60 Link Here
54
54
55
package org.apache.poi.hssf.record.formula;
55
package org.apache.poi.hssf.record.formula;
56
56
57
import org.apache.poi.hssf.util.SheetReferences;
57
import org.apache.poi.hssf.model.Workbook;
58
58
59
/**
59
/**
60
 * Missing Function Arguments
60
 * Missing Function Arguments
Lines 91-97 Link Here
91
    }
91
    }
92
92
93
   
93
   
94
    public String toFormulaString(SheetReferences refs)
94
    public String toFormulaString(Workbook book)
95
    {
95
    {
96
        return " ";
96
        return " ";
97
    }
97
    }
Lines 103-107 Link Here
103
    }
103
    }
104
104
105
}
105
}
106
107
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/MultiplyPtg.java (-5 / +5 lines)
Lines 61-67 Link Here
61
package org.apache.poi.hssf.record.formula;
61
package org.apache.poi.hssf.record.formula;
62
62
63
import java.util.List;
63
import java.util.List;
64
import org.apache.poi.hssf.util.SheetReferences;
64
import org.apache.poi.hssf.model.Workbook;
65
65
66
/**
66
/**
67
 * Implements the standard mathmatical multiplication - *
67
 * Implements the standard mathmatical multiplication - *
Lines 114-120 Link Here
114
    }
114
    }
115
    
115
    
116
116
117
    public String toFormulaString(SheetReferences refs)
117
    public String toFormulaString(Workbook book)
118
    {
118
    {
119
        return "*";
119
        return "*";
120
    }
120
    }
Lines 123-131 Link Here
123
    {
123
    {
124
        StringBuffer buffer = new StringBuffer();
124
        StringBuffer buffer = new StringBuffer();
125
125
126
        buffer.append(operands[ 0 ].toFormulaString((SheetReferences)null));
126
        buffer.append(operands[ 0 ].toFormulaString((Workbook)null));
127
        buffer.append("*");
127
        buffer.append("*");
128
        buffer.append(operands[ 1 ].toFormulaString((SheetReferences)null));
128
        buffer.append(operands[ 1 ].toFormulaString((Workbook)null));
129
        return buffer.toString();
129
        return buffer.toString();
130
    }
130
    }
131
    
131
    
Lines 133-139 Link Here
133
        StringBuffer buffer = new StringBuffer();
133
        StringBuffer buffer = new StringBuffer();
134
134
135
        buffer.append(operands[ 0 ]);
135
        buffer.append(operands[ 0 ]);
136
        buffer.append(toFormulaString((SheetReferences)null));
136
        buffer.append(toFormulaString((Workbook)null));
137
        buffer.append(operands[ 1 ]);
137
        buffer.append(operands[ 1 ]);
138
        return buffer.toString();
138
        return buffer.toString();
139
    }                  
139
    }                  
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/NamePtg.java (-6 / +21 lines)
Lines 61-67 Link Here
61
package org.apache.poi.hssf.record.formula;
61
package org.apache.poi.hssf.record.formula;
62
62
63
import org.apache.poi.util.LittleEndian;
63
import org.apache.poi.util.LittleEndian;
64
import org.apache.poi.hssf.util.SheetReferences;
64
import org.apache.poi.hssf.model.Workbook;
65
import org.apache.poi.hssf.record.NameRecord;
65
66
66
/**
67
/**
67
 *
68
 *
Lines 85-93 Link Here
85
86
86
    /** Creates new NamePtg */
87
    /** Creates new NamePtg */
87
88
88
    public NamePtg(String name)
89
    public NamePtg(String name, Workbook book)
89
    {
90
    {
90
        //TODO
91
        final short n = (short) (book.getNumNames() + 1);
92
        NameRecord rec;
93
        for (short i = 1; i < n; i++) {
94
            rec = book.getNameRecord(i - 1);
95
            if (name.equals(rec.getNameText())) {
96
                field_1_label_index = i;
97
                return;
98
            }
99
        }
100
        rec = new NameRecord();
101
        rec.setNameText(name);
102
        rec.setNameTextLength((byte) name.length());
103
        book.addName(rec);
104
        field_1_label_index = n;
91
    }
105
    }
92
106
93
    /** Creates new NamePtg */
107
    /** Creates new NamePtg */
Lines 113-124 Link Here
113
        return SIZE;
127
        return SIZE;
114
    }
128
    }
115
129
116
    public String toFormulaString(SheetReferences refs)
130
    public String toFormulaString(Workbook book)
117
    {
131
    {
118
        return "NAMED RANGE";
132
        NameRecord rec = book.getNameRecord(field_1_label_index - 1);
133
        return rec.getNameText();
119
    }
134
    }
120
    
135
    
121
    public byte getDefaultOperandClass() {return Ptg.CLASS_VALUE;}
136
    public byte getDefaultOperandClass() {return Ptg.CLASS_REF;}
122
137
123
    public Object clone() {
138
    public Object clone() {
124
      NamePtg ptg = new NamePtg();
139
      NamePtg ptg = new NamePtg();
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/NameXPtg.java (-2 / +2 lines)
Lines 61-67 Link Here
61
package org.apache.poi.hssf.record.formula;
61
package org.apache.poi.hssf.record.formula;
62
62
63
import org.apache.poi.util.LittleEndian;
63
import org.apache.poi.util.LittleEndian;
64
import org.apache.poi.hssf.util.SheetReferences;
64
import org.apache.poi.hssf.model.Workbook;
65
65
66
/**
66
/**
67
 *
67
 *
Lines 113-119 Link Here
113
        return SIZE;
113
        return SIZE;
114
    }
114
    }
115
115
116
    public String toFormulaString(SheetReferences refs)
116
    public String toFormulaString(Workbook book)
117
    {
117
    {
118
        return "NO IDEA - NAME";
118
        return "NO IDEA - NAME";
119
    }
119
    }
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/NotEqualPtg.java (-3 / +3 lines)
Lines 56-62 Link Here
56
56
57
import java.util.List;
57
import java.util.List;
58
58
59
import org.apache.poi.hssf.util.SheetReferences;
59
import org.apache.poi.hssf.model.Workbook;
60
60
61
/**
61
/**
62
 * Ptg class to implement not equal
62
 * Ptg class to implement not equal
Lines 101-107 Link Here
101
		  return 2;
101
		  return 2;
102
	 }
102
	 }
103
103
104
	 public String toFormulaString(SheetReferences refs)
104
    public String toFormulaString(Workbook book)
105
	 {
105
	 {
106
		  return "<>";
106
		  return "<>";
107
	 }
107
	 }
Lines 111-117 Link Here
111
111
112
        
112
        
113
		  buffer.append(operands[ 0 ]);
113
		  buffer.append(operands[ 0 ]);
114
		  buffer.append(toFormulaString((SheetReferences)null));
114
		  buffer.append(toFormulaString((Workbook)null));
115
		  buffer.append(operands[ 1 ]);
115
		  buffer.append(operands[ 1 ]);
116
		  return buffer.toString();
116
		  return buffer.toString();
117
	 }       
117
	 }       
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/NumberPtg.java (-3 / +2 lines)
Lines 55-61 Link Here
55
package org.apache.poi.hssf.record.formula;
55
package org.apache.poi.hssf.record.formula;
56
56
57
import org.apache.poi.util.LittleEndian;
57
import org.apache.poi.util.LittleEndian;
58
import org.apache.poi.hssf.util.SheetReferences;
58
import org.apache.poi.hssf.model.Workbook;
59
/**
59
/**
60
 * Number
60
 * Number
61
 * Stores a floating point value in a formula
61
 * Stores a floating point value in a formula
Lines 113-119 Link Here
113
        return SIZE;
113
        return SIZE;
114
    }
114
    }
115
115
116
    public String toFormulaString(SheetReferences refs)
116
    public String toFormulaString(Workbook book)
117
    {
117
    {
118
        return "" + getValue();
118
        return "" + getValue();
119
    }
119
    }
Lines 125-128 Link Here
125
      return ptg;
125
      return ptg;
126
    }
126
    }
127
}
127
}
128
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/ParenthesisPtg.java (-3 / +2 lines)
Lines 57-63 Link Here
57
57
58
import java.util.List;
58
import java.util.List;
59
59
60
import org.apache.poi.hssf.util.SheetReferences;
60
import org.apache.poi.hssf.model.Workbook;
61
61
62
/**
62
/**
63
 * While formula tokens are stored in RPN order and thus do not need parenthesis for 
63
 * While formula tokens are stored in RPN order and thus do not need parenthesis for 
Lines 107-113 Link Here
107
        return 1;
107
        return 1;
108
    }
108
    }
109
109
110
    public String toFormulaString(SheetReferences refs)
110
    public String toFormulaString(Workbook book)
111
    {
111
    {
112
        return "()";
112
        return "()";
113
    }
113
    }
Lines 124-127 Link Here
124
    }
124
    }
125
125
126
}
126
}
127
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/PowerPtg.java (-3 / +3 lines)
Lines 62-68 Link Here
62
62
63
import java.util.List;
63
import java.util.List;
64
64
65
import org.apache.poi.hssf.util.SheetReferences;
65
import org.apache.poi.hssf.model.Workbook;
66
66
67
/**
67
/**
68
 *
68
 *
Lines 108-114 Link Here
108
        return 2;
108
        return 2;
109
    }
109
    }
110
110
111
    public String toFormulaString(SheetReferences refs)
111
    public String toFormulaString(Workbook book)
112
    {
112
    {
113
        return "^";
113
        return "^";
114
    }
114
    }
Lines 118-124 Link Here
118
118
119
        
119
        
120
        buffer.append(operands[ 0 ]);
120
        buffer.append(operands[ 0 ]);
121
        buffer.append(toFormulaString((SheetReferences)null));
121
        buffer.append(toFormulaString((Workbook)null));
122
        buffer.append(operands[ 1 ]);
122
        buffer.append(operands[ 1 ]);
123
        return buffer.toString();
123
        return buffer.toString();
124
    }       
124
    }       
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/Ptg.java (-2 / +2 lines)
Lines 63-69 Link Here
63
import java.util.List;
63
import java.util.List;
64
import java.util.ArrayList;
64
import java.util.ArrayList;
65
65
66
import org.apache.poi.hssf.util.SheetReferences;
66
import org.apache.poi.hssf.model.Workbook;
67
67
68
/**
68
/**
69
 *
69
 *
Lines 347-353 Link Here
347
    /**
347
    /**
348
     * return a string representation of this token alone
348
     * return a string representation of this token alone
349
     */
349
     */
350
    public abstract String toFormulaString(SheetReferences refs);
350
    public abstract String toFormulaString(Workbook book);
351
    /**
351
    /**
352
     * dump a debug representation (hexdump) to a string
352
     * dump a debug representation (hexdump) to a string
353
     */
353
     */
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/Ref3DPtg.java (-1 / +3 lines)
Lines 61-66 Link Here
61
import org.apache.poi.hssf.util.RangeAddress;
61
import org.apache.poi.hssf.util.RangeAddress;
62
import org.apache.poi.hssf.util.CellReference;
62
import org.apache.poi.hssf.util.CellReference;
63
import org.apache.poi.hssf.util.SheetReferences;
63
import org.apache.poi.hssf.util.SheetReferences;
64
import org.apache.poi.hssf.model.Workbook;
64
import org.apache.poi.util.BitField;
65
import org.apache.poi.util.BitField;
65
import org.apache.poi.hssf.model.Workbook;
66
import org.apache.poi.hssf.model.Workbook;
66
67
Lines 193-200 Link Here
193
194
194
    }
195
    }
195
196
196
    public String toFormulaString(SheetReferences refs) {
197
    public String toFormulaString(Workbook book) {
197
        StringBuffer retval = new StringBuffer();
198
        StringBuffer retval = new StringBuffer();
199
        SheetReferences refs = book == null ? null : book.getSheetReferences();
198
        if (refs != null) {
200
        if (refs != null) {
199
            retval.append(refs.getSheetName((int)this.field_1_index_extern_sheet));
201
            retval.append(refs.getSheetName((int)this.field_1_index_extern_sheet));
200
            retval.append('!');
202
            retval.append('!');
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/ReferencePtg.java (-2 / +2 lines)
Lines 64-70 Link Here
64
import org.apache.poi.util.BitField;
64
import org.apache.poi.util.BitField;
65
65
66
import org.apache.poi.hssf.util.CellReference;
66
import org.apache.poi.hssf.util.CellReference;
67
import org.apache.poi.hssf.util.SheetReferences;
67
import org.apache.poi.hssf.model.Workbook;
68
68
69
/**
69
/**
70
 * ReferencePtg - handles references (such as A1, A2, IA4)
70
 * ReferencePtg - handles references (such as A1, A2, IA4)
Lines 179-185 Link Here
179
        return SIZE;
179
        return SIZE;
180
    }
180
    }
181
181
182
    public String toFormulaString(SheetReferences refs)
182
    public String toFormulaString(Workbook book)
183
    {
183
    {
184
        //TODO -- should we store a cellreference instance in this ptg?? but .. memory is an issue, i believe!
184
        //TODO -- should we store a cellreference instance in this ptg?? but .. memory is an issue, i believe!
185
        return (new CellReference(getRow(),getColumn(),!isRowRelative(),!isColRelative())).toString();
185
        return (new CellReference(getRow(),getColumn(),!isRowRelative(),!isColRelative())).toString();
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/StringPtg.java (-3 / +2 lines)
Lines 56-62 Link Here
56
56
57
import org.apache.poi.util.LittleEndian;
57
import org.apache.poi.util.LittleEndian;
58
import org.apache.poi.util.BitField;
58
import org.apache.poi.util.BitField;
59
import org.apache.poi.hssf.util.SheetReferences;
59
import org.apache.poi.hssf.model.Workbook;
60
import org.apache.poi.util.StringUtil;
60
import org.apache.poi.util.StringUtil;
61
61
62
/**
62
/**
Lines 145-151 Link Here
145
        }
145
        }
146
    }
146
    }
147
147
148
    public String toFormulaString(SheetReferences refs)
148
    public String toFormulaString(Workbook book)
149
    {
149
    {
150
        return "\""+getValue()+"\"";
150
        return "\""+getValue()+"\"";
151
    }
151
    }
Lines 162-165 Link Here
162
   }
162
   }
163
163
164
}
164
}
165
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/SubtractPtg.java (-2 / +2 lines)
Lines 61-67 Link Here
61
package org.apache.poi.hssf.record.formula;
61
package org.apache.poi.hssf.record.formula;
62
62
63
import java.util.List;
63
import java.util.List;
64
import org.apache.poi.hssf.util.SheetReferences;
64
import org.apache.poi.hssf.model.Workbook;
65
65
66
/**
66
/**
67
 *
67
 *
Lines 105-111 Link Here
105
        return 2;
105
        return 2;
106
    }
106
    }
107
107
108
    public String toFormulaString(SheetReferences refs)
108
    public String toFormulaString(Workbook book)
109
    {
109
    {
110
        return "-";
110
        return "-";
111
    }
111
    }
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/UnaryMinusPtg.java (-2 / +2 lines)
Lines 56-62 Link Here
56
56
57
import java.util.List;
57
import java.util.List;
58
58
59
import org.apache.poi.hssf.util.SheetReferences;
59
import org.apache.poi.hssf.model.Workbook;
60
60
61
/**
61
/**
62
 * Unary Plus operator
62
 * Unary Plus operator
Lines 105-111 Link Here
105
    }
105
    }
106
    
106
    
107
    /** Implementation of method from Ptg */
107
    /** Implementation of method from Ptg */
108
    public String toFormulaString(SheetReferences refs)
108
    public String toFormulaString(Workbook book)
109
    {
109
    {
110
        return "+";
110
        return "+";
111
    }
111
    }
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/UnaryPlusPtg.java (-2 / +2 lines)
Lines 56-62 Link Here
56
56
57
import java.util.List;
57
import java.util.List;
58
58
59
import org.apache.poi.hssf.util.SheetReferences;
59
import org.apache.poi.hssf.model.Workbook;
60
60
61
/**
61
/**
62
 * Unary Plus operator
62
 * Unary Plus operator
Lines 105-111 Link Here
105
    }
105
    }
106
    
106
    
107
    /** Implementation of method from Ptg */
107
    /** Implementation of method from Ptg */
108
    public String toFormulaString(SheetReferences refs)
108
    public String toFormulaString(Workbook book)
109
    {
109
    {
110
        return "+";
110
        return "+";
111
    }
111
    }
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/UnionPtg.java (-2 / +2 lines)
Lines 54-60 Link Here
54
54
55
package org.apache.poi.hssf.record.formula;
55
package org.apache.poi.hssf.record.formula;
56
56
57
import org.apache.poi.hssf.util.SheetReferences;
57
import org.apache.poi.hssf.model.Workbook;
58
58
59
/**
59
/**
60
 * @author Glen Stampoultzis (glens at apache.org)
60
 * @author Glen Stampoultzis (glens at apache.org)
Lines 95-101 Link Here
95
    }
95
    }
96
96
97
    /** Implementation of method from Ptg */
97
    /** Implementation of method from Ptg */
98
    public String toFormulaString(SheetReferences refs)
98
    public String toFormulaString(Workbook book)
99
    {
99
    {
100
        return ",";
100
        return ",";
101
    }
101
    }
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/formula/UnknownPtg.java (-2 / +2 lines)
Lines 60-66 Link Here
60
 */
60
 */
61
package org.apache.poi.hssf.record.formula;
61
package org.apache.poi.hssf.record.formula;
62
62
63
import org.apache.poi.hssf.util.SheetReferences;
63
import org.apache.poi.hssf.model.Workbook;
64
64
65
/**
65
/**
66
 *
66
 *
Lines 94-100 Link Here
94
        return size;
94
        return size;
95
    }
95
    }
96
96
97
    public String toFormulaString(SheetReferences refs)
97
    public String toFormulaString(Workbook book)
98
    {
98
    {
99
        return "UNKNOWN";
99
        return "UNKNOWN";
100
    }
100
    }
(-)jakarta-poi/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java (-2 / +1 lines)
Lines 726-733 Link Here
726
726
727
    public String getCellFormula() {
727
    public String getCellFormula() {
728
        //Workbook.currentBook=book;
728
        //Workbook.currentBook=book;
729
        SheetReferences refs = book.getSheetReferences();
729
        String retval = FormulaParser.toFormulaString(book, ((FormulaRecordAggregate)record).getFormulaRecord().getParsedExpression());
730
        String retval = FormulaParser.toFormulaString(refs, ((FormulaRecordAggregate)record).getFormulaRecord().getParsedExpression());
731
        //Workbook.currentBook=null;
730
        //Workbook.currentBook=null;
732
        return retval;
731
        return retval;
733
    }
732
    }
(-)jakarta-poi/src/java/org/apache/poi/hssf/usermodel/HSSFName.java (-3 / +1 lines)
Lines 123-130 Link Here
123
123
124
    public String getReference() {
124
    public String getReference() {
125
        String result;
125
        String result;
126
        SheetReferences refs = book.getSheetReferences();
126
        result = name.getAreaReference(book);
127
        result = name.getAreaReference(refs);
128
127
129
        return result;
128
        return result;
130
    }
129
    }
Lines 167-170 Link Here
167
    }
166
    }
168
167
169
}
168
}
170
(-)jakarta-poi/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java (-1 / +1 lines)
Lines 898-904 Link Here
898
		if (name == null) return null;
898
		if (name == null) return null;
899
		//adding one here because 0 indicates a global named region; doesnt make sense for print areas
899
		//adding one here because 0 indicates a global named region; doesnt make sense for print areas
900
   
900
   
901
		return name.getAreaReference(workbook.getSheetReferences());
901
		return name.getAreaReference(workbook);
902
	}    
902
	}    
903
    
903
    
904
    /**
904
    /**
(-)jakarta-poi/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java (-2 / +14 lines)
Lines 56-62 Link Here
56
import junit.framework.TestCase;
56
import junit.framework.TestCase;
57
57
58
import org.apache.poi.hssf.record.formula.*;
58
import org.apache.poi.hssf.record.formula.*;
59
import org.apache.poi.hssf.util.SheetReferences;
60
59
61
/**
60
/**
62
 * Test the low level formula parser functionality. High level tests are to 
61
 * Test the low level formula parser functionality. High level tests are to 
Lines 145-151 Link Here
145
        assertEquals(true, flag.getValue());
144
        assertEquals(true, flag.getValue());
146
        assertEquals("Y", y.getValue());
145
        assertEquals("Y", y.getValue());
147
        assertEquals("N", n.getValue());
146
        assertEquals("N", n.getValue());
148
        assertEquals("IF", funif.toFormulaString(new SheetReferences()));
147
        assertEquals("IF", funif.toFormulaString((Workbook) null));
149
        assertTrue("Goto ptg exists", goto1.isGoto());
148
        assertTrue("Goto ptg exists", goto1.isGoto());
150
    }
149
    }
151
150
Lines 285-290 Link Here
285
		
284
		
286
	}
285
	}
287
	    
286
	    
287
    public void testMacroFunction() {
288
        Workbook w = new Workbook();
289
        FormulaParser fp = new FormulaParser("FOO()", w);
290
        fp.parse();
291
        Ptg[] ptg = fp.getRPNPtg();
292
293
        AbstractFunctionPtg tfunc = (AbstractFunctionPtg) ptg[0];
294
        assertEquals("externalflag", tfunc.getName());
295
296
        NamePtg tname = (NamePtg) ptg[1];
297
        assertEquals("FOO", tname.toFormulaString(w));
298
    }
299
288
     public static void main(String [] args) {
300
     public static void main(String [] args) {
289
        System.out.println("Testing org.apache.poi.hssf.record.formula.FormulaParser");
301
        System.out.println("Testing org.apache.poi.hssf.record.formula.FormulaParser");
290
        junit.textui.TestRunner.run(TestFormulaParser.class);
302
        junit.textui.TestRunner.run(TestFormulaParser.class);
(-)jakarta-poi/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java (-6 / +52 lines)
Lines 744-750 Link Here
744
    }
744
    }
745
    
745
    
746
    public void testSheetFunctions()
746
    public void testSheetFunctions()
747
        throws java.io.IOException
747
        throws IOException
748
    {
748
    {
749
        String filename = System.getProperty("HSSF.testdata.path");
749
        String filename = System.getProperty("HSSF.testdata.path");
750
750
Lines 818-824 Link Here
818
    }
818
    }
819
    
819
    
820
    public void testStringFormulas()
820
    public void testStringFormulas()
821
        throws java.io.IOException
821
        throws IOException
822
    {
822
    {
823
        String readFilename = System.getProperty("HSSF.testdata.path");
823
        String readFilename = System.getProperty("HSSF.testdata.path");
824
824
Lines 852-858 Link Here
852
    
852
    
853
    
853
    
854
    public void testLogicalFormulas()
854
    public void testLogicalFormulas()
855
        throws java.io.IOException
855
        throws IOException
856
    {
856
    {
857
857
858
            File file = File.createTempFile("testLogicalFormula",".xls");
858
            File file = File.createTempFile("testLogicalFormula",".xls");
Lines 880-886 Link Here
880
    }
880
    }
881
    
881
    
882
    public void testDateFormulas()
882
    public void testDateFormulas()
883
        throws java.io.IOException
883
        throws IOException
884
    {
884
    {
885
        String readFilename = System.getProperty("HSSF.testdata.path");
885
        String readFilename = System.getProperty("HSSF.testdata.path");
886
886
Lines 918-924 Link Here
918
918
919
    
919
    
920
    public void testIfFormulas()
920
    public void testIfFormulas()
921
        throws java.io.IOException
921
        throws IOException
922
    {
922
    {
923
        String readFilename = System.getProperty("HSSF.testdata.path");
923
        String readFilename = System.getProperty("HSSF.testdata.path");
924
924
Lines 1008-1014 Link Here
1008
    }
1008
    }
1009
1009
1010
	public void testSumIf()
1010
	public void testSumIf()
1011
		throws java.io.IOException
1011
		throws IOException
1012
	{
1012
	{
1013
		String readFilename = System.getProperty("HSSF.testdata.path");		
1013
		String readFilename = System.getProperty("HSSF.testdata.path");		
1014
		String function ="SUMIF(A1:A5,\">4000\",B1:B5)";
1014
		String function ="SUMIF(A1:A5,\">4000\",B1:B5)";
Lines 1060-1065 Link Here
1060
		assertTrue("sumif == 0 bytes", file.length() > 0);
1060
		assertTrue("sumif == 0 bytes", file.length() > 0);
1061
	}
1061
	}
1062
    
1062
    
1063
    public void testSquareMacro() throws IOException {
1064
        File dir = new File(System.getProperty("HSSF.testdata.path"));
1065
        File xls = new File(dir, "SquareMacro.xls");
1066
        FileInputStream in = new FileInputStream(xls);
1067
        HSSFWorkbook w;
1068
        try {
1069
            w = new HSSFWorkbook(in);
1070
        } finally {
1071
            in.close();
1072
        }
1073
        HSSFSheet s0 = w.getSheetAt(0);
1074
        HSSFRow[] r = {s0.getRow(0), s0.getRow(1)};
1075
1076
        HSSFCell a1 = r[0].getCell((short) 0);
1077
        assertEquals("square(1)", a1.getCellFormula());
1078
        assertEquals(1d, a1.getNumericCellValue(), 1e-9);
1079
1080
        HSSFCell a2 = r[1].getCell((short) 0);
1081
        assertEquals("square(2)", a2.getCellFormula());
1082
        assertEquals(4d, a2.getNumericCellValue(), 1e-9);
1083
1084
        HSSFCell b1 = r[0].getCell((short) 1);
1085
        assertEquals("IF(TRUE,square(1))", b1.getCellFormula());
1086
        assertEquals(1d, b1.getNumericCellValue(), 1e-9);
1087
1088
        HSSFCell b2 = r[1].getCell((short) 1);
1089
        assertEquals("IF(TRUE,square(2))", b2.getCellFormula());
1090
        assertEquals(4d, b2.getNumericCellValue(), 1e-9);
1091
1092
        HSSFCell c1 = r[0].getCell((short) 2);
1093
        assertEquals("square(square(1))", c1.getCellFormula());
1094
        assertEquals(1d, c1.getNumericCellValue(), 1e-9);
1095
1096
        HSSFCell c2 = r[1].getCell((short) 2);
1097
        assertEquals("square(square(2))", c2.getCellFormula());
1098
        assertEquals(16d, c2.getNumericCellValue(), 1e-9);
1099
1100
        HSSFCell d1 = r[0].getCell((short) 3);
1101
        assertEquals("square(one())", d1.getCellFormula());
1102
        assertEquals(1d, d1.getNumericCellValue(), 1e-9);
1103
1104
        HSSFCell d2 = r[1].getCell((short) 3);
1105
        assertEquals("square(two())", d2.getCellFormula());
1106
        assertEquals(4d, d2.getNumericCellValue(), 1e-9);
1107
    }
1108
1063
    public static void main(String [] args) {
1109
    public static void main(String [] args) {
1064
        System.out
1110
        System.out
1065
        .println("Testing org.apache.poi.hssf.usermodel.TestFormulas");
1111
        .println("Testing org.apache.poi.hssf.usermodel.TestFormulas");

Return to bug 21444