Search in sources :

Example 6 with HSSFEvaluationWorkbook

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

the class TestEventWorkbookBuilder method testFormulas.

public void testFormulas() {
    FormulaRecord[] fRecs = mockListen.getFormulaRecords();
    // Check our formula records
    assertEquals(6, fRecs.length);
    InternalWorkbook stubWB = listener.getStubWorkbook();
    assertNotNull(stubWB);
    HSSFWorkbook stubHSSF = listener.getStubHSSFWorkbook();
    assertNotNull(stubHSSF);
    // Check these stubs have the right stuff on them
    assertEquals("Sheet1", stubWB.getSheetName(0));
    assertEquals("Sheet1", stubHSSF.getSheetName(0));
    assertEquals("S2", stubWB.getSheetName(1));
    assertEquals("S2", stubHSSF.getSheetName(1));
    assertEquals("Sh3", stubWB.getSheetName(2));
    assertEquals("Sh3", stubHSSF.getSheetName(2));
    // Check we can get the formula without breaking
    for (FormulaRecord fRec : fRecs) {
        HSSFFormulaParser.toFormulaString(stubHSSF, fRec.getParsedExpression());
    }
    // Peer into just one formula, and check that
    //  all the ptgs give back the right things
    Ptg[] ptgs = fRecs[0].getParsedExpression();
    assertEquals(1, ptgs.length);
    assertTrue(ptgs[0] instanceof Ref3DPtg);
    Ref3DPtg ptg = (Ref3DPtg) ptgs[0];
    HSSFEvaluationWorkbook book = HSSFEvaluationWorkbook.create(stubHSSF);
    assertEquals("Sheet1!A1", ptg.toFormulaString(book));
    // Now check we get the right formula back for
    //  a few sample ones
    FormulaRecord fr;
    // Sheet 1 A2 is on same sheet
    fr = fRecs[0];
    assertEquals(1, fr.getRow());
    assertEquals(0, fr.getColumn());
    assertEquals("Sheet1!A1", HSSFFormulaParser.toFormulaString(stubHSSF, fr.getParsedExpression()));
    // Sheet 1 A5 is to another sheet
    fr = fRecs[3];
    assertEquals(4, fr.getRow());
    assertEquals(0, fr.getColumn());
    assertEquals("'S2'!A1", HSSFFormulaParser.toFormulaString(stubHSSF, fr.getParsedExpression()));
    // Sheet 1 A7 is to another sheet, range
    fr = fRecs[5];
    assertEquals(6, fr.getRow());
    assertEquals(0, fr.getColumn());
    assertEquals("SUM(Sh3!A1:A4)", HSSFFormulaParser.toFormulaString(stubHSSF, fr.getParsedExpression()));
    // Now, load via Usermodel and re-check
    HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("3dFormulas.xls");
    assertEquals("Sheet1!A1", wb.getSheetAt(0).getRow(1).getCell(0).getCellFormula());
    assertEquals("SUM(Sh3!A1:A4)", wb.getSheetAt(0).getRow(6).getCell(0).getCellFormula());
}
Also used : Ptg(org.apache.poi.ss.formula.ptg.Ptg) Ref3DPtg(org.apache.poi.ss.formula.ptg.Ref3DPtg) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) Ref3DPtg(org.apache.poi.ss.formula.ptg.Ref3DPtg) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFEvaluationWorkbook(org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook) InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook)

Example 7 with HSSFEvaluationWorkbook

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

the class TestArea3DPtg method testToFormulaString.

/**
	 * confirms that sheet names get properly escaped
	 */
public void testToFormulaString() {
    Area3DPtg target = new Area3DPtg("A1:B1", (short) 0);
    String sheetName = "my sheet";
    HSSFWorkbook wb = createWorkbookWithSheet(sheetName);
    HSSFEvaluationWorkbook book = HSSFEvaluationWorkbook.create(wb);
    assertEquals("'my sheet'!A1:B1", target.toFormulaString(book));
    wb.setSheetName(0, "Sheet1");
    assertEquals("Sheet1!A1:B1", target.toFormulaString(book));
    wb.setSheetName(0, "C64");
    assertEquals("'C64'!A1:B1", target.toFormulaString(book));
}
Also used : HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFEvaluationWorkbook(org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook)

Example 8 with HSSFEvaluationWorkbook

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

the class TestDec2Hex method createContext.

private OperationEvaluationContext createContext() {
    HSSFWorkbook wb = new HSSFWorkbook();
    Sheet sheet = wb.createSheet();
    Row row = sheet.createRow(0);
    Cell cell = row.createCell(0);
    cell.setCellValue("123.43");
    cell = row.createCell(1);
    cell.setCellValue("8");
    cell = row.createCell(2);
    cell.setCellValue("-8");
    HSSFEvaluationWorkbook workbook = HSSFEvaluationWorkbook.create(wb);
    WorkbookEvaluator workbookEvaluator = new WorkbookEvaluator(workbook, new IStabilityClassifier() {

        @Override
        public boolean isCellFinal(int sheetIndex, int rowIndex, int columnIndex) {
            return true;
        }
    }, null);
    OperationEvaluationContext ctx = new OperationEvaluationContext(workbookEvaluator, workbook, 0, 0, 0, null);
    return ctx;
}
Also used : IStabilityClassifier(org.apache.poi.ss.formula.IStabilityClassifier) WorkbookEvaluator(org.apache.poi.ss.formula.WorkbookEvaluator) OperationEvaluationContext(org.apache.poi.ss.formula.OperationEvaluationContext) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFEvaluationWorkbook(org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook)

Example 9 with HSSFEvaluationWorkbook

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

the class TestRef3DPtg method testToFormulaString.

public void testToFormulaString() {
    Ref3DPtg target = new Ref3DPtg("A1", (short) 0);
    HSSFWorkbook wb = createWorkbookWithSheet("my sheet");
    HSSFEvaluationWorkbook book = HSSFEvaluationWorkbook.create(wb);
    assertEquals("'my sheet'!A1", target.toFormulaString(book));
    wb.setSheetName(0, "ProfitAndLoss");
    assertEquals("ProfitAndLoss!A1", target.toFormulaString(book));
    wb.setSheetName(0, "profit+loss");
    assertEquals("'profit+loss'!A1", target.toFormulaString(book));
}
Also used : HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFEvaluationWorkbook(org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook)

Aggregations

HSSFEvaluationWorkbook (org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook)9 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)9 IStabilityClassifier (org.apache.poi.ss.formula.IStabilityClassifier)4 OperationEvaluationContext (org.apache.poi.ss.formula.OperationEvaluationContext)4 WorkbookEvaluator (org.apache.poi.ss.formula.WorkbookEvaluator)4 Ptg (org.apache.poi.ss.formula.ptg.Ptg)3 InternalWorkbook (org.apache.poi.hssf.model.InternalWorkbook)2 Area3DPtg (org.apache.poi.ss.formula.ptg.Area3DPtg)2 MemFuncPtg (org.apache.poi.ss.formula.ptg.MemFuncPtg)2 Ref3DPtg (org.apache.poi.ss.formula.ptg.Ref3DPtg)2 UnionPtg (org.apache.poi.ss.formula.ptg.UnionPtg)2 InputStream (java.io.InputStream)1 FormulaRecord (org.apache.poi.hssf.record.FormulaRecord)1 NameRecord (org.apache.poi.hssf.record.NameRecord)1 UnicodeString (org.apache.poi.hssf.record.common.UnicodeString)1 HSSFName (org.apache.poi.hssf.usermodel.HSSFName)1 TestHSSFWorkbook (org.apache.poi.hssf.usermodel.TestHSSFWorkbook)1 AbstractFunctionPtg (org.apache.poi.ss.formula.ptg.AbstractFunctionPtg)1 AddPtg (org.apache.poi.ss.formula.ptg.AddPtg)1 AreaPtg (org.apache.poi.ss.formula.ptg.AreaPtg)1