Search in sources :

Example 1 with HSSFName

use of org.apache.poi.hssf.usermodel.HSSFName 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 HSSFName

use of org.apache.poi.hssf.usermodel.HSSFName in project poi by apache.

the class TestFormulaParser method testNamesWithUnderscore.

/** bug 49725, defined names with underscore */
@Test
public void testNamesWithUnderscore() throws IOException {
    //or new XSSFWorkbook();
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("NamesWithUnderscore");
    HSSFName nm;
    nm = wb.createName();
    nm.setNameName("DA6_LEO_WBS_Number");
    nm.setRefersToFormula("33");
    nm = wb.createName();
    nm.setNameName("DA6_LEO_WBS_Name");
    nm.setRefersToFormula("33");
    nm = wb.createName();
    nm.setNameName("A1_");
    nm.setRefersToFormula("22");
    nm = wb.createName();
    nm.setNameName("_A1");
    nm.setRefersToFormula("11");
    nm = wb.createName();
    nm.setNameName("A_1");
    nm.setRefersToFormula("44");
    nm = wb.createName();
    nm.setNameName("A_1_");
    nm.setRefersToFormula("44");
    HSSFRow row = sheet.createRow(0);
    HSSFCell cell = row.createCell(0);
    cell.setCellFormula("DA6_LEO_WBS_Number*2");
    assertEquals("DA6_LEO_WBS_Number*2", cell.getCellFormula());
    cell.setCellFormula("(A1_*_A1+A_1)/A_1_");
    assertEquals("(A1_*_A1+A_1)/A_1_", cell.getCellFormula());
    cell.setCellFormula("INDEX(DA6_LEO_WBS_Name,MATCH($A3,DA6_LEO_WBS_Number,0))");
    assertEquals("INDEX(DA6_LEO_WBS_Name,MATCH($A3,DA6_LEO_WBS_Number,0))", cell.getCellFormula());
    wb.close();
}
Also used : TestHSSFName(org.apache.poi.hssf.usermodel.TestHSSFName) HSSFName(org.apache.poi.hssf.usermodel.HSSFName) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 3 with HSSFName

use of org.apache.poi.hssf.usermodel.HSSFName in project poi by apache.

the class TestNameRecord method testFormulaGeneral.

public void testFormulaGeneral() throws IOException {
    // perhaps this testcase belongs on TestHSSFName
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFName name = wb.createName();
    wb.createSheet("Sheet1");
    name.setNameName("test");
    name.setRefersToFormula("Sheet1!A1+Sheet1!A2");
    assertEquals("Sheet1!A1+Sheet1!A2", name.getRefersToFormula());
    name.setRefersToFormula("5*6");
    assertEquals("5*6", name.getRefersToFormula());
    wb.close();
}
Also used : HSSFName(org.apache.poi.hssf.usermodel.HSSFName) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 4 with HSSFName

use of org.apache.poi.hssf.usermodel.HSSFName in project poi by apache.

the class TestAreaReference method testDiscontinousReference.

public void testDiscontinousReference() throws Exception {
    InputStream is = HSSFTestDataSamples.openSampleFileStream("44167.xls");
    HSSFWorkbook wb = new HSSFWorkbook(is);
    InternalWorkbook workbook = TestHSSFWorkbook.getInternalWorkbook(wb);
    HSSFEvaluationWorkbook eb = HSSFEvaluationWorkbook.create(wb);
    assertEquals(1, wb.getNumberOfNames());
    String sheetName = "Tabelle1";
    String rawRefA = "$C$10:$C$14";
    String rawRefB = "$C$16:$C$18";
    String refA = sheetName + "!" + rawRefA;
    String refB = sheetName + "!" + rawRefB;
    String ref = refA + "," + refB;
    // Check the low level record
    NameRecord nr = workbook.getNameRecord(0);
    assertNotNull(nr);
    assertEquals("test", nr.getNameText());
    Ptg[] def = nr.getNameDefinition();
    assertEquals(4, def.length);
    MemFuncPtg ptgA = (MemFuncPtg) def[0];
    Area3DPtg ptgB = (Area3DPtg) def[1];
    Area3DPtg ptgC = (Area3DPtg) def[2];
    UnionPtg ptgD = (UnionPtg) def[3];
    assertEquals("", ptgA.toFormulaString());
    assertEquals(refA, ptgB.toFormulaString(eb));
    assertEquals(refB, ptgC.toFormulaString(eb));
    assertEquals(",", ptgD.toFormulaString());
    assertEquals(ref, HSSFFormulaParser.toFormulaString(wb, nr.getNameDefinition()));
    // Check the high level definition
    int idx = wb.getNameIndex("test");
    assertEquals(0, idx);
    HSSFName aNamedCell = wb.getNameAt(idx);
    // Should have 2 references
    assertEquals(ref, aNamedCell.getRefersToFormula());
    // Check the parsing of the reference into cells
    assertFalse(AreaReference.isContiguous(aNamedCell.getRefersToFormula()));
    AreaReference[] arefs = AreaReference.generateContiguous(aNamedCell.getRefersToFormula());
    assertEquals(2, arefs.length);
    assertEquals(refA, arefs[0].formatAsString());
    assertEquals(refB, arefs[1].formatAsString());
    for (AreaReference ar : arefs) {
        confirmResolveCellRef(wb, ar.getFirstCell());
        confirmResolveCellRef(wb, ar.getLastCell());
    }
}
Also used : Ptg(org.apache.poi.ss.formula.ptg.Ptg) UnionPtg(org.apache.poi.ss.formula.ptg.UnionPtg) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) MemFuncPtg(org.apache.poi.ss.formula.ptg.MemFuncPtg) AreaReference(org.apache.poi.ss.util.AreaReference) MemFuncPtg(org.apache.poi.ss.formula.ptg.MemFuncPtg) InputStream(java.io.InputStream) TestHSSFWorkbook(org.apache.poi.hssf.usermodel.TestHSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFEvaluationWorkbook(org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook) InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook) HSSFName(org.apache.poi.hssf.usermodel.HSSFName) NameRecord(org.apache.poi.hssf.record.NameRecord) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) UnionPtg(org.apache.poi.ss.formula.ptg.UnionPtg)

Example 5 with HSSFName

use of org.apache.poi.hssf.usermodel.HSSFName in project poi by apache.

the class TestIndirect method createWBA.

private static HSSFWorkbook createWBA() {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet("Sheet1");
    HSSFSheet sheet2 = wb.createSheet("Sheet2");
    HSSFSheet sheet3 = wb.createSheet("John's sales");
    createDataRow(sheet1, 0, 11, 12, 13, 14);
    createDataRow(sheet1, 1, 21, 22, 23, 24);
    createDataRow(sheet1, 2, 31, 32, 33, 34);
    createDataRow(sheet2, 0, 50, 55, 60, 65);
    createDataRow(sheet2, 1, 51, 56, 61, 66);
    createDataRow(sheet2, 2, 52, 57, 62, 67);
    createDataRow(sheet3, 0, 30, 31, 32);
    createDataRow(sheet3, 1, 33, 34, 35);
    HSSFName name1 = wb.createName();
    name1.setNameName("sales1");
    name1.setRefersToFormula("Sheet1!A1:D1");
    HSSFName name2 = wb.createName();
    name2.setNameName("sales2");
    name2.setRefersToFormula("Sheet2!B1:C3");
    HSSFRow row = sheet1.createRow(3);
    //A4
    row.createCell(0).setCellValue("sales1");
    //B4
    row.createCell(1).setCellValue("sales2");
    return wb;
}
Also used : HSSFName(org.apache.poi.hssf.usermodel.HSSFName) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Aggregations

HSSFName (org.apache.poi.hssf.usermodel.HSSFName)9 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)9 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)5 TestHSSFName (org.apache.poi.hssf.usermodel.TestHSSFName)4 Test (org.junit.Test)4 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)3 HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)3 Area3DPtg (org.apache.poi.ss.formula.ptg.Area3DPtg)3 MemFuncPtg (org.apache.poi.ss.formula.ptg.MemFuncPtg)3 Ptg (org.apache.poi.ss.formula.ptg.Ptg)3 NameRecord (org.apache.poi.hssf.record.NameRecord)2 FormulaParseException (org.apache.poi.ss.formula.FormulaParseException)2 AbstractFunctionPtg (org.apache.poi.ss.formula.ptg.AbstractFunctionPtg)2 AddPtg (org.apache.poi.ss.formula.ptg.AddPtg)2 AreaPtg (org.apache.poi.ss.formula.ptg.AreaPtg)2 ArrayPtg (org.apache.poi.ss.formula.ptg.ArrayPtg)2 AttrPtg (org.apache.poi.ss.formula.ptg.AttrPtg)2 BoolPtg (org.apache.poi.ss.formula.ptg.BoolPtg)2 ConcatPtg (org.apache.poi.ss.formula.ptg.ConcatPtg)2 DividePtg (org.apache.poi.ss.formula.ptg.DividePtg)2