Search in sources :

Example 1 with Ref3DPtg

use of org.apache.poi.ss.formula.ptg.Ref3DPtg in project poi by apache.

the class InternalWorkbook method cloneFilter.

public NameRecord cloneFilter(int filterDbNameIndex, int newSheetIndex) {
    NameRecord origNameRecord = getNameRecord(filterDbNameIndex);
    // copy original formula but adjust 3D refs to the new external sheet index
    int newExtSheetIx = checkExternSheet(newSheetIndex);
    Ptg[] ptgs = origNameRecord.getNameDefinition();
    for (int i = 0; i < ptgs.length; i++) {
        Ptg ptg = ptgs[i];
        if (ptg instanceof Area3DPtg) {
            Area3DPtg a3p = (Area3DPtg) ((OperandPtg) ptg).copy();
            a3p.setExternSheetIndex(newExtSheetIx);
            ptgs[i] = a3p;
        } else if (ptg instanceof Ref3DPtg) {
            Ref3DPtg r3p = (Ref3DPtg) ((OperandPtg) ptg).copy();
            r3p.setExternSheetIndex(newExtSheetIx);
            ptgs[i] = r3p;
        }
    }
    NameRecord newNameRecord = createBuiltInName(NameRecord.BUILTIN_FILTER_DB, newSheetIndex + 1);
    newNameRecord.setNameDefinition(ptgs);
    newNameRecord.setHidden(true);
    return newNameRecord;
}
Also used : OperandPtg(org.apache.poi.ss.formula.ptg.OperandPtg) NameXPtg(org.apache.poi.ss.formula.ptg.NameXPtg) Ref3DPtg(org.apache.poi.ss.formula.ptg.Ref3DPtg) Ptg(org.apache.poi.ss.formula.ptg.Ptg) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) OperandPtg(org.apache.poi.ss.formula.ptg.OperandPtg) NameRecord(org.apache.poi.hssf.record.NameRecord) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) Ref3DPtg(org.apache.poi.ss.formula.ptg.Ref3DPtg)

Example 2 with Ref3DPtg

use of org.apache.poi.ss.formula.ptg.Ref3DPtg in project poi by apache.

the class OperationEvaluationContext method getExternalNameXEval.

private ValueEval getExternalNameXEval(ExternalName externName, String workbookName) {
    try {
        // Fetch the workbook this refers to, and the name as defined with that
        WorkbookEvaluator refWorkbookEvaluator = _bookEvaluator.getOtherWorkbookEvaluator(workbookName);
        EvaluationName evaluationName = refWorkbookEvaluator.getName(externName.getName(), externName.getIx() - 1);
        if (evaluationName != null && evaluationName.hasFormula()) {
            if (evaluationName.getNameDefinition().length > 1) {
                throw new RuntimeException("Complex name formulas not supported yet");
            }
            // Need to evaluate the reference in the context of the other book
            OperationEvaluationContext refWorkbookContext = new OperationEvaluationContext(refWorkbookEvaluator, refWorkbookEvaluator.getWorkbook(), -1, -1, -1, _tracker);
            Ptg ptg = evaluationName.getNameDefinition()[0];
            if (ptg instanceof Ref3DPtg) {
                Ref3DPtg ref3D = (Ref3DPtg) ptg;
                return refWorkbookContext.getRef3DEval(ref3D);
            } else if (ptg instanceof Ref3DPxg) {
                Ref3DPxg ref3D = (Ref3DPxg) ptg;
                return refWorkbookContext.getRef3DEval(ref3D);
            } else if (ptg instanceof Area3DPtg) {
                Area3DPtg area3D = (Area3DPtg) ptg;
                return refWorkbookContext.getArea3DEval(area3D);
            } else if (ptg instanceof Area3DPxg) {
                Area3DPxg area3D = (Area3DPxg) ptg;
                return refWorkbookContext.getArea3DEval(area3D);
            }
        }
        return ErrorEval.REF_INVALID;
    } catch (WorkbookNotFoundException wnfe) {
        return ErrorEval.REF_INVALID;
    }
}
Also used : Area3DPxg(org.apache.poi.ss.formula.ptg.Area3DPxg) NameXPtg(org.apache.poi.ss.formula.ptg.NameXPtg) Ptg(org.apache.poi.ss.formula.ptg.Ptg) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) Ref3DPtg(org.apache.poi.ss.formula.ptg.Ref3DPtg) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) Ref3DPxg(org.apache.poi.ss.formula.ptg.Ref3DPxg) WorkbookNotFoundException(org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment.WorkbookNotFoundException) Ref3DPtg(org.apache.poi.ss.formula.ptg.Ref3DPtg)

Example 3 with Ref3DPtg

use of org.apache.poi.ss.formula.ptg.Ref3DPtg in project poi by apache.

the class NameRecord method getExternSheetNumber.

/** gets the extern sheet number
	 * @return extern sheet index
	 */
public int getExternSheetNumber() {
    Ptg[] tokens = field_13_name_definition.getTokens();
    if (tokens.length == 0) {
        return 0;
    }
    Ptg ptg = tokens[0];
    if (ptg.getClass() == Area3DPtg.class) {
        return ((Area3DPtg) ptg).getExternSheetIndex();
    }
    if (ptg.getClass() == Ref3DPtg.class) {
        return ((Ref3DPtg) ptg).getExternSheetIndex();
    }
    return 0;
}
Also used : Ptg(org.apache.poi.ss.formula.ptg.Ptg) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) Ref3DPtg(org.apache.poi.ss.formula.ptg.Ref3DPtg) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) Ref3DPtg(org.apache.poi.ss.formula.ptg.Ref3DPtg)

Example 4 with Ref3DPtg

use of org.apache.poi.ss.formula.ptg.Ref3DPtg in project poi by apache.

the class EmbeddedObjectRefSubRecord method readRefPtg.

private static Ptg readRefPtg(byte[] formulaRawBytes) {
    LittleEndianInput in = new LittleEndianInputStream(new ByteArrayInputStream(formulaRawBytes));
    byte ptgSid = in.readByte();
    switch(ptgSid) {
        case AreaPtg.sid:
            return new AreaPtg(in);
        case Area3DPtg.sid:
            return new Area3DPtg(in);
        case RefPtg.sid:
            return new RefPtg(in);
        case Ref3DPtg.sid:
            return new Ref3DPtg(in);
    }
    return null;
}
Also used : LittleEndianInput(org.apache.poi.util.LittleEndianInput) ByteArrayInputStream(java.io.ByteArrayInputStream) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) LittleEndianInputStream(org.apache.poi.util.LittleEndianInputStream) AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg) RefPtg(org.apache.poi.ss.formula.ptg.RefPtg) Ref3DPtg(org.apache.poi.ss.formula.ptg.Ref3DPtg)

Example 5 with Ref3DPtg

use of org.apache.poi.ss.formula.ptg.Ref3DPtg 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)

Aggregations

Ref3DPtg (org.apache.poi.ss.formula.ptg.Ref3DPtg)10 Ptg (org.apache.poi.ss.formula.ptg.Ptg)7 Area3DPtg (org.apache.poi.ss.formula.ptg.Area3DPtg)6 AreaPtg (org.apache.poi.ss.formula.ptg.AreaPtg)4 NameXPtg (org.apache.poi.ss.formula.ptg.NameXPtg)3 AreaErrPtg (org.apache.poi.ss.formula.ptg.AreaErrPtg)2 NamePtg (org.apache.poi.ss.formula.ptg.NamePtg)2 RefPtg (org.apache.poi.ss.formula.ptg.RefPtg)2 CellReference (org.apache.poi.ss.util.CellReference)2 Test (org.junit.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InternalWorkbook (org.apache.poi.hssf.model.InternalWorkbook)1 FormulaRecord (org.apache.poi.hssf.record.FormulaRecord)1 NameRecord (org.apache.poi.hssf.record.NameRecord)1 HSSFEvaluationWorkbook (org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook)1 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)1 WorkbookNotFoundException (org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment.WorkbookNotFoundException)1 AbstractFunctionPtg (org.apache.poi.ss.formula.ptg.AbstractFunctionPtg)1 AddPtg (org.apache.poi.ss.formula.ptg.AddPtg)1 Area3DPxg (org.apache.poi.ss.formula.ptg.Area3DPxg)1