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

(-)C:/D/eclipseworkspaces/Trial/jakarta-poi/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java (-3 / +10 lines)
Lines 597-604 Link Here
597
        } else {
597
        } else {
598
            setCellType(CELL_TYPE_FORMULA,false,row,col,styleIndex);
598
            setCellType(CELL_TYPE_FORMULA,false,row,col,styleIndex);
599
            FormulaRecordAggregate rec = (FormulaRecordAggregate) record;
599
            FormulaRecordAggregate rec = (FormulaRecordAggregate) record;
600
            rec.getFormulaRecord().setOptions(( short ) 2);
600
            FormulaRecord frec = rec.getFormulaRecord();
601
            rec.getFormulaRecord().setValue(0);
601
            frec.setOptions(( short ) 2);
602
            frec.setValue(0);
602
            
603
            
603
            //only set to default if there is no extended format index already set
604
            //only set to default if there is no extended format index already set
604
            if (rec.getXFIndex() == (short)0) rec.setXFIndex(( short ) 0x0f);
605
            if (rec.getXFIndex() == (short)0) rec.setXFIndex(( short ) 0x0f);
Lines 607-615 Link Here
607
            Ptg[] ptg  = fp.getRPNPtg();
608
            Ptg[] ptg  = fp.getRPNPtg();
608
            int   size = 0;
609
            int   size = 0;
609
            //System.out.println("got Ptgs " + ptg.length);
610
            //System.out.println("got Ptgs " + ptg.length);
611
            
612
            // clear the Ptg Stack
613
            for (int i=0, iSize=frec.getNumberOfExpressionTokens(); i<iSize; i++) {
614
                frec.popExpressionToken();
615
            }
616
            
610
            for (int k = 0; k < ptg.length; k++) {
617
            for (int k = 0; k < ptg.length; k++) {
611
                size += ptg[ k ].getSize();
618
                size += ptg[ k ].getSize();
612
                rec.getFormulaRecord().pushExpressionToken(ptg[ k ]);
619
                frec.pushExpressionToken(ptg[ k ]);
613
            }
620
            }
614
            rec.getFormulaRecord().setExpressionLength(( short ) size);
621
            rec.getFormulaRecord().setExpressionLength(( short ) size);
615
            //Workbook.currentBook = null;
622
            //Workbook.currentBook = null;
(-)C:/D/eclipseworkspaces/Trial/jakarta-poi/src/java/org/apache/poi/hssf/model/FormulaParser.java (-4 / +33 lines)
Lines 229-244 Link Here
229
    }
229
    }
230
    
230
    
231
    
231
    
232
    /** Get the exponent for numbers of form 1.3E21 */
233
    private String GetExponent() {
234
        StringBuffer retval = new StringBuffer();
235
        String sign = "";
236
        GetChar();
237
        if ('-' == look) {
238
            sign = "-";
239
            GetChar();
240
        }
241
        while (IsDigit(look)) {
242
            retval.append(look);
243
            GetChar();
244
        }
245
        if (retval.length() > 0) {
246
            retval.insert(0, sign);
247
        }
248
        return retval.toString();
249
    }
250
    
232
    /** Get a Number */
251
    /** Get a Number */
233
    private String GetNum() {
252
    private String GetNum() {
234
        String Value ="";
253
        StringBuffer value = new StringBuffer();
235
        if  (!IsDigit(look)) Expected("Integer");
254
        if  (!IsDigit(look)) Expected("Integer");
236
        while (IsDigit(look)){
255
        while (IsDigit(look)){
237
            Value = Value + look;
256
            value.append(look);
238
            GetChar();
257
            GetChar();
239
        }
258
        }
240
        SkipWhite();
259
        SkipWhite();
241
        return Value;
260
        return value.toString();
242
    }
261
    }
243
262
244
    /** Output a String with Tab */
263
    /** Output a String with Tab */
Lines 482-489 Link Here
482
                Match('.');
501
                Match('.');
483
                String decimalPart = null;
502
                String decimalPart = null;
484
                if (IsDigit(look)) number = number +"."+ GetNum(); //this also takes care of someone entering "1234."
503
                if (IsDigit(look)) number = number +"."+ GetNum(); //this also takes care of someone entering "1234."
504
                if ('E' == look) {
505
                    String exponent = GetExponent();
506
                    number += 'E' + exponent;
507
                }
485
                tokens.add(new NumberPtg(number));
508
                tokens.add(new NumberPtg(number));
486
            } else {
509
            } 
510
            else if ('E' == look) {
511
                String exponent = GetExponent();
512
                number += 'E'+exponent;
513
                tokens.add(new NumberPtg(number));
514
            }
515
            else {
487
                tokens.add(getNumberPtgFromString(number));  //TODO:what if the number is too big to be a short? ..add factory to return Int or Number!
516
                tokens.add(getNumberPtgFromString(number));  //TODO:what if the number is too big to be a short? ..add factory to return Int or Number!
488
            }
517
            }
489
        }
518
        }
(-)C:/D/eclipseworkspaces/Trial/jakarta-poi/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java (-1 / +67 lines)
Lines 17-22 Link Here
17
        
17
        
18
package org.apache.poi.hssf.model;
18
package org.apache.poi.hssf.model;
19
19
20
import java.io.FileOutputStream;
21
20
import junit.framework.TestCase;
22
import junit.framework.TestCase;
21
23
22
import org.apache.poi.hssf.record.formula.AbstractFunctionPtg;
24
import org.apache.poi.hssf.record.formula.AbstractFunctionPtg;
Lines 38-43 Link Here
38
import org.apache.poi.hssf.record.formula.UnaryMinusPtg;
40
import org.apache.poi.hssf.record.formula.UnaryMinusPtg;
39
import org.apache.poi.hssf.record.formula.UnaryPlusPtg;
41
import org.apache.poi.hssf.record.formula.UnaryPlusPtg;
40
import org.apache.poi.hssf.usermodel.HSSFCell;
42
import org.apache.poi.hssf.usermodel.HSSFCell;
43
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
41
import org.apache.poi.hssf.usermodel.HSSFRow;
44
import org.apache.poi.hssf.usermodel.HSSFRow;
42
import org.apache.poi.hssf.usermodel.HSSFSheet;
45
import org.apache.poi.hssf.usermodel.HSSFSheet;
43
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
46
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
Lines 404-410 Link Here
404
		
407
		
405
	}
408
	}
406
409
407
     public static void main(String [] args) {
410
    // bug 38396 : Formula with exponential numbers not parsed correctly.
411
    public void testExponentialParsing() {
412
        FormulaParser fp = new FormulaParser("1.3E21/2", null);
413
        fp.parse();
414
        Ptg[] ptgs = fp.getRPNPtg();
415
        assertTrue("three tokens expected, got "+ptgs.length,ptgs.length == 3);
416
        assertTrue("NumberPtg",(ptgs[0] instanceof NumberPtg));
417
        assertTrue("IntPtg",(ptgs[1] instanceof IntPtg));
418
        assertTrue("DividePtg",(ptgs[2] instanceof DividePtg));
419
420
        fp = new FormulaParser("1322E21/2", null);
421
        fp.parse();
422
        ptgs = fp.getRPNPtg();
423
        assertTrue("three tokens expected, got "+ptgs.length,ptgs.length == 3);
424
        assertTrue("NumberPtg",(ptgs[0] instanceof NumberPtg));
425
        assertTrue("IntPtg",(ptgs[1] instanceof IntPtg));
426
        assertTrue("DividePtg",(ptgs[2] instanceof DividePtg));
427
428
        fp = new FormulaParser("1.3E1/2", null);
429
        fp.parse();
430
        ptgs = fp.getRPNPtg();
431
        assertTrue("three tokens expected, got "+ptgs.length,ptgs.length == 3);
432
        assertTrue("NumberPtg",(ptgs[0] instanceof NumberPtg));
433
        assertTrue("IntPtg",(ptgs[1] instanceof IntPtg));
434
        assertTrue("DividePtg",(ptgs[2] instanceof DividePtg));
435
436
    }
437
    public void testExponentialInSheet() throws Exception {
438
        HSSFWorkbook wb = new HSSFWorkbook();
439
        
440
        wb.createSheet("Cash_Flow");;
441
        
442
        HSSFSheet sheet = wb.createSheet("Test");
443
        HSSFRow row = sheet.createRow(0);
444
        HSSFCell cell = row.createCell((short)0);
445
        String formula = null;
446
447
        cell.setCellFormula("1.3E21/3");
448
        formula = cell.getCellFormula();
449
        assertEquals("Exponential formula string", "1.3E21/3", formula);
450
        
451
        cell.setCellFormula("1322E21/3");
452
        formula = cell.getCellFormula();
453
        assertEquals("Exponential formula string", "1.322E24/3", formula);
454
455
        cell.setCellFormula("1.3E1/3");
456
        formula = cell.getCellFormula();
457
        assertEquals("Exponential formula string", "13.0/3", formula);
458
459
        cell.setCellFormula("1.3E-4/3");
460
        formula = cell.getCellFormula();
461
        assertEquals("Exponential formula string", "1.3E-4/3", formula);
462
463
        cell.setCellFormula("13E-15/3");
464
        formula = cell.getCellFormula();
465
        assertEquals("Exponential formula string", "1.3E-14/3", formula);
466
467
        cell.setCellFormula("1.3E3/3");
468
        formula = cell.getCellFormula();
469
        assertEquals("Exponential formula string", "1300.0/3", formula);
470
        
471
    }
472
473
    public static void main(String [] args) {
408
        System.out.println("Testing org.apache.poi.hssf.record.formula.FormulaParser");
474
        System.out.println("Testing org.apache.poi.hssf.record.formula.FormulaParser");
409
        junit.textui.TestRunner.run(TestFormulaParser.class);
475
        junit.textui.TestRunner.run(TestFormulaParser.class);
410
    }
476
    }

Return to bug 38396