public static CellRange ParseCellRange(Worksheet worksheet, string cellRange) { CalcExpression expression = FormulaUtility.Formula2Expression(worksheet, cellRange); if (expression is CalcReferenceExpression) { return(GetRangeFromExpression(expression as CalcReferenceExpression)); } if (expression is CalcNameExpression) { CalcNameExpression expression2 = expression as CalcNameExpression; NameInfo customName = worksheet.GetCustomName(expression2.Name); if ((customName != null) && (customName.Expression is CalcReferenceExpression)) { return(GetRangeFromExpression(customName.Expression as CalcReferenceExpression)); } } else if (expression is CalcExternalNameExpression) { CalcExternalNameExpression expression3 = expression as CalcExternalNameExpression; NameInfo info2 = worksheet.GetCustomName(expression3.Name); if ((info2 != null) && (info2.Expression is CalcReferenceExpression)) { return(GetRangeFromExpression(info2.Expression as CalcReferenceExpression)); } } return(null); }
/// <summary> /// hdt 唐忠宝 将 private 改成 public /// </summary> /// <param name="expr"></param> /// <param name="context"></param> /// <param name="acceptsArray"></param> /// <param name="acceptsReference"></param> /// <returns></returns> public object Evaluate(CalcExpression expr, CalcEvaluatorContext context, bool acceptsArray, bool acceptsReference) { if (expr == null) { throw new ArgumentNullException(); } while (true) { if (!(expr is CalcParenthesesExpression)) { if (expr is CalcConstantExpression) { return(this.EvaluateConstant(expr as CalcConstantExpression, context, acceptsArray)); } if (expr is CalcReferenceExpression) { return(this.EvaluateReference(expr as CalcReferenceExpression, context, acceptsReference)); } if (expr is CalcExternalNameExpression) { CalcExternalNameExpression expression = expr as CalcExternalNameExpression; return(this.EvaluateName(expression.Name, expression.Source.GetEvaluatorContext(new CalcCellIdentity(context.Row, context.Column)), context, acceptsArray, acceptsReference)); } if (expr is CalcNameExpression) { return(this.EvaluateName((expr as CalcNameExpression).Name, context, context, acceptsArray, acceptsReference)); } if (expr is CalcUnaryOperatorExpression) { return(this.EvaluateUnaryOperation(expr as CalcUnaryOperatorExpression, context, acceptsArray)); } if (expr is CalcBinaryOperatorExpression) { return(this.EvaluateBinaryOperation(expr as CalcBinaryOperatorExpression, context, acceptsArray)); } if (expr is CalcFunctionExpression) { return(this.EvaluateFunction(expr as CalcFunctionExpression, context, acceptsArray, acceptsReference)); } if (!(expr is CalcSharedExpression)) { throw new ArgumentException("Exceptions.NotSupportExpression", expr.ToString()); } return(this.Evaluate((expr as CalcSharedExpression).Expression, context, acceptsArray, acceptsReference)); } expr = (expr as CalcParenthesesExpression).Arg; } }
protected override CalcExpression VisitExternalNameExpression(CalcExternalNameExpression expr, int baseRow, int baseColumn) { if ((expr.Source == this._context.Source) && (CultureInfo.InvariantCulture.CompareInfo.Compare(expr.Name, this._name, CompareOptions.IgnoreCase) == 0)) { throw new ArgumentException(); } CalcEvaluator.ValidateNameVisitor visitor = new CalcEvaluator.ValidateNameVisitor(this._context, this._name); CalcExpression name = expr.Source.GetEvaluatorContext(new CalcCellIdentity(this._context.Row, this._context.Column)).GetName(expr.Name); if (name == null) { throw new ArgumentException(); } visitor.Visit(name, baseRow, baseColumn); return(base.VisitExternalNameExpression(expr, baseRow, baseColumn)); }
public static void ExtractAllReferenceExpression(ICalcEvaluator evaluator, CalcExpression root, List <CalcReferenceExpression> nodes) { if (root is CalcBinaryOperatorExpression) { CalcBinaryOperatorExpression expression = root as CalcBinaryOperatorExpression; ExtractAllReferenceExpression(evaluator, expression.Left, nodes); ExtractAllReferenceExpression(evaluator, expression.Right, nodes); } else if (root is CalcParenthesesExpression) { CalcParenthesesExpression expression2 = root as CalcParenthesesExpression; ExtractAllReferenceExpression(evaluator, expression2.Arg, nodes); } else if (root is CalcExternalNameExpression) { CalcExternalNameExpression expression3 = root as CalcExternalNameExpression; ICalcSource source = expression3.Source; if (source != null) { CalcExpression expression4 = source.GetDefinedName(expression3.Name, -1, -1); if (expression4 != null) { ExtractAllReferenceExpression(evaluator, expression4, nodes); } } } else if (root is CalcFunctionExpression) { CalcFunctionExpression expr = root as CalcFunctionExpression; Worksheet worksheet = evaluator as Worksheet; if (worksheet != null) { CalcEvaluatorContext context = new CalcEvaluatorContext(worksheet, false, worksheet.ActiveRowIndex, worksheet.ActiveColumnIndex, 1, 1); object obj2 = new CalcEvaluator().Evaluate(expr, context, true, true); if (obj2 is CalcReference) { CalcReference reference = obj2 as CalcReference; int row = reference.GetRow(0); int rowCount = reference.GetRowCount(0); int column = reference.GetColumn(0); int columnCount = reference.GetColumnCount(0); ICalcSource source2 = null; CalcReference reference2 = reference.GetSource(); // hdt MethodInfo info = reference2.GetType().GetRuntimeMethod("GetContext", null); if (info != null) { source2 = info.Invoke(reference2, null) as ICalcSource; } if (source2 == null) { source2 = worksheet; } CalcExternalRangeExpression expression6 = CreateExternalRangeExpressionByCount(source2, row, column, rowCount, columnCount, false, false, false, false); nodes.Add(expression6); } } } else if (root is CalcReferenceExpression) { nodes.Add(root as CalcReferenceExpression); } }
public virtual CalcExpression Visit(CalcExpression expr, int baseRow, int baseColumn) { CalcConstantExpression expression = expr as CalcConstantExpression; if (expression != null) { return(this.VisitConstantExpression(expression)); } CalcCellExpression expression2 = expr as CalcCellExpression; if (expression2 != null) { return(this.VisitCellExpression(expression2, baseRow, baseColumn)); } CalcRangeExpression expression3 = expr as CalcRangeExpression; if (expression3 != null) { return(this.VisitRangeExpression(expression3, baseRow, baseColumn)); } CalcUnaryOperatorExpression expression4 = expr as CalcUnaryOperatorExpression; if (expression4 != null) { return(this.VisitUnaryOperatorExpression(expression4, baseRow, baseColumn)); } CalcBinaryOperatorExpression expression5 = expr as CalcBinaryOperatorExpression; if (expression5 != null) { return(this.VisitBinaryOperatorExpression(expression5, baseRow, baseColumn)); } CalcFunctionExpression expression6 = expr as CalcFunctionExpression; if (expression6 != null) { return(this.VisitFunctionExpression(expression6, baseRow, baseColumn)); } CalcExternalCellExpression expression7 = expr as CalcExternalCellExpression; if (expression7 != null) { return(this.VisitExternalCellExpression(expression7, baseRow, baseColumn)); } CalcExternalRangeExpression expression8 = expr as CalcExternalRangeExpression; if (expression8 != null) { return(this.VisitExternalRangeExpression(expression8, baseRow, baseColumn)); } if (expr is CalcSharedExpression) { CalcExpression expression9 = (expr as CalcSharedExpression).Expression; CalcExpression expression10 = this.Visit(expression9, baseRow, baseColumn); if (expression10 == expression9) { return(expr); } return(new CalcSharedExpression(expression10)); } CalcParenthesesExpression expression11 = expr as CalcParenthesesExpression; if (expression11 != null) { return(this.VisitParenthesesExpression(expression11, baseRow, baseColumn)); } CalcNameExpression expression12 = expr as CalcNameExpression; if (expression12 != null) { return(this.VisitNameExpression(expression12, baseRow, baseColumn)); } CalcExternalNameExpression expression13 = expr as CalcExternalNameExpression; if (expression13 != null) { return(this.VisitExternalNameExpression(expression13, baseRow, baseColumn)); } CalcSheetRangeExpression expression14 = expr as CalcSheetRangeExpression; if (expression14 != null) { return(this.VisitSheetRangeExpression(expression14, baseRow, baseColumn)); } return(expr); }
protected virtual CalcExpression VisitExternalNameExpression(CalcExternalNameExpression expr, int baseRow, int baseColumn) { return(expr); }