Search in sources :

Example 1 with AreaPtg

use of org.apache.poi.ss.formula.ptg.AreaPtg 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 2 with AreaPtg

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

the class CFRecordsAggregate method shiftRange.

private static CellRangeAddress shiftRange(FormulaShifter shifter, CellRangeAddress cra, int currentExternSheetIx) {
    // FormulaShifter works well in terms of Ptgs - so convert CellRangeAddress to AreaPtg (and back) here
    AreaPtg aptg = new AreaPtg(cra.getFirstRow(), cra.getLastRow(), cra.getFirstColumn(), cra.getLastColumn(), false, false, false, false);
    Ptg[] ptgs = { aptg };
    if (!shifter.adjustFormula(ptgs, currentExternSheetIx)) {
        return cra;
    }
    Ptg ptg0 = ptgs[0];
    if (ptg0 instanceof AreaPtg) {
        AreaPtg bptg = (AreaPtg) ptg0;
        return new CellRangeAddress(bptg.getFirstRow(), bptg.getLastRow(), bptg.getFirstColumn(), bptg.getLastColumn());
    }
    if (ptg0 instanceof AreaErrPtg) {
        return null;
    }
    throw new IllegalStateException("Unexpected shifted ptg class (" + ptg0.getClass().getName() + ")");
}
Also used : Ptg(org.apache.poi.ss.formula.ptg.Ptg) AreaErrPtg(org.apache.poi.ss.formula.ptg.AreaErrPtg) AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg) AreaErrPtg(org.apache.poi.ss.formula.ptg.AreaErrPtg) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg)

Example 3 with AreaPtg

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

the class TestFormulaShifter method testCopyAreasSourceRowsAbsAbs.

@Test
public void testCopyAreasSourceRowsAbsAbs() {
    // aptg is part of a formula in a cell that was just copied to another row
    // aptg row references should be updated by the difference in rows that the cell was copied
    // No other references besides the cells that were involved in the copy need to be updated
    // this makes the row copy significantly different from the row shift, where all references
    // in the workbook need to track the row shift
    // all these operations are on an area ref spanning rows 10 to 20
    final AreaPtg aptg = createAreaPtg(10, 20, false, false);
    //AbsFirstRow AbsLastRow references should't change when copied to a different row
    confirmAreaCopy(aptg, 0, 30, 20, 10, 20, false);
    confirmAreaCopy(aptg, 15, 25, -15, 10, 20, false);
}
Also used : AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg) Test(org.junit.Test)

Example 4 with AreaPtg

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

the class TestFormulaShifter method testShiftAreasSourceRows.

// Note - the expected result row coordinates here were determined/verified
// in Excel 2007 by manually testing.
/**
     * Tests what happens to area refs when a range of rows from inside, or overlapping are
     * moved
     */
@Test
public void testShiftAreasSourceRows() {
    // all these operations are on an area ref spanning rows 10 to 20
    AreaPtg aptg = createAreaPtg(10, 20);
    confirmAreaShift(aptg, 9, 21, 20, 30, 40);
    confirmAreaShift(aptg, 10, 21, 20, 30, 40);
    confirmAreaShift(aptg, 9, 20, 20, 30, 40);
    // simple expansion of top
    confirmAreaShift(aptg, 8, 11, -3, 7, 20);
    // rows containing area top being shifted down:
    confirmAreaShift(aptg, 8, 11, 3, 13, 20);
    confirmAreaShift(aptg, 8, 11, 7, 17, 20);
    confirmAreaShift(aptg, 8, 11, 8, 18, 20);
    // note behaviour changes here
    confirmAreaShift(aptg, 8, 11, 9, 12, 20);
    confirmAreaShift(aptg, 8, 11, 10, 12, 21);
    confirmAreaShift(aptg, 8, 11, 12, 12, 23);
    // ignored
    confirmAreaShift(aptg, 8, 11, 13, 10, 20);
    // rows from within being moved:
    // stay within - no change
    confirmAreaShift(aptg, 12, 16, 3, 10, 20);
    // move completely out - no change
    confirmAreaShift(aptg, 11, 19, 20, 10, 20);
    // moved exactly to top - no change
    confirmAreaShift(aptg, 16, 17, -6, 10, 20);
    // truncation at top
    confirmAreaShift(aptg, 16, 17, -7, 11, 20);
    // moved exactly to bottom - no change
    confirmAreaShift(aptg, 12, 16, 4, 10, 20);
    // truncation at bottom
    confirmAreaShift(aptg, 12, 16, 6, 10, 17);
    // rows containing area bottom being shifted up:
    // simple contraction at bottom
    confirmAreaShift(aptg, 18, 22, -1, 10, 19);
    // simple contraction at bottom
    confirmAreaShift(aptg, 18, 22, -7, 10, 13);
    // top calculated differently here
    confirmAreaShift(aptg, 18, 22, -8, 10, 17);
    confirmAreaShift(aptg, 18, 22, -9, 9, 17);
    // no change because range would be turned inside out
    confirmAreaShift(aptg, 18, 22, -15, 10, 20);
    // dest truncates top (even though src is from inside range)
    confirmAreaShift(aptg, 15, 19, -7, 13, 20);
    // complex: src encloses bottom, dest encloses top
    confirmAreaShift(aptg, 19, 23, -12, 7, 18);
    // simple expansion at bottom
    confirmAreaShift(aptg, 18, 22, 5, 10, 25);
}
Also used : AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg) Test(org.junit.Test)

Example 5 with AreaPtg

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

the class TestFormulaShifter method confirmAreaShift.

private static void confirmAreaShift(AreaPtg aptg, int firstRowMoved, int lastRowMoved, int numberRowsMoved, int expectedAreaFirstRow, int expectedAreaLastRow) {
    FormulaShifter fs = FormulaShifter.createForRowShift(0, "", firstRowMoved, lastRowMoved, numberRowsMoved, SpreadsheetVersion.EXCEL2007);
    boolean expectedChanged = aptg.getFirstRow() != expectedAreaFirstRow || aptg.getLastRow() != expectedAreaLastRow;
    // clone so we can re-use aptg in calling method
    AreaPtg copyPtg = (AreaPtg) aptg.copy();
    Ptg[] ptgs = { copyPtg };
    boolean actualChanged = fs.adjustFormula(ptgs, 0);
    if (expectedAreaFirstRow < 0) {
        assertEquals(AreaErrPtg.class, ptgs[0].getClass());
        return;
    }
    assertEquals(expectedChanged, actualChanged);
    // expected to change in place (although this is not a strict requirement)
    assertEquals(copyPtg, ptgs[0]);
    assertEquals(expectedAreaFirstRow, copyPtg.getFirstRow());
    assertEquals(expectedAreaLastRow, copyPtg.getLastRow());
}
Also used : Ptg(org.apache.poi.ss.formula.ptg.Ptg) Ref3DPtg(org.apache.poi.ss.formula.ptg.Ref3DPtg) AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg) AreaErrPtg(org.apache.poi.ss.formula.ptg.AreaErrPtg) AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg)

Aggregations

AreaPtg (org.apache.poi.ss.formula.ptg.AreaPtg)15 Test (org.junit.Test)7 Ptg (org.apache.poi.ss.formula.ptg.Ptg)5 AreaErrPtg (org.apache.poi.ss.formula.ptg.AreaErrPtg)4 Ref3DPtg (org.apache.poi.ss.formula.ptg.Ref3DPtg)3 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)3 RefPtg (org.apache.poi.ss.formula.ptg.RefPtg)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 AssertionFailedError (junit.framework.AssertionFailedError)1 FormulaRecordAggregate (org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate)1 Area3DPtg (org.apache.poi.ss.formula.ptg.Area3DPtg)1 FuncPtg (org.apache.poi.ss.formula.ptg.FuncPtg)1 FuncVarPtg (org.apache.poi.ss.formula.ptg.FuncVarPtg)1 LittleEndianInput (org.apache.poi.util.LittleEndianInput)1 LittleEndianInputStream (org.apache.poi.util.LittleEndianInputStream)1