Search in sources :

Example 1 with FormulaParseException

use of org.apache.poi.ss.formula.FormulaParseException in project poi by apache.

the class TestFormulaParser method testParseComplexName.

/**
     * In bug 47078, POI had trouble evaluating a defined name flagged as 'complex'.
     * POI should also be able to parse such defined names.
     */
@Test
public void testParseComplexName() throws IOException {
    // Mock up a spreadsheet to match the critical details of the sample
    HSSFWorkbook wb = new HSSFWorkbook();
    wb.createSheet("Sheet1");
    HSSFName definedName = wb.createName();
    definedName.setNameName("foo");
    definedName.setRefersToFormula("Sheet1!B2");
    // Set the complex flag - POI doesn't usually manipulate this flag
    NameRecord nameRec = TestHSSFName.getNameRecord(definedName);
    // 0x10 -> complex
    nameRec.setOptionFlag((short) 0x10);
    Ptg[] result;
    try {
        result = HSSFFormulaParser.parse("1+foo", wb);
    } catch (FormulaParseException e) {
        if (e.getMessage().equals("Specified name 'foo' is not a range as expected.")) {
            fail("Identified bug 47078c");
        }
        wb.close();
        throw e;
    }
    confirmTokenClasses(result, IntPtg.class, NamePtg.class, AddPtg.class);
    wb.close();
}
Also used : FormulaParseException(org.apache.poi.ss.formula.FormulaParseException) TestHSSFName(org.apache.poi.hssf.usermodel.TestHSSFName) HSSFName(org.apache.poi.hssf.usermodel.HSSFName) NumberPtg(org.apache.poi.ss.formula.ptg.NumberPtg) ArrayPtg(org.apache.poi.ss.formula.ptg.ArrayPtg) AttrPtg(org.apache.poi.ss.formula.ptg.AttrPtg) PercentPtg(org.apache.poi.ss.formula.ptg.PercentPtg) RangePtg(org.apache.poi.ss.formula.ptg.RangePtg) AddPtg(org.apache.poi.ss.formula.ptg.AddPtg) EqualPtg(org.apache.poi.ss.formula.ptg.EqualPtg) UnaryMinusPtg(org.apache.poi.ss.formula.ptg.UnaryMinusPtg) NameXPtg(org.apache.poi.ss.formula.ptg.NameXPtg) RefPtg(org.apache.poi.ss.formula.ptg.RefPtg) DividePtg(org.apache.poi.ss.formula.ptg.DividePtg) GreaterThanPtg(org.apache.poi.ss.formula.ptg.GreaterThanPtg) MultiplyPtg(org.apache.poi.ss.formula.ptg.MultiplyPtg) Ref3DPtg(org.apache.poi.ss.formula.ptg.Ref3DPtg) StringPtg(org.apache.poi.ss.formula.ptg.StringPtg) ErrPtg(org.apache.poi.ss.formula.ptg.ErrPtg) Ptg(org.apache.poi.ss.formula.ptg.Ptg) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) NamePtg(org.apache.poi.ss.formula.ptg.NamePtg) MemAreaPtg(org.apache.poi.ss.formula.ptg.MemAreaPtg) ConcatPtg(org.apache.poi.ss.formula.ptg.ConcatPtg) UnaryPlusPtg(org.apache.poi.ss.formula.ptg.UnaryPlusPtg) BoolPtg(org.apache.poi.ss.formula.ptg.BoolPtg) IntersectionPtg(org.apache.poi.ss.formula.ptg.IntersectionPtg) AbstractFunctionPtg(org.apache.poi.ss.formula.ptg.AbstractFunctionPtg) IntPtg(org.apache.poi.ss.formula.ptg.IntPtg) UnionPtg(org.apache.poi.ss.formula.ptg.UnionPtg) FuncVarPtg(org.apache.poi.ss.formula.ptg.FuncVarPtg) SubtractPtg(org.apache.poi.ss.formula.ptg.SubtractPtg) FuncPtg(org.apache.poi.ss.formula.ptg.FuncPtg) MissingArgPtg(org.apache.poi.ss.formula.ptg.MissingArgPtg) MemFuncPtg(org.apache.poi.ss.formula.ptg.MemFuncPtg) PowerPtg(org.apache.poi.ss.formula.ptg.PowerPtg) AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg) ParenthesisPtg(org.apache.poi.ss.formula.ptg.ParenthesisPtg) NameRecord(org.apache.poi.hssf.record.NameRecord) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 2 with FormulaParseException

use of org.apache.poi.ss.formula.FormulaParseException in project poi by apache.

the class TestFormulaParser method confirmArgCountMsg.

private static void confirmArgCountMsg(String formula, String expectedMessage) throws IOException {
    HSSFWorkbook book = new HSSFWorkbook();
    try {
        HSSFFormulaParser.parse(formula, book);
        fail("Didn't get parse exception as expected");
    } catch (FormulaParseException e) {
        confirmParseException(e, expectedMessage);
    }
    book.close();
}
Also used : FormulaParseException(org.apache.poi.ss.formula.FormulaParseException) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 3 with FormulaParseException

use of org.apache.poi.ss.formula.FormulaParseException in project poi by apache.

the class TestFormulaParser method testZeroRowRefs.

/**
     * Zero is not a valid row number so cell references like 'A0' are not valid.
     * Actually, they should be treated like defined names.
     * <br/>
     * In addition, leading zeros (on the row component) should be removed from cell
     * references during parsing.
     */
@Test
public void testZeroRowRefs() throws IOException {
    // bad because zero is not a valid row number
    String badCellRef = "B0";
    // this should get parsed as "B1"
    String leadingZeroCellRef = "B000001";
    HSSFWorkbook wb = new HSSFWorkbook();
    try {
        HSSFFormulaParser.parse(badCellRef, wb);
        fail("Identified bug 47312b - Shouldn't be able to parse cell ref '" + badCellRef + "'.");
    } catch (FormulaParseException e) {
        // expected during successful test
        confirmParseException(e, "Specified named range '" + badCellRef + "' does not exist in the current workbook.");
    }
    Ptg[] ptgs;
    try {
        ptgs = HSSFFormulaParser.parse(leadingZeroCellRef, wb);
        assertEquals("B1", ((RefPtg) ptgs[0]).toFormulaString());
    } catch (FormulaParseException e) {
        confirmParseException(e, "Specified named range '" + leadingZeroCellRef + "' does not exist in the current workbook.");
        // close but no cigar
        fail("Identified bug 47312c - '" + leadingZeroCellRef + "' should parse as 'B1'.");
    }
    // create a defined name called 'B0' and try again
    Name n = wb.createName();
    n.setNameName("B0");
    n.setRefersToFormula("1+1");
    ptgs = HSSFFormulaParser.parse("B0", wb);
    confirmTokenClasses(ptgs, NamePtg.class);
    wb.close();
}
Also used : FormulaParseException(org.apache.poi.ss.formula.FormulaParseException) NumberPtg(org.apache.poi.ss.formula.ptg.NumberPtg) ArrayPtg(org.apache.poi.ss.formula.ptg.ArrayPtg) AttrPtg(org.apache.poi.ss.formula.ptg.AttrPtg) PercentPtg(org.apache.poi.ss.formula.ptg.PercentPtg) RangePtg(org.apache.poi.ss.formula.ptg.RangePtg) AddPtg(org.apache.poi.ss.formula.ptg.AddPtg) EqualPtg(org.apache.poi.ss.formula.ptg.EqualPtg) UnaryMinusPtg(org.apache.poi.ss.formula.ptg.UnaryMinusPtg) NameXPtg(org.apache.poi.ss.formula.ptg.NameXPtg) RefPtg(org.apache.poi.ss.formula.ptg.RefPtg) DividePtg(org.apache.poi.ss.formula.ptg.DividePtg) GreaterThanPtg(org.apache.poi.ss.formula.ptg.GreaterThanPtg) MultiplyPtg(org.apache.poi.ss.formula.ptg.MultiplyPtg) Ref3DPtg(org.apache.poi.ss.formula.ptg.Ref3DPtg) StringPtg(org.apache.poi.ss.formula.ptg.StringPtg) ErrPtg(org.apache.poi.ss.formula.ptg.ErrPtg) Ptg(org.apache.poi.ss.formula.ptg.Ptg) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) NamePtg(org.apache.poi.ss.formula.ptg.NamePtg) MemAreaPtg(org.apache.poi.ss.formula.ptg.MemAreaPtg) ConcatPtg(org.apache.poi.ss.formula.ptg.ConcatPtg) UnaryPlusPtg(org.apache.poi.ss.formula.ptg.UnaryPlusPtg) BoolPtg(org.apache.poi.ss.formula.ptg.BoolPtg) IntersectionPtg(org.apache.poi.ss.formula.ptg.IntersectionPtg) AbstractFunctionPtg(org.apache.poi.ss.formula.ptg.AbstractFunctionPtg) IntPtg(org.apache.poi.ss.formula.ptg.IntPtg) UnionPtg(org.apache.poi.ss.formula.ptg.UnionPtg) FuncVarPtg(org.apache.poi.ss.formula.ptg.FuncVarPtg) SubtractPtg(org.apache.poi.ss.formula.ptg.SubtractPtg) FuncPtg(org.apache.poi.ss.formula.ptg.FuncPtg) MissingArgPtg(org.apache.poi.ss.formula.ptg.MissingArgPtg) MemFuncPtg(org.apache.poi.ss.formula.ptg.MemFuncPtg) PowerPtg(org.apache.poi.ss.formula.ptg.PowerPtg) AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg) ParenthesisPtg(org.apache.poi.ss.formula.ptg.ParenthesisPtg) UnicodeString(org.apache.poi.hssf.record.common.UnicodeString) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Name(org.apache.poi.ss.usermodel.Name) TestHSSFName(org.apache.poi.hssf.usermodel.TestHSSFName) HSSFName(org.apache.poi.hssf.usermodel.HSSFName) Test(org.junit.Test)

Example 4 with FormulaParseException

use of org.apache.poi.ss.formula.FormulaParseException in project poi by apache.

the class TestFormulaParserEval method testEvaluateFormulaWithRowBeyond32768_Bug44539.

public void testEvaluateFormulaWithRowBeyond32768_Bug44539() {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet();
    wb.setSheetName(0, "Sheet1");
    HSSFRow row = sheet.createRow(0);
    HSSFCell cell = row.createCell(0);
    cell.setCellFormula("SUM(A32769:A32770)");
    // put some values in the cells to make the evaluation more interesting
    sheet.createRow(32768).createCell(0).setCellValue(31);
    sheet.createRow(32769).createCell(0).setCellValue(11);
    HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
    CellValue result;
    try {
        result = fe.evaluate(cell);
    } catch (FormulaParseException e) {
        if (!e.getMessage().equals("Found reference to named range \"A\", but that named range wasn't defined!")) {
            throw new AssertionFailedError("Identifed bug 44539");
        }
        throw e;
    }
    assertEquals(CellType.NUMERIC, result.getCellTypeEnum());
    assertEquals(42.0, result.getNumberValue(), 0.0);
}
Also used : HSSFFormulaEvaluator(org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator) FormulaParseException(org.apache.poi.ss.formula.FormulaParseException) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) CellValue(org.apache.poi.ss.usermodel.CellValue) AssertionFailedError(junit.framework.AssertionFailedError) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 5 with FormulaParseException

use of org.apache.poi.ss.formula.FormulaParseException in project poi by apache.

the class XSSFRowShifter method shiftFormula.

/**
     * Shift a formula using the supplied FormulaShifter
     *
     * @param row     the row of the cell this formula belongs to. Used to get a reference to the parent workbook.
     * @param formula the formula to shift
     * @param shifter the FormulaShifter object that operates on the parsed formula tokens
     * @return the shifted formula if the formula was changed,
     *         <code>null</code> if the formula wasn't modified
     */
private static String shiftFormula(Row row, String formula, FormulaShifter shifter) {
    Sheet sheet = row.getSheet();
    Workbook wb = sheet.getWorkbook();
    int sheetIndex = wb.getSheetIndex(sheet);
    final int rowIndex = row.getRowNum();
    XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create((XSSFWorkbook) wb);
    try {
        Ptg[] ptgs = FormulaParser.parse(formula, fpb, FormulaType.CELL, sheetIndex, rowIndex);
        String shiftedFmla = null;
        if (shifter.adjustFormula(ptgs, sheetIndex)) {
            shiftedFmla = FormulaRenderer.toFormulaString(fpb, ptgs);
        }
        return shiftedFmla;
    } catch (FormulaParseException fpe) {
        // Log, but don't change, rather than breaking
        logger.log(POILogger.WARN, "Error shifting formula on row ", row.getRowNum(), fpe);
        return formula;
    }
}
Also used : FormulaParseException(org.apache.poi.ss.formula.FormulaParseException) XSSFEvaluationWorkbook(org.apache.poi.xssf.usermodel.XSSFEvaluationWorkbook) Ptg(org.apache.poi.ss.formula.ptg.Ptg) AreaErrPtg(org.apache.poi.ss.formula.ptg.AreaErrPtg) AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg) Sheet(org.apache.poi.ss.usermodel.Sheet) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFEvaluationWorkbook(org.apache.poi.xssf.usermodel.XSSFEvaluationWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook)

Aggregations

FormulaParseException (org.apache.poi.ss.formula.FormulaParseException)8 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)5 AreaPtg (org.apache.poi.ss.formula.ptg.AreaPtg)4 Ptg (org.apache.poi.ss.formula.ptg.Ptg)4 Test (org.junit.Test)4 HSSFName (org.apache.poi.hssf.usermodel.HSSFName)3 TestHSSFName (org.apache.poi.hssf.usermodel.TestHSSFName)3 AbstractFunctionPtg (org.apache.poi.ss.formula.ptg.AbstractFunctionPtg)3 AddPtg (org.apache.poi.ss.formula.ptg.AddPtg)3 Area3DPtg (org.apache.poi.ss.formula.ptg.Area3DPtg)3 ArrayPtg (org.apache.poi.ss.formula.ptg.ArrayPtg)3 AttrPtg (org.apache.poi.ss.formula.ptg.AttrPtg)3 BoolPtg (org.apache.poi.ss.formula.ptg.BoolPtg)3 ConcatPtg (org.apache.poi.ss.formula.ptg.ConcatPtg)3 DividePtg (org.apache.poi.ss.formula.ptg.DividePtg)3 EqualPtg (org.apache.poi.ss.formula.ptg.EqualPtg)3 ErrPtg (org.apache.poi.ss.formula.ptg.ErrPtg)3 FuncPtg (org.apache.poi.ss.formula.ptg.FuncPtg)3 FuncVarPtg (org.apache.poi.ss.formula.ptg.FuncVarPtg)3 GreaterThanPtg (org.apache.poi.ss.formula.ptg.GreaterThanPtg)3