Exemplo n.º 1
0
 private AreaPtg CloneArea(AreaPtg a)
 {
     return(new AreaPtg(
                a.FirstRow, a.LastRow, a.FirstColumn, a.LastColumn,
                a.IsFirstRowRelative, a.IsLastRowRelative, a.IsFirstColRelative, a.IsLastColRelative
                ));
 }
Exemplo n.º 2
0
 private void validateReference(bool abs, AreaPtg ref1)
 {
     Assert.AreEqual(abs, !ref1.IsFirstColRelative, "First column reference is not " + (abs ? "absolute" : "relative"));
     Assert.AreEqual(abs, !ref1.IsLastColRelative, "Last column reference is not " + (abs ? "absolute" : "relative"));
     Assert.AreEqual(abs, !ref1.IsFirstRowRelative, "First row reference is not " + (abs ? "absolute" : "relative"));
     Assert.AreEqual(abs, !ref1.IsLastRowRelative, "Last row reference is not " + (abs ? "absolute" : "relative"));
 }
Exemplo n.º 3
0
        public void Test44410()
        {
            HSSFWorkbook wb = HSSFTestDataSamples.OpenSampleWorkbook("SingleLetterRanges.xls");

            NPOI.SS.UserModel.ISheet sheet = wb.GetSheetAt(0);

            HSSFFormulaEvaluator eva = new HSSFFormulaEvaluator(wb);

            // =index(C:C,2,1) -> 2
            IRow rowIDX = sheet.GetRow(3);
            // =sum(C:C) -> 6
            IRow rowSUM = sheet.GetRow(4);
            // =sum(C:D) -> 66
            IRow rowSUM2D = sheet.GetRow(5);

            // Test the sum
            ICell cellSUM = rowSUM.GetCell(0);

            FormulaRecordAggregate frec = (FormulaRecordAggregate)((HSSFCell)cellSUM).CellValueRecord;

            Ptg[] ops = frec.FormulaRecord.ParsedExpression;
            Assert.AreEqual(2, ops.Length);
            Assert.AreEqual(typeof(AreaPtg), ops[0].GetType());
            Assert.AreEqual(typeof(FuncVarPtg), ops[1].GetType());

            // Actually stored as C1 to C65536
            // (last row is -1 === 65535)
            AreaPtg ptg = (AreaPtg)ops[0];

            Assert.AreEqual(2, ptg.FirstColumn);
            Assert.AreEqual(2, ptg.LastColumn);
            Assert.AreEqual(0, ptg.FirstRow);
            Assert.AreEqual(65535, ptg.LastRow);
            Assert.AreEqual("C:C", ptg.ToFormulaString());

            // Will show as C:C, but won't know how many
            // rows it covers as we don't have the sheet
            // to hand when turning the Ptgs into a string
            Assert.AreEqual("SUM(C:C)", cellSUM.CellFormula);

            // But the evaluator knows the sheet, so it
            // can do it properly
            Assert.AreEqual(6, eva.Evaluate(cellSUM).NumberValue, 0);

            // Test the index
            // Again, the formula string will be right but
            // lacking row count, Evaluated will be right
            ICell cellIDX = rowIDX.GetCell(0);

            Assert.AreEqual("INDEX(C:C,2,1)", cellIDX.CellFormula);
            Assert.AreEqual(2, eva.Evaluate(cellIDX).NumberValue, 0);

            // Across two colums
            ICell cellSUM2D = rowSUM2D.GetCell(0);

            Assert.AreEqual("SUM(C:D)", cellSUM2D.CellFormula);
            Assert.AreEqual(66, eva.Evaluate(cellSUM2D).NumberValue, 0);

            wb.Close();
        }
Exemplo n.º 4
0
        public void Test_47701()
        {
            byte[] data = HexRead.ReadFromString(
                            "15, 00, 12, 00, 12, 00, 02, 00, 11, 20, " +
                            "00, 00, 00, 00, 80, 3D, 03, 05, 00, 00, " +
                            "00, 00, 0C, 00, 14, 00, 00, 00, 00, 00, " +
                            "00, 00, 00, 00, 00, 00, 01, 00, 0A, 00, " +
                            "00, 00, 10, 00, 01, 00, 13, 00, EE, 1F, " +
                            "10, 00, 09, 00, 40, 9F, 74, 01, 25, 09, " +
                            "00, 0C, 00, 07, 00, 07, 00, 07, 04, 00, " +
                            "00, 00, 08, 00, 00, 00");
            RecordInputStream in1 = TestcaseRecordInputStream.Create(ObjRecord.sid, data);
            // check read OK
            ObjRecord record = new ObjRecord(in1);
            Assert.AreEqual(3, record.SubRecords.Count);
            SubRecord sr = record.SubRecords[(2)];
            Assert.IsTrue(sr is LbsDataSubRecord);
            LbsDataSubRecord lbs = (LbsDataSubRecord)sr;
            Assert.AreEqual(4, lbs.NumberOfItems);

            Assert.IsTrue(lbs.Formula is AreaPtg);
            AreaPtg ptg = (AreaPtg)lbs.Formula;
            CellRangeAddress range = new CellRangeAddress(
                    ptg.FirstRow, ptg.LastRow, ptg.FirstColumn, ptg.LastColumn);
            Assert.AreEqual("H10:H13", range.FormatAsString());

            // check that it re-Serializes to the same data
            byte[] ser = record.Serialize();
            TestcaseRecordInputStream.ConfirmRecordEncoding(ObjRecord.sid, data, ser);
        }
Exemplo n.º 5
0
        public void TestGetValue_bug44950()
        {
            // TODO - this Test probably isn't Testing much anymore
            AreaPtg    ptg = new AreaPtg("B2:D3");
            NumberEval one = new NumberEval(1);

            ValueEval[] values =
            {
                one,
                new NumberEval(2),
                new NumberEval(3),
                new NumberEval(4),
                new NumberEval(5),
                new NumberEval(6),
            };
            AreaEval ae = EvalFactory.CreateAreaEval(ptg, values);

            if (one == ae.GetAbsoluteValue(1, 2))
            {
                throw new AssertionException("Identified bug 44950 a");
            }
            Confirm(1, ae, 1, 1);
            Confirm(2, ae, 1, 2);
            Confirm(3, ae, 1, 3);
            Confirm(4, ae, 2, 1);
            Confirm(5, ae, 2, 2);
            Confirm(6, ae, 2, 3);
        }
Exemplo n.º 6
0
        public void TestGetValue_bug44950()
        {
            AreaPtg    ptg = new AreaPtg("B2:D3");
            NumberEval one = new NumberEval(1);

            ValueEval[] values =
            {
                one,
                new NumberEval(2),
                new NumberEval(3),
                new NumberEval(4),
                new NumberEval(5),
                new NumberEval(6),
            };
            AreaEval ae = EvalFactory.CreateAreaEval(ptg, values);

            if (one == ae.GetValueAt(1, 2))
            {
                throw new AssertFailedException("Identified bug 44950 a");
            }
            Confirm(1, ae, 1, 1);
            Confirm(2, ae, 1, 2);
            Confirm(3, ae, 1, 3);
            Confirm(4, ae, 2, 1);
            Confirm(5, ae, 2, 2);
            Confirm(6, ae, 2, 3);
        }
Exemplo n.º 7
0
        public void TestCopyAreasSourceRowsRelRel()
        {
            // all these operations are on an area ref spanning rows 10 to 20
            AreaPtg aptg = CreateAreaPtg(10, 20, true, true);

            ConfirmAreaCopy(aptg, 0, 30, 20, 30, 40, true);
            ConfirmAreaCopy(aptg, 15, 25, -15, -1, -1, true); //DeletedRef
        }
Exemplo n.º 8
0
        public void TestCopyAreasSourceRowsRelAbs()
        {
            // all these operations are on an area ref spanning rows 10 to 20
            AreaPtg aptg = CreateAreaPtg(10, 20, true, false);

            // Only first row should move
            ConfirmAreaCopy(aptg, 0, 30, 20, 20, 30, true);
            ConfirmAreaCopy(aptg, 15, 25, -15, -1, -1, true); //DeletedRef
        }
Exemplo n.º 9
0
        public void SetUp()
        {
            short firstRow = 5;
            short lastRow  = 13;
            short firstCol = 7;
            short lastCol  = 17;

            relative = new AreaPtg(firstRow, lastRow, firstCol, lastCol, true, true, true, true);
            absolute = new AreaPtg(firstRow, lastRow, firstCol, lastCol, false, false, false, false);
        }
Exemplo n.º 10
0
        private static void resetColumns(AreaPtg aptg)
        {
            int fc = aptg.FirstColumn;
            int lc = aptg.LastColumn;

            aptg.FirstColumn = (fc);
            aptg.LastColumn  = (lc);
            Assert.AreEqual(fc, aptg.FirstColumn);
            Assert.AreEqual(lc, aptg.LastColumn);
        }
Exemplo n.º 11
0
        public void TestSortTopLeftToBottomRight()
        {
            AreaPtg ptg = new AreaPtg("A$1:$B5");

            Assert.AreEqual("A$1:$B5", ptg.ToFormulaString());
            ptg.FirstColumn = (3);
            Assert.AreEqual("D$1:$B5", ptg.ToFormulaString(),
                            "Area Ptg should not implicitly re-sort itself (except during construction)");
            ptg.SortTopLeftToBottomRight();
            Assert.AreEqual("$B$1:D5", ptg.ToFormulaString(),
                            "Area Ptg should restore itself to top-left to lower-right order when explicitly asked");
        }
Exemplo n.º 12
0
        public void TestCopyAreasSourceRowsAbsRel()
        {
            // 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
            AreaPtg aptg = CreateAreaPtg(10, 20, false, true);

            // Only last row should move
            ConfirmAreaCopy(aptg, 0, 30, 20, 10, 40, true);
            ConfirmAreaCopy(aptg, 15, 25, -15, 5, 10, true); //sortTopLeftToBottomRight swapped firstRow and lastRow because firstRow is absolute
        }
Exemplo n.º 13
0
        public void TestShiftAreasDestRows()
        {
            // all these operations are on an area ref spanning rows 20 to 25
            AreaPtg aptg = CreateAreaPtg(20, 25);

            // no change because no overlap:
            ConfirmAreaShift(aptg, 5, 10, 9, 20, 25);
            ConfirmAreaShift(aptg, 5, 10, 21, 20, 25);

            ConfirmAreaShift(aptg, 11, 14, 10, 20, 25);

            ConfirmAreaShift(aptg, 7, 17, 10, -1, -1);  // Converted to DeletedAreaRef
            ConfirmAreaShift(aptg, 5, 15, 7, 23, 25);   // tRuncation at top
            ConfirmAreaShift(aptg, 13, 16, 10, 20, 22); // tRuncation at bottom
        }
Exemplo n.º 14
0
        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
            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);
        }
Exemplo n.º 15
0
        /**
         * Creates a non shared formula from the shared formula counterpart, i.e.
         * Converts the shared formula into the equivalent {@link org.apache.poi.ss.formula.ptg.Ptg} array that it would have,
         * were it not shared.
         *
         * @param ptgs parsed tokens of the shared formula
         * @param formulaRow
         * @param formulaColumn
         */
        public Ptg[] ConvertSharedFormulas(Ptg[] ptgs, int formulaRow, int formulaColumn)
        {

            Ptg[] newPtgStack = new Ptg[ptgs.Length];

            for (int k = 0; k < ptgs.Length; k++)
            {
                Ptg ptg = ptgs[k];
                byte originalOperandClass = unchecked((byte)-1);
                if (!ptg.IsBaseToken)
                {
                    originalOperandClass = ptg.PtgClass;
                }
                if (ptg is RefPtgBase)
                {
                    RefPtgBase refNPtg = (RefPtgBase)ptg;
                    ptg = new RefPtg(FixupRelativeRow(formulaRow, refNPtg.Row, refNPtg.IsRowRelative),
                                         FixupRelativeColumn(formulaColumn, refNPtg.Column, refNPtg.IsColRelative),
                                         refNPtg.IsRowRelative,
                                         refNPtg.IsColRelative);
                    ptg.PtgClass = (originalOperandClass);
                }
                else if (ptg is AreaPtgBase)
                {
                    AreaPtgBase areaNPtg = (AreaPtgBase)ptg;
                    ptg = new AreaPtg(FixupRelativeRow(formulaRow, areaNPtg.FirstRow, areaNPtg.IsFirstRowRelative),
                                    FixupRelativeRow(formulaRow, areaNPtg.LastRow, areaNPtg.IsLastRowRelative),
                                    FixupRelativeColumn(formulaColumn, areaNPtg.FirstColumn, areaNPtg.IsFirstColRelative),
                                    FixupRelativeColumn(formulaColumn, areaNPtg.LastColumn, areaNPtg.IsLastColRelative),
                                    areaNPtg.IsFirstRowRelative,
                                    areaNPtg.IsLastRowRelative,
                                    areaNPtg.IsFirstColRelative,
                                    areaNPtg.IsLastColRelative);
                    ptg.PtgClass = (originalOperandClass);
                }
                else if (ptg is OperandPtg)
                {
                    // Any subclass of OperandPtg is mutable, so it's safest to not share these instances.
                    ptg = ((OperandPtg)ptg).Copy();
                }
                else
                {
                    // all other Ptgs are immutable and can be shared
                }
                newPtgStack[k] = ptg;
            }
            return newPtgStack;
        }
Exemplo n.º 16
0
        /**
         * Creates a non shared formula from the shared formula counterpart, i.e.
         * Converts the shared formula into the equivalent {@link org.apache.poi.ss.formula.ptg.Ptg} array that it would have,
         * were it not shared.
         *
         * @param ptgs parsed tokens of the shared formula
         * @param formulaRow
         * @param formulaColumn
         */
        public Ptg[] ConvertSharedFormulas(Ptg[] ptgs, int formulaRow, int formulaColumn)
        {
            Ptg[] newPtgStack = new Ptg[ptgs.Length];

            for (int k = 0; k < ptgs.Length; k++)
            {
                Ptg  ptg = ptgs[k];
                byte originalOperandClass = unchecked ((byte)-1);
                if (!ptg.IsBaseToken)
                {
                    originalOperandClass = ptg.PtgClass;
                }
                if (ptg is RefPtgBase)
                {
                    RefPtgBase refNPtg = (RefPtgBase)ptg;
                    ptg = new RefPtg(FixupRelativeRow(formulaRow, refNPtg.Row, refNPtg.IsRowRelative),
                                     FixupRelativeColumn(formulaColumn, refNPtg.Column, refNPtg.IsColRelative),
                                     refNPtg.IsRowRelative,
                                     refNPtg.IsColRelative);
                    ptg.PtgClass = (originalOperandClass);
                }
                else if (ptg is AreaPtgBase)
                {
                    AreaPtgBase areaNPtg = (AreaPtgBase)ptg;
                    ptg = new AreaPtg(FixupRelativeRow(formulaRow, areaNPtg.FirstRow, areaNPtg.IsFirstRowRelative),
                                      FixupRelativeRow(formulaRow, areaNPtg.LastRow, areaNPtg.IsLastRowRelative),
                                      FixupRelativeColumn(formulaColumn, areaNPtg.FirstColumn, areaNPtg.IsFirstColRelative),
                                      FixupRelativeColumn(formulaColumn, areaNPtg.LastColumn, areaNPtg.IsLastColRelative),
                                      areaNPtg.IsFirstRowRelative,
                                      areaNPtg.IsLastRowRelative,
                                      areaNPtg.IsFirstColRelative,
                                      areaNPtg.IsLastColRelative);
                    ptg.PtgClass = (originalOperandClass);
                }
                else if (ptg is OperandPtg)
                {
                    // Any subclass of OperandPtg is mutable, so it's safest to not share these instances.
                    ptg = ((OperandPtg)ptg).Copy();
                }
                else
                {
                    // all other Ptgs are immutable and can be shared
                }
                newPtgStack[k] = ptg;
            }
            return(newPtgStack);
        }
Exemplo n.º 17
0
        /**
         * Creates a dummy AreaEval
         * @param values empty (<code>null</code>) entries in this array will be Converted to NumberEval.ZERO
         */
        public static AreaEval CreateAreaEval(AreaPtg areaPtg, ValueEval[] values)
        {
            int nCols     = areaPtg.LastColumn - areaPtg.FirstColumn + 1;
            int nRows     = areaPtg.LastRow - areaPtg.FirstRow + 1;
            int nExpected = nRows * nCols;

            if (values.Length != nExpected)
            {
                throw new SystemException("Expected " + nExpected + " values but got " + values.Length);
            }
            for (int i = 0; i < nExpected; i++)
            {
                if (values[i] == null)
                {
                    values[i] = NumberEval.ZERO;
                }
            }
            return(new MockAreaEval(areaPtg, values));
        }
Exemplo n.º 18
0
        private static String ShiftAllColumnsBy1(String formula)
        {
            int          letUsShiftColumn1By1Column = 1;
            HSSFWorkbook wb = null;

            Ptg[] ptgs = HSSFFormulaParser.Parse(formula, wb);
            for (int i = 0; i < ptgs.Length; i++)
            {
                Ptg ptg = ptgs[i];
                if (ptg is AreaPtg)
                {
                    AreaPtg aptg = (AreaPtg)ptg;
                    aptg.FirstColumn = ((short)(aptg.FirstColumn + letUsShiftColumn1By1Column));
                    aptg.LastColumn  = ((short)(aptg.LastColumn + letUsShiftColumn1By1Column));
                }
            }
            String newFormula = HSSFFormulaParser.ToFormulaString(wb, ptgs);

            return(newFormula);
        }
Exemplo n.º 19
0
        private static CellRangeAddress ShiftRange(FormulaShifter Shifter, CellRangeAddress cra, int currentExternSheetIx)
        {
            Ptg[] ptgs = new Ptg[1] {
                (Ptg) new AreaPtg(cra.FirstRow, cra.LastRow, cra.FirstColumn, cra.LastColumn, false, false, false, false)
            };
            if (!Shifter.AdjustFormula(ptgs, currentExternSheetIx))
            {
                return(cra);
            }
            Ptg ptg = ptgs[0];

            if (ptg is AreaPtg)
            {
                AreaPtg areaPtg = (AreaPtg)ptg;
                return(new CellRangeAddress(areaPtg.FirstRow, areaPtg.LastRow, areaPtg.FirstColumn, areaPtg.LastColumn));
            }
            if (ptg is AreaErrPtg)
            {
                return((CellRangeAddress)null);
            }
            throw new InvalidOperationException("Unexpected Shifted ptg class (" + ptg.GetType().Name + ")");
        }
Exemplo n.º 20
0
        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);
            bool           expectedChanged = aptg.FirstRow != expectedAreaFirstRow || aptg.LastRow != expectedAreaLastRow;

            AreaPtg copyPtg = (AreaPtg)aptg.Copy(); // clone so we can re-use aptg in calling method

            Ptg[] ptgs          = { copyPtg, };
            bool  actualChanged = fs.AdjustFormula(ptgs, 0);

            if (expectedAreaFirstRow < 0)
            {
                Assert.AreEqual(typeof(AreaErrPtg), ptgs[0].GetType());
                return;
            }
            Assert.AreEqual(expectedChanged, actualChanged);
            Assert.AreEqual(copyPtg, ptgs[0]);  // expected to change in place (although this is not a strict requirement)
            Assert.AreEqual(expectedAreaFirstRow, copyPtg.FirstRow);
            Assert.AreEqual(expectedAreaLastRow, copyPtg.LastRow);
        }
Exemplo n.º 21
0
        public void TestAbsoluteRelativeRefs()
        {
            AreaPtg sca1 = new AreaPtg(4, 5, 6, 7, true, false, true, false);
            AreaPtg sca2 = new AreaPtg(4, 5, 6, 7, false, true, false, true);
            AreaPtg sca3 = new AreaPtg(5, 5, 7, 7, true, false, true, false);
            AreaPtg sca4 = new AreaPtg(5, 5, 7, 7, false, true, false, true);

            Assert.AreEqual("G5:$H$6", sca1.ToFormulaString(), "first rel., last abs.");
            Assert.AreEqual("$G$5:H6", sca2.ToFormulaString(), "first abs., last rel.");
            Assert.AreEqual("H6:$H$6", sca3.ToFormulaString(), "first rel., last abs.");
            Assert.AreEqual("$H$6:H6", sca4.ToFormulaString(), "first abs., last rel.");

            AreaPtg cla1 = CloneArea(sca1);
            AreaPtg cla2 = CloneArea(sca2);
            AreaPtg cla3 = CloneArea(sca3);
            AreaPtg cla4 = CloneArea(sca4);

            Assert.AreEqual("G5:$H$6", cla1.ToFormulaString(), "first rel., last abs.");
            Assert.AreEqual("$G$5:H6", cla2.ToFormulaString(), "first abs., last rel.");
            Assert.AreEqual("H6:$H$6", cla3.ToFormulaString(), "first rel., last abs.");
            Assert.AreEqual("$H$6:H6", cla4.ToFormulaString(), "first abs., last rel.");
        }
Exemplo n.º 22
0
        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);

            ConfirmAreaShift(aptg, 8, 11, -3, 7, 20); // simple expansion of top
            // 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);
            ConfirmAreaShift(aptg, 8, 11, 9, 12, 20); // note behaviour Changes here
            ConfirmAreaShift(aptg, 8, 11, 10, 12, 21);
            ConfirmAreaShift(aptg, 8, 11, 12, 12, 23);
            ConfirmAreaShift(aptg, 8, 11, 13, 10, 20);  // ignored

            // rows from within being Moved:
            ConfirmAreaShift(aptg, 12, 16, 3, 10, 20);  // stay within - no change
            ConfirmAreaShift(aptg, 11, 19, 20, 10, 20); // Move completely out - no change
            ConfirmAreaShift(aptg, 16, 17, -6, 10, 20); // Moved exactly to top - no change
            ConfirmAreaShift(aptg, 16, 17, -7, 11, 20); // tRuncation at top
            ConfirmAreaShift(aptg, 12, 16, 4, 10, 20);  // Moved exactly to bottom - no change
            ConfirmAreaShift(aptg, 12, 16, 6, 10, 17);  // tRuncation at bottom

            // rows Containing area bottom being Shifted up:
            ConfirmAreaShift(aptg, 18, 22, -1, 10, 19);  // simple contraction at bottom
            ConfirmAreaShift(aptg, 18, 22, -7, 10, 13);  // simple contraction at bottom
            ConfirmAreaShift(aptg, 18, 22, -8, 10, 17);  // top calculated differently here
            ConfirmAreaShift(aptg, 18, 22, -9, 9, 17);
            ConfirmAreaShift(aptg, 18, 22, -15, 10, 20); // no change because range would be turned inside out
            ConfirmAreaShift(aptg, 15, 19, -7, 13, 20);  // dest tRuncates top (even though src is from inside range)
            ConfirmAreaShift(aptg, 19, 23, -12, 7, 18);  // complex: src encloses bottom, dest encloses top

            ConfirmAreaShift(aptg, 18, 22, 5, 10, 25);   // simple expansion at bottom
        }
Exemplo n.º 23
0
        private static void ConfirmAreaCopy(AreaPtg aptg,
                                            int firstRowCopied, int lastRowCopied, int rowOffset,
                                            int expectedFirstRow, int expectedLastRow, bool expectedChanged)
        {
            AreaPtg copyPtg = (AreaPtg)aptg.Copy(); // clone so we can re-use aptg in calling method

            Ptg[]          ptgs          = { copyPtg, };
            FormulaShifter fs            = FormulaShifter.CreateForRowCopy(0, null, firstRowCopied, lastRowCopied, rowOffset, SpreadsheetVersion.EXCEL2007);
            bool           actualChanged = fs.AdjustFormula(ptgs, 0);

            // DeletedAreaRef
            if (expectedFirstRow < 0 || expectedLastRow < 0)
            {
                Assert.AreEqual(typeof(AreaErrPtg), ptgs[0].GetType(),
                                "Reference should have shifted off worksheet, producing #REF! error: " + ptgs[0]);
                return;
            }

            Assert.AreEqual(expectedChanged, actualChanged, "Should this AreaPtg change due to row copy?");
            Assert.AreEqual(copyPtg, ptgs[0], "AreaPtgs should be modified in-place when a row containing the AreaPtg is copied");  // expected to change in place (although this is not a strict requirement)
            Assert.AreEqual(expectedFirstRow, copyPtg.FirstRow, "AreaPtg first row");
            Assert.AreEqual(expectedLastRow, copyPtg.LastRow, "AreaPtg last row");
        }
Exemplo n.º 24
0
        public void TestColumnOperand()
        {
            short   firstRow = (short)8;
            short   lastRow  = (short)12;
            short   colNum   = (short)5;
            AreaPtg areaPtg  = new AreaPtg(firstRow, lastRow, colNum, colNum, false, false, false, false);

            ValueEval[] values =
            {
                new NumberEval(27),
                new NumberEval(29),
                new NumberEval(35),                     // value in row 10
                new NumberEval(37),
                new NumberEval(38),
            };
            ValueEval[] args =
            {
                EvalFactory.CreateAreaEval(areaPtg, values),
            };
            double result = NumericFunctionInvoker.Invoke(UnaryPlusEval.instance, args, 10, (short)20);

            Assert.AreEqual(35, result, 0);
        }
Exemplo n.º 25
0
        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.FirstRow, cra.LastRow, cra.FirstColumn, cra.LastColumn, false, false, false, false);

            Ptg[] ptgs = { aptg, };

            if (!shifter.AdjustFormula(ptgs, currentExternSheetIx))
            {
                return(cra);
            }
            Ptg ptg0 = ptgs[0];

            if (ptg0 is AreaPtg)
            {
                AreaPtg bptg = (AreaPtg)ptg0;
                return(new CellRangeAddress(bptg.FirstRow, bptg.LastRow, bptg.FirstColumn, bptg.LastColumn));
            }
            if (ptg0 is AreaErrPtg)
            {
                return(null);
            }
            throw new InvalidCastException("Unexpected shifted ptg class (" + ptg0.GetType().Name + ")");
        }
Exemplo n.º 26
0
        /**
         * Creates a dummy AreaEval
         * @param values empty (<code>null</code>) entries in this array will be Converted to NumberEval.ZERO
         */
        public static AreaEval CreateAreaEval(String areaRefStr, ValueEval[] values)
        {
            AreaPtg areaPtg = new AreaPtg(areaRefStr);

            return(CreateAreaEval(areaPtg, values));
        }
Exemplo n.º 27
0
        /**
         * returns an appropriate Eval impl instance for the Ptg. The Ptg must be
         * one of: Area3DPtg, AreaPtg, ReferencePtg, Ref3DPtg, IntPtg, NumberPtg,
         * StringPtg, BoolPtg <br/>special Note: OperationPtg subtypes cannot be
         * passed here!
         */
        private ValueEval GetEvalForPtg(Ptg ptg, OperationEvaluationContext ec)
        {
            //  consider converting all these (ptg is XxxPtg) expressions To (ptg.GetType() == XxxPtg.class)

            if (ptg is NamePtg)
            {
                // named ranges, macro functions
                NamePtg         namePtg    = (NamePtg)ptg;
                IEvaluationName nameRecord = _workbook.GetName(namePtg);
                if (nameRecord.IsFunctionName)
                {
                    return(new NameEval(nameRecord.NameText));
                }
                if (nameRecord.HasFormula)
                {
                    return(EvaluateNameFormula(nameRecord.NameDefinition, ec));
                }

                throw new Exception("Don't now how To evalate name '" + nameRecord.NameText + "'");
            }
            if (ptg is NameXPtg)
            {
                return(ec.GetNameXEval(((NameXPtg)ptg)));
            }

            if (ptg is IntPtg)
            {
                return(new NumberEval(((IntPtg)ptg).Value));
            }
            if (ptg is NumberPtg)
            {
                return(new NumberEval(((NumberPtg)ptg).Value));
            }
            if (ptg is StringPtg)
            {
                return(new StringEval(((StringPtg)ptg).Value));
            }
            if (ptg is BoolPtg)
            {
                return(BoolEval.ValueOf(((BoolPtg)ptg).Value));
            }
            if (ptg is ErrPtg)
            {
                return(ErrorEval.ValueOf(((ErrPtg)ptg).ErrorCode));
            }
            if (ptg is MissingArgPtg)
            {
                return(MissingArgEval.instance);
            }
            if (ptg is AreaErrPtg || ptg is RefErrorPtg ||
                ptg is DeletedArea3DPtg || ptg is DeletedRef3DPtg)
            {
                return(ErrorEval.REF_INVALID);
            }
            if (ptg is Ref3DPtg)
            {
                Ref3DPtg rptg = (Ref3DPtg)ptg;
                return(ec.GetRef3DEval(rptg.Row, rptg.Column, rptg.ExternSheetIndex));
            }
            if (ptg is Area3DPtg)
            {
                Area3DPtg aptg = (Area3DPtg)ptg;
                return(ec.GetArea3DEval(aptg.FirstRow, aptg.FirstColumn, aptg.LastRow, aptg.LastColumn, aptg.ExternSheetIndex));
            }
            if (ptg is RefPtg)
            {
                RefPtg rptg = (RefPtg)ptg;
                return(ec.GetRefEval(rptg.Row, rptg.Column));
            }
            if (ptg is AreaPtg)
            {
                AreaPtg aptg = (AreaPtg)ptg;
                return(ec.GetAreaEval(aptg.FirstRow, aptg.FirstColumn, aptg.LastRow, aptg.LastColumn));
            }

            if (ptg is UnknownPtg)
            {
                // POI uses UnknownPtg when the encoded Ptg array seems To be corrupted.
                // This seems To occur in very rare cases (e.g. unused name formulas in bug 44774, attachment 21790)
                // In any case, formulas are re-parsed before execution, so UnknownPtg should not Get here
                throw new RuntimeException("UnknownPtg not allowed");
            }
            if (ptg is ExpPtg)
            {
                // ExpPtg is used for array formulas and shared formulas.
                // it is currently unsupported, and may not even get implemented here
                throw new RuntimeException("ExpPtg currently not supported");
            }
            throw new RuntimeException("Unexpected ptg class (" + ptg.GetType().Name + ")");
        }
Exemplo n.º 28
0
 public MockAreaEval(AreaPtg areaPtg, ValueEval[] values)
     : base(areaPtg)
 {
     _values = values;
 }