Exemple #1
0
 /**
  * Call this on any ptg reference contained in a row of cells that was copied.
  * If the ptg reference is relative, the references will be shifted by the distance
  * that the rows were copied.
  * In the future similar functions could be written due to column copying or
  * individual cell copying. Just make sure to only call adjustPtgDueToRowCopy on
  * formula cells that are copied (unless row shifting, where references outside
  * of the shifted region need to be updated to reflect the shift, a copy is self-contained).
  *
  * @param ptg the ptg to shift
  * @return deleted ref ptg, in-place modified ptg, or null
  * If Ptg would be shifted off the first or last row of a sheet, return deleted ref
  * If Ptg needs to be changed, modifies Ptg in-place
  * If Ptg doesn't need to be changed, returns <code>null</code>
  */
 private Ptg AdjustPtgDueToRowCopy(Ptg ptg)
 {
     if (ptg is RefPtg)
     {
         RefPtg rptg = (RefPtg)ptg;
         return(RowCopyRefPtg(rptg));
     }
     if (ptg is Ref3DPtg)
     {
         Ref3DPtg rptg = (Ref3DPtg)ptg;
         return(RowCopyRefPtg(rptg));
     }
     if (ptg is Ref3DPxg)
     {
         Ref3DPxg rpxg = (Ref3DPxg)ptg;
         return(RowCopyRefPtg(rpxg));
     }
     if (ptg is Area2DPtgBase)
     {
         return(RowCopyAreaPtg((Area2DPtgBase)ptg));
     }
     if (ptg is Area3DPtg)
     {
         Area3DPtg aptg = (Area3DPtg)ptg;
         return(RowCopyAreaPtg(aptg));
     }
     if (ptg is Area3DPxg)
     {
         Area3DPxg apxg = (Area3DPxg)ptg;
         return(RowCopyAreaPtg(apxg));
     }
     return(null);
 }
Exemple #2
0
        public void TestColumnGreater255()
        {
            RefPtgBase ptg;

            ptg = new RefPtg("IW1");
            Assert.AreEqual(256, ptg.Column);
            Assert.AreEqual("IW1", ptg.FormatReferenceAsString());

            ptg = new RefPtg("JA1");
            Assert.AreEqual(260, ptg.Column);
            Assert.AreEqual("JA1", ptg.FormatReferenceAsString());
        }
Exemple #3
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;
        }
Exemple #4
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);
        }
Exemple #5
0
 /**
  * @return <c>true</c> if this Ptg needed to be changed
  */
 private Ptg AdjustPtgDueToRowMove(Ptg ptg, int currentExternSheetIx)
 {
     if (ptg is RefPtg)
     {
         if (currentExternSheetIx != _externSheetIndex)
         {
             // local refs on other sheets are unaffected
             return(null);
         }
         RefPtg rptg = (RefPtg)ptg;
         return(RowMoveRefPtg(rptg));
     }
     if (ptg is Ref3DPtg)
     {
         Ref3DPtg rptg = (Ref3DPtg)ptg;
         if (_externSheetIndex != rptg.ExternSheetIndex)
         {
             // only move 3D refs that refer to the sheet with cells being moved
             // (currentExternSheetIx is irrelevant)
             return(null);
         }
         return(RowMoveRefPtg(rptg));
     }
     if (ptg is Area2DPtgBase)
     {
         if (currentExternSheetIx != _externSheetIndex)
         {
             // local refs on other sheets are unaffected
             return(ptg);
         }
         return(RowMoveAreaPtg((Area2DPtgBase)ptg));
     }
     if (ptg is Area3DPtg)
     {
         Area3DPtg aptg = (Area3DPtg)ptg;
         if (_externSheetIndex != aptg.ExternSheetIndex)
         {
             // only move 3D refs that refer to the sheet with cells being moved
             // (currentExternSheetIx is irrelevant)
             return(null);
         }
         return(RowMoveAreaPtg(aptg));
     }
     return(null);
 }
        public void TestLinkFormula()
        {
            RecordInputStream is1 = new RecordInputStream(new MemoryStream(linkData));

            is1.NextRecord();
            TextObjectRecord rec = new TextObjectRecord(is1);

            Ptg ptg = rec.LinkRefPtg;

            Assert.IsNotNull(ptg);
            Assert.AreEqual(typeof(RefPtg), ptg.GetType());
            RefPtg rptg = (RefPtg)ptg;

            Assert.AreEqual("T2", rptg.ToFormulaString());

            byte[] data2 = rec.Serialize();
            Assert.AreEqual(linkData.Length, data2.Length);
            Assert.IsTrue(Arrays.Equals(linkData, data2));
        }
Exemple #7
0
        public void TestConvertSharedFormulasOperandClasses_bug45123()
        {
            RecordInputStream in1        = TestcaseRecordInputStream.Create(0, SHARED_FORMULA_WITH_REF_ARRAYS_DATA);
            short             encodedLen = in1.ReadShort();

            Ptg[] sharedFormula = Ptg.ReadTokens(encodedLen, in1);

            Ptg[] convertedFormula = SharedFormulaRecord.ConvertSharedFormulas(sharedFormula, 100, 200);

            RefPtg refPtg = (RefPtg)convertedFormula[1];

            Assert.AreEqual("$C101", refPtg.ToFormulaString());
            if (refPtg.PtgClass == Ptg.CLASS_REF)
            {
                throw new AssertFailedException("Identified bug 45123");
            }

            ConfirmOperandClasses(sharedFormula, convertedFormula);
        }
Exemple #8
0
        public void TestReSerialize()
        {
            FormulaRecord formulaRecord = new FormulaRecord();

            formulaRecord.Row              = (/*setter*/ 1);
            formulaRecord.Column           = (/*setter*/ (short)1);
            formulaRecord.ParsedExpression = (/*setter*/ new Ptg[] { new RefPtg("B$5"), });
            formulaRecord.Value            = (/*setter*/ 3.3);
            byte[] ser = formulaRecord.Serialize();
            Assert.AreEqual(31, ser.Length);

            RecordInputStream in1 = TestcaseRecordInputStream.Create(ser);
            FormulaRecord     fr2 = new FormulaRecord(in1);

            Assert.AreEqual(3.3, fr2.Value, 0.0);
            Ptg[] ptgs = fr2.ParsedExpression;
            Assert.AreEqual(1, ptgs.Length);
            RefPtg rp = (RefPtg)ptgs[0];

            Assert.AreEqual("B$5", rp.ToFormulaString());
        }
Exemple #9
0
        public void TestConvertSharedFormulasOperandClasses_bug45123()
        {
            ILittleEndianInput in1 = TestcaseRecordInputStream.CreateLittleEndian(SHARED_FORMULA_WITH_REF_ARRAYS_DATA);
            int encodedLen         = in1.ReadUShort();

            Ptg[] sharedFormula = Ptg.ReadTokens(encodedLen, in1);

            SharedFormula sf = new SharedFormula(SpreadsheetVersion.EXCEL97);

            Ptg[] ConvertedFormula = sf.ConvertSharedFormulas(sharedFormula, 100, 200);

            RefPtg refPtg = (RefPtg)ConvertedFormula[1];

            Assert.AreEqual("$C101", refPtg.ToFormulaString());
            if (refPtg.PtgClass == Ptg.CLASS_REF)
            {
                throw new AssertionException("Identified bug 45123");
            }

            ConfirmOperandClasses(sharedFormula, ConvertedFormula);
        }
Exemple #10
0
 public MockRefEval(RefPtg ptg, ValueEval value)
     : base(-1, -1, ptg.Row, ptg.Column)
 {
     _value = value;
 }
        /**
         * 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 + ")");
        }