Esempio n. 1
0
 public LazyAreaEval(int firstRowIndex, int firstColumnIndex, int lastRowIndex,
         int lastColumnIndex, SheetRefEvaluator evaluator):
     base(firstRowIndex, firstColumnIndex, lastRowIndex, lastColumnIndex)
 {
     
     _evaluator = evaluator;
 }
Esempio n. 2
0
 public LazyRefEval(int rowIndex, int columnIndex, SheetRefEvaluator sre)
     : base(rowIndex, columnIndex)
 {
     if (sre == null)
     {
         throw new ArgumentException("sre must not be null");
     }
     _evaluator = sre;
 }
Esempio n. 3
0
 public SheetRangeEvaluator(int firstSheetIndex, int lastSheetIndex, SheetRefEvaluator[] sheetEvaluators)
 {
     if (firstSheetIndex < 0)
     {
         throw new ArgumentException("Invalid firstSheetIndex: " + firstSheetIndex + ".");
     }
     if (lastSheetIndex < firstSheetIndex)
     {
         throw new ArgumentException("Invalid lastSheetIndex: " + lastSheetIndex + " for firstSheetIndex: " + firstSheetIndex + ".");
     }
     _firstSheetIndex = firstSheetIndex;
     _lastSheetIndex = lastSheetIndex;
     _sheetEvaluators = sheetEvaluators;
 }
Esempio n. 4
0
        public ValueEval GetNameXEval(NameXPtg nameXPtg)
        {
            ExternalSheet externSheet = _workbook.GetExternalSheet(nameXPtg.SheetRefIndex);

            if (externSheet == null)
            {
                return(new NameXEval(nameXPtg));
            }
            String       workbookName = externSheet.GetWorkbookName();
            ExternalName externName   = _workbook.GetExternalName(
                nameXPtg.SheetRefIndex,
                nameXPtg.NameIndex
                );

            try
            {
                WorkbookEvaluator refWorkbookEvaluator = _bookEvaluator.GetOtherWorkbookEvaluator(workbookName);
                IEvaluationName   evaluationName       = refWorkbookEvaluator.GetName(externName.Name, externName.Ix - 1);
                if (evaluationName != null && evaluationName.HasFormula)
                {
                    if (evaluationName.NameDefinition.Length > 1)
                    {
                        throw new Exception("Complex name formulas not supported yet");
                    }
                    Ptg ptg = evaluationName.NameDefinition[0];
                    if (ptg is Ref3DPtg)
                    {
                        Ref3DPtg          ref3D      = (Ref3DPtg)ptg;
                        int               sheetIndex = refWorkbookEvaluator.GetSheetIndexByExternIndex(ref3D.ExternSheetIndex);
                        String            sheetName  = refWorkbookEvaluator.GetSheetName(sheetIndex);
                        SheetRefEvaluator sre        = CreateExternSheetRefEvaluator(workbookName, sheetName);
                        return(new LazyRefEval(ref3D.Row, ref3D.Column, sre));
                    }
                    else if (ptg is Area3DPtg)
                    {
                        Area3DPtg         area3D     = (Area3DPtg)ptg;
                        int               sheetIndex = refWorkbookEvaluator.GetSheetIndexByExternIndex(area3D.ExternSheetIndex);
                        String            sheetName  = refWorkbookEvaluator.GetSheetName(sheetIndex);
                        SheetRefEvaluator sre        = CreateExternSheetRefEvaluator(workbookName, sheetName);
                        return(new LazyAreaEval(area3D.FirstRow, area3D.FirstColumn, area3D.LastRow, area3D.LastColumn, sre));
                    }
                }
                return(ErrorEval.REF_INVALID);
            }
            catch (WorkbookNotFoundException)
            {
                return(ErrorEval.REF_INVALID);
            }
        }
Esempio n. 5
0
        public SheetRangeEvaluator GetRefEvaluatorForCurrentSheet()
        {
            SheetRefEvaluator sre = new SheetRefEvaluator(_bookEvaluator, _tracker, _sheetIndex);

            return(new SheetRangeEvaluator(_sheetIndex, sre));
        }
Esempio n. 6
0
 public LazyAreaEval(int firstRowIndex, int firstColumnIndex, int lastRowIndex,
                     int lastColumnIndex, SheetRefEvaluator evaluator) :
     base(firstRowIndex, firstColumnIndex, lastRowIndex, lastColumnIndex)
 {
     _evaluator = evaluator;
 }
Esempio n. 7
0
 public LazyAreaEval(AreaI ptg, SheetRefEvaluator evaluator) : base(ptg)
 {
     _evaluator = evaluator;
 }
Esempio n. 8
0
        SheetRangeEvaluator CreateExternSheetRefEvaluator(ExternalSheet externalSheet)
        {
            WorkbookEvaluator targetEvaluator;
            int otherFirstSheetIndex;
            int otherLastSheetIndex = -1;

            if (externalSheet == null || externalSheet.WorkbookName == null)
            {
                // sheet is in same workbook
                targetEvaluator = _bookEvaluator;

                if (externalSheet == null)
                {
                    otherFirstSheetIndex = 0;
                }
                else
                {
                    otherFirstSheetIndex = _workbook.GetSheetIndex(externalSheet.SheetName);
                }

                if (externalSheet is ExternalSheetRange)
                {
                    String lastSheetName = ((ExternalSheetRange)externalSheet).LastSheetName;
                    otherLastSheetIndex = _workbook.GetSheetIndex(lastSheetName);
                }
            }
            else
            {
                // look up sheet by name from external workbook
                String workbookName = externalSheet.WorkbookName;
                try
                {
                    targetEvaluator = _bookEvaluator.GetOtherWorkbookEvaluator(workbookName);
                }
                catch (WorkbookNotFoundException e)
                {
                    throw new RuntimeException(e.Message, e);
                }

                otherFirstSheetIndex = targetEvaluator.GetSheetIndex(externalSheet.SheetName);
                if (externalSheet is ExternalSheetRange)
                {
                    String lastSheetName = ((ExternalSheetRange)externalSheet).LastSheetName;
                    otherLastSheetIndex = targetEvaluator.GetSheetIndex(lastSheetName);
                }

                if (otherFirstSheetIndex < 0)
                {
                    throw new Exception("Invalid sheet name '" + externalSheet.SheetName
                                        + "' in bool '" + workbookName + "'.");
                }
            }

            if (otherLastSheetIndex == -1)
            {
                // Reference to just one sheet
                otherLastSheetIndex = otherFirstSheetIndex;
            }

            SheetRefEvaluator[] Evals = new SheetRefEvaluator[otherLastSheetIndex - otherFirstSheetIndex + 1];
            for (int i = 0; i < Evals.Length; i++)
            {
                int otherSheetIndex = i + otherFirstSheetIndex;
                Evals[i] = new SheetRefEvaluator(targetEvaluator, _tracker, otherSheetIndex);
            }
            return(new SheetRangeEvaluator(otherFirstSheetIndex, otherLastSheetIndex, Evals));
        }
        SheetRangeEvaluator CreateExternSheetRefEvaluator(ExternalSheet externalSheet)
        {
            WorkbookEvaluator targetEvaluator;
            int otherFirstSheetIndex;
            int otherLastSheetIndex = -1;
            if (externalSheet == null || externalSheet.WorkbookName == null)
            {
                // sheet is in same workbook
                targetEvaluator = _bookEvaluator;
                otherFirstSheetIndex = _workbook.GetSheetIndex(externalSheet.SheetName);

                if (externalSheet is ExternalSheetRange)
                {
                    String lastSheetName = ((ExternalSheetRange)externalSheet).LastSheetName;
                    otherLastSheetIndex = _workbook.GetSheetIndex(lastSheetName);
                }
            }
            else
            {
                // look up sheet by name from external workbook
                String workbookName = externalSheet.WorkbookName;
                try
                {
                    targetEvaluator = _bookEvaluator.GetOtherWorkbookEvaluator(workbookName);
                }
                catch (WorkbookNotFoundException e)
                {
                    throw new RuntimeException(e.Message, e);
                }

                otherFirstSheetIndex = targetEvaluator.GetSheetIndex(externalSheet.SheetName);
                if (externalSheet is ExternalSheetRange)
                {
                    String lastSheetName = ((ExternalSheetRange)externalSheet).LastSheetName;
                    otherLastSheetIndex = targetEvaluator.GetSheetIndex(lastSheetName);
                }

                if (otherFirstSheetIndex < 0)
                {
                    throw new Exception("Invalid sheet name '" + externalSheet.SheetName
                            + "' in bool '" + workbookName + "'.");
                }
            }

            if (otherLastSheetIndex == -1)
            {
                // Reference to just one sheet
                otherLastSheetIndex = otherFirstSheetIndex;
            }

            SheetRefEvaluator[] Evals = new SheetRefEvaluator[otherLastSheetIndex - otherFirstSheetIndex + 1];
            for (int i = 0; i < Evals.Length; i++)
            {
                int otherSheetIndex = i + otherFirstSheetIndex;
                Evals[i] = new SheetRefEvaluator(targetEvaluator, _tracker, otherSheetIndex);
            }
            return new SheetRangeEvaluator(otherFirstSheetIndex, otherLastSheetIndex, Evals);
        }
Esempio n. 10
0
        public ValueEval GetRef3DEval(int rowIndex, int columnIndex, int extSheetIndex)
        {
            SheetRefEvaluator sre = CreateExternSheetRefEvaluator(extSheetIndex);

            return(new LazyRefEval(rowIndex, columnIndex, sre));
        }
Esempio n. 11
0
 public SheetRangeEvaluator(int onlySheetIndex, SheetRefEvaluator sheetEvaluator)
     : this(onlySheetIndex, onlySheetIndex, new SheetRefEvaluator[] { sheetEvaluator })
 {
     
 }
Esempio n. 12
0
        public LazyRefEval(Ref3DPtg ptg, SheetRefEvaluator sre)
            : base(ptg.Row, ptg.Column)
        {

            _evaluator = sre;
        }
Esempio n. 13
0
 public SheetRangeEvaluator(int onlySheetIndex, SheetRefEvaluator sheetEvaluator)
     : this(onlySheetIndex, onlySheetIndex, new SheetRefEvaluator[] { sheetEvaluator })
 {
 }
Esempio n. 14
0
        /**
         * Resolves a cell or area reference dynamically.
         * @param workbookName the name of the workbook Containing the reference.  If <code>null</code>
         * the current workbook is assumed.  Note - to Evaluate formulas which use multiple workbooks,
         * a {@link CollaboratingWorkbooksEnvironment} must be set up.
         * @param sheetName the name of the sheet Containing the reference.  May be <code>null</code>
         * (when <c>workbookName</c> is also null) in which case the current workbook and sheet is
         * assumed.
         * @param refStrPart1 the single cell reference or first part of the area reference.  Must not
         * be <code>null</code>.
         * @param refStrPart2 the second part of the area reference. For single cell references this
         * parameter must be <code>null</code>
         * @param isA1Style specifies the format for <c>refStrPart1</c> and <c>refStrPart2</c>.
         * Pass <c>true</c> for 'A1' style and <c>false</c> for 'R1C1' style.
         * TODO - currently POI only supports 'A1' reference style
         * @return a {@link RefEval} or {@link AreaEval}
         */
        public ValueEval GetDynamicReference(String workbookName, String sheetName, String refStrPart1,
                                             String refStrPart2, bool isA1Style)
        {
            if (!isA1Style)
            {
                throw new Exception("R1C1 style not supported yet");
            }
            SheetRefEvaluator sre = CreateExternSheetRefEvaluator(workbookName, sheetName);

            if (sre == null)
            {
                return(ErrorEval.REF_INVALID);
            }
            // ugly typecast - TODO - make spReadsheet version more easily accessible
            SpreadsheetVersion ssVersion = ((IFormulaParsingWorkbook)_workbook).GetSpreadsheetVersion();

            NameType part1refType = ClassifyCellReference(refStrPart1, ssVersion);

            switch (part1refType)
            {
            case NameType.BAD_CELL_OR_NAMED_RANGE:
                return(ErrorEval.REF_INVALID);

            case NameType.NAMED_RANGE:
                IEvaluationName nm = ((IFormulaParsingWorkbook)_workbook).GetName(refStrPart1, _sheetIndex);
                if (!nm.IsRange)
                {
                    throw new Exception("Specified name '" + refStrPart1 + "' is not a range as expected.");
                }
                return(_bookEvaluator.EvaluateNameFormula(nm.NameDefinition, this));
            }
            if (refStrPart2 == null)
            {
                // no ':'
                switch (part1refType)
                {
                case NameType.COLUMN:
                case NameType.ROW:
                    return(ErrorEval.REF_INVALID);

                case NameType.CELL:
                    CellReference cr = new CellReference(refStrPart1);
                    return(new LazyRefEval(cr.Row, cr.Col, sre));
                }
                throw new InvalidOperationException("Unexpected reference classification of '" + refStrPart1 + "'.");
            }
            NameType part2refType = ClassifyCellReference(refStrPart1, ssVersion);

            switch (part2refType)
            {
            case NameType.BAD_CELL_OR_NAMED_RANGE:
                return(ErrorEval.REF_INVALID);

            case NameType.NAMED_RANGE:
                throw new Exception("Cannot Evaluate '" + refStrPart1
                                    + "'. Indirect Evaluation of defined names not supported yet");
            }

            if (part2refType != part1refType)
            {
                // LHS and RHS of ':' must be compatible
                return(ErrorEval.REF_INVALID);
            }
            int firstRow, firstCol, lastRow, lastCol;

            switch (part1refType)
            {
            case NameType.COLUMN:
                firstRow = 0;
                lastRow  = ssVersion.LastRowIndex;
                firstCol = ParseColRef(refStrPart1);
                lastCol  = ParseColRef(refStrPart2);
                break;

            case NameType.ROW:
                firstCol = 0;
                lastCol  = ssVersion.LastColumnIndex;
                firstRow = ParseRowRef(refStrPart1);
                lastRow  = ParseRowRef(refStrPart2);
                break;

            case NameType.CELL:
                CellReference cr;
                cr       = new CellReference(refStrPart1);
                firstRow = cr.Row;
                firstCol = cr.Col;
                cr       = new CellReference(refStrPart2);
                lastRow  = cr.Row;
                lastCol  = cr.Col;
                break;

            default:
                throw new InvalidOperationException("Unexpected reference classification of '" + refStrPart1 + "'.");
            }
            return(new LazyAreaEval(firstRow, firstCol, lastRow, lastCol, sre));
        }
Esempio n. 15
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 Eval GetEvalForPtg(Ptg ptg, int sheetIndex, EvaluationTracker tracker)
        {
            //  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;
                EvaluationName nameRecord = _workbook.GetName(namePtg);
                if (nameRecord.IsFunctionName)
                {
                    return new NameEval(nameRecord.NameText);
                }
                if (nameRecord.HasFormula)
                {
                    return EvaluateNameFormula(nameRecord.NameDefinition, sheetIndex, tracker);
                }

                throw new Exception("Don't now how To evalate name '" + nameRecord.NameText + "'");
            }
            if (ptg is NameXPtg)
            {
                return new NameXEval(((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 refPtg = (Ref3DPtg)ptg;
                SheetRefEvaluator sre = CreateExternSheetRefEvaluator(tracker, refPtg);
                return new LazyRefEval(refPtg, sre);
            }
            if (ptg is Area3DPtg)
            {
                Area3DPtg aptg = (Area3DPtg)ptg;
                SheetRefEvaluator sre = CreateExternSheetRefEvaluator(tracker, aptg);
                return new LazyAreaEval(aptg, sre);
            }
            SheetRefEvaluator sre1 = new SheetRefEvaluator(this, tracker, _workbook, sheetIndex);
            if (ptg is RefPtg)
            {
                return new LazyRefEval(((RefPtg)ptg), sre1);
            }
            if (ptg is AreaPtg)
            {
                return new LazyAreaEval(((AreaPtg)ptg), sre1);
            }

            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 Exception("UnknownPtg not allowed");
            }

            throw new Exception("Unexpected ptg class (" + ptg.GetType().Name + ")");
        }
Esempio n. 16
0
        public ValueEval GetRefEval(int rowIndex, int columnIndex)
        {
            SheetRefEvaluator sre = GetRefEvaluatorForCurrentSheet();

            return(new LazyRefEval(rowIndex, columnIndex, sre));
        }
Esempio n. 17
0
        public LazyRefEval(Ref3DPtg ptg, SheetRefEvaluator sre)
            : base(ptg.Row, ptg.Column)
        {

            _evaluator = sre;
        }
Esempio n. 18
0
 public LazyAreaEval(AreaI ptg, SheetRefEvaluator evaluator)
     : base(ptg)
 {
     _evaluator = evaluator;
 }
 public SheetRangeEvaluator GetRefEvaluatorForCurrentSheet()
 {
     SheetRefEvaluator sre = new SheetRefEvaluator(_bookEvaluator, _tracker, _sheetIndex);
     return new SheetRangeEvaluator(_sheetIndex, sre);
 }