예제 #1
0
        /**
         * Evaluate a formula outside a cell value, e.g. conditional format rules or data validation expressions
         *
         * @param formula to evaluate
         * @param ref defines the optional sheet and row/column base for the formula, if it is relative
         * @return value
         */
        public ValueEval Evaluate(String formula, CellReference reference)
        {
            String sheetName = reference == null ? null : reference.SheetName;
            int    sheetIndex;

            if (sheetName == null)
            {
                sheetIndex = -1; // workbook scope only
            }
            else
            {
                sheetIndex = Workbook.GetSheetIndex(sheetName);
            }
            int rowIndex = reference == null ? -1 : reference.Row;
            int colIndex = reference == null ? -1 : reference.Col;
            OperationEvaluationContext ec = new OperationEvaluationContext(
                this,
                Workbook,
                sheetIndex,
                rowIndex,
                colIndex,
                new EvaluationTracker(_cache)
                );

            Ptg[] ptgs = FormulaParser.Parse(formula, (IFormulaParsingWorkbook)Workbook, FormulaType.Cell, sheetIndex, rowIndex);
            return(EvaluateNameFormula(ptgs, ec));
        }
        private 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));
        }
예제 #3
0
        public int GetSheetIndex(IEvaluationSheet sheet)
        {
            object result = _sheetIndexesBySheet[sheet];

            if (result == null)
            {
                int sheetIndex = _workbook.GetSheetIndex(sheet);
                if (sheetIndex < 0)
                {
                    throw new Exception("Specified sheet from a different book");
                }
                result = sheetIndex;
                _sheetIndexesBySheet[sheet] = result;
            }
            return((int)result);
        }
예제 #4
0
        public int GetSheetIndex(IEvaluationSheet sheet)
        {
            int result = int.MinValue;

            if (_sheetIndexesBySheet.ContainsKey(sheet))
            {
                result = _sheetIndexesBySheet[sheet];
            }
            if (result == int.MinValue)
            {
                int sheetIndex = _workbook.GetSheetIndex(sheet);
                if (sheetIndex < 0)
                {
                    throw new Exception("Specified sheet from a different book");
                }
                result = sheetIndex;
                _sheetIndexesBySheet[sheet] = result;
            }
            return(result);
        }
예제 #5
0
        private ForkedEvaluationSheet GetSharedSheet(String sheetName)
        {
            ForkedEvaluationSheet result = null;

            if (_sharedSheetsByName.ContainsKey(sheetName))
            {
                result = _sharedSheetsByName[(sheetName)];
            }
            if (result == null)
            {
                result = new ForkedEvaluationSheet(_masterBook.GetSheet(_masterBook
                                                                        .GetSheetIndex(sheetName)));
                if (_sharedSheetsByName.ContainsKey(sheetName))
                {
                    _sharedSheetsByName[sheetName] = result;
                }
                else
                {
                    _sharedSheetsByName.Add(sheetName, result);
                }
            }
            return(result);
        }
예제 #6
0
 public int GetSheetIndex(IEvaluationWorkbook mewb)
 {
     return(mewb.GetSheetIndex(_masterSheet));
 }