public SheetRangeEvaluator GetRefEvaluatorForCurrentSheet()
        {
            SheetRefEvaluator sre = new SheetRefEvaluator(_bookEvaluator, _tracker, _sheetIndex);

            return(new SheetRangeEvaluator(_sheetIndex, sre));
        }
        /**
         * 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 se = CreateExternSheetRefEvaluator(workbookName, sheetName);

            if (se == null)
            {
                return(ErrorEval.REF_INVALID);
            }
            SheetRangeEvaluator sre = new SheetRangeEvaluator(_sheetIndex, se);

            // ugly typecast - TODO - make spReadsheet version more easily accessible
            SpreadsheetVersion ssVersion = ((IFormulaParsingWorkbook)_workbook).GetSpreadsheetVersion();

            NameType part1refType = ClassifyCellReference(refStrPart1, ssVersion);

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

            case NameType.NamedRange:
                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.BadCellOrNamedRange:
                return(ErrorEval.REF_INVALID);

            case NameType.NamedRange:
                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;
                if (part2refType.Equals(NameType.Column))
                {
                    lastRow  = ssVersion.LastRowIndex;
                    firstCol = ParseRowRef(refStrPart1);
                    lastCol  = ParseRowRef(refStrPart2);
                }
                else
                {
                    lastRow  = ssVersion.LastRowIndex;
                    firstCol = ParseColRef(refStrPart1);
                    lastCol  = ParseColRef(refStrPart2);
                }
                break;

            case NameType.Row:
                // support of cell range in the form of integer:integer
                firstCol = 0;
                if (part2refType.Equals(NameType.Row))
                {
                    firstRow = ParseColRef(refStrPart1);
                    lastRow  = ParseColRef(refStrPart2);
                    lastCol  = ssVersion.LastColumnIndex;
                }
                else
                {
                    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));
        }
Example #3
0
 public SheetRangeEvaluator(int onlySheetIndex, SheetRefEvaluator sheetEvaluator)
     : this(onlySheetIndex, onlySheetIndex, new SheetRefEvaluator[] { sheetEvaluator })
 {
 }