Search in sources :

Example 6 with FormulaParseException

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

the class Indirect method evaluateIndirect.

private static ValueEval evaluateIndirect(final OperationEvaluationContext ec, String text, boolean isA1style) {
    // Search backwards for '!' because sheet names can contain '!'
    int plingPos = text.lastIndexOf('!');
    String workbookName;
    String sheetName;
    // whitespace around this gets trimmed OK
    String refText;
    if (plingPos < 0) {
        workbookName = null;
        sheetName = null;
        refText = text;
    } else {
        String[] parts = parseWorkbookAndSheetName(text.subSequence(0, plingPos));
        if (parts == null) {
            return ErrorEval.REF_INVALID;
        }
        workbookName = parts[0];
        sheetName = parts[1];
        refText = text.substring(plingPos + 1);
    }
    if (Table.isStructuredReference.matcher(refText).matches()) {
        // The argument is structured reference
        Area3DPxg areaPtg = null;
        try {
            areaPtg = FormulaParser.parseStructuredReference(refText, (FormulaParsingWorkbook) ec.getWorkbook(), ec.getRowIndex());
        } catch (FormulaParseException e) {
            return ErrorEval.REF_INVALID;
        }
        return ec.getArea3DEval(areaPtg);
    } else {
        // The argument is regular reference
        String refStrPart1;
        String refStrPart2;
        int colonPos = refText.indexOf(':');
        if (colonPos < 0) {
            refStrPart1 = refText.trim();
            refStrPart2 = null;
        } else {
            refStrPart1 = refText.substring(0, colonPos).trim();
            refStrPart2 = refText.substring(colonPos + 1).trim();
        }
        return ec.getDynamicReference(workbookName, sheetName, refStrPart1, refStrPart2, isA1style);
    }
}
Also used : Area3DPxg(org.apache.poi.ss.formula.ptg.Area3DPxg) FormulaParsingWorkbook(org.apache.poi.ss.formula.FormulaParsingWorkbook) FormulaParseException(org.apache.poi.ss.formula.FormulaParseException)

Example 7 with FormulaParseException

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

the class TestFormulaParser method testNamedRangeThatLooksLikeCell.

@Test
public void testNamedRangeThatLooksLikeCell() throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("Sheet1");
    HSSFName name = wb.createName();
    name.setRefersToFormula("Sheet1!B1");
    name.setNameName("pfy1");
    Ptg[] ptgs;
    try {
        ptgs = HSSFFormulaParser.parse("count(pfy1)", wb);
    } catch (IllegalArgumentException e) {
        if (e.getMessage().equals("Specified colIx (1012) is out of range")) {
            fail("Identified bug 45354");
        }
        wb.close();
        throw e;
    }
    confirmTokenClasses(ptgs, NamePtg.class, FuncVarPtg.class);
    HSSFCell cell = sheet.createRow(0).createCell(0);
    cell.setCellFormula("count(pfy1)");
    assertEquals("COUNT(pfy1)", cell.getCellFormula());
    try {
        cell.setCellFormula("count(pf1)");
        fail("Expected formula parse execption");
    } catch (FormulaParseException e) {
        confirmParseException(e, "Specified named range 'pf1' does not exist in the current workbook.");
    }
    // plain cell ref, col is in range
    cell.setCellFormula("count(fp1)");
    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) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 8 with FormulaParseException

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

the class BaseTestSheetUpdateArrayFormulas method testSetArrayFormula_incorrectFormula.

/**
     * Passing an incorrect formula to sheet.setArrayFormula
     *  should throw FormulaParseException
     */
@Test
public final void testSetArrayFormula_incorrectFormula() throws IOException {
    Workbook workbook = _testDataProvider.createWorkbook();
    Sheet sheet = workbook.createSheet();
    try {
        sheet.setArrayFormula("incorrect-formula(C11_C12*D11_D12)", new CellRangeAddress(10, 10, 10, 10));
        fail("expected exception");
    } catch (FormulaParseException e) {
    //expected exception
    }
    workbook.close();
}
Also used : FormulaParseException(org.apache.poi.ss.formula.FormulaParseException) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) Test(org.junit.Test)

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