Exemplo n.º 1
0
        /// <summary>
        /// Name a cell, and replace all references to it with the name
        /// </summary>
        /// <exception cref="AggregateException">If any cells could not be inlined, with as innerexceptions the individual errors.</exception>
        public void Refactor(ExcelRaw.Range toName, string name)
        {
            ParseTreeNode parse;

            try
            {
                parse = Helper.Parse(name);
            }
            catch (ArgumentException)
            {
                // Parse error
                throw new ArgumentException($"Name {name} is not a valid name for a named range");
            }
            parse = parse.SkipToRelevant(true);
            if (!parse.Is(GrammarNames.NamedRange))
            {
                throw new ArgumentException($"Name {name} is not a valid name for a named range, because Excel interpets it as a {parse.Type()}");
            }

            // Set the name
            var Scope = toName.Worksheet;
            var Names = Scope.Names;

            ExcelRaw.Name newName = Names.Add(name, toName);

            Marshal.ReleaseComObject(newName);
            Marshal.ReleaseComObject(Names);
            Marshal.ReleaseComObject(Scope);

            // Perform refactoring
            var ctx = toName.CreateContext();

            Inline(toName, ctx, ctx.Parse(name));
        }