public void RecalcCellReferences(SheetChange sheetChange)
        {
            foreach (var i in this.Cells())
            {
                var c = i.Value;
                if (c.Formula != null)
                {
                    c.Formula.Text      = ExcelFormula.TranslateForSheetChange(c.Formula.Text, sheetChange, _wsheet.Name);
                    c.Formula.R1        = ExcelRange.TranslateForSheetChange(c.Formula.R1, sheetChange, _wsheet.Name);
                    c.Formula.R2        = ExcelRange.TranslateForSheetChange(c.Formula.R2, sheetChange, _wsheet.Name);
                    c.Formula.Reference = ExcelRange.TranslateForSheetChange(c.Formula.Reference, sheetChange, _wsheet.Name);
                }
            }

            // Adjust conditional formatting
            List <ConditionalFormatting> cfToRemoveList = new List <ConditionalFormatting>();

            foreach (var cf in this.GetElements <ConditionalFormatting>())
            {
                bool removeCf          = false;
                List <StringValue> lst = new List <StringValue>();
                foreach (var sqrefItem in cf.SequenceOfReferences.Items)
                {
                    string newRef = ExcelRange.TranslateForSheetChange(sqrefItem.Value, sheetChange, _wsheet.Name);
                    if (!newRef.StartsWith("#")) // no error
                    {
                        lst.Add(new StringValue(newRef));
                    }
                    else
                    {
                        cfToRemoveList.Add(cf);
                        removeCf = true;
                        break;
                    }
                }
                if (removeCf)
                {
                    break;
                }
                cf.SequenceOfReferences = new ListValue <StringValue>(lst);
                foreach (var f in cf.Descendants <Formula>())
                {
                    f.Text = ExcelFormula.TranslateForSheetChange(f.Text, sheetChange, _wsheet.Name);
                }
            }
            foreach (ConditionalFormatting cf in cfToRemoveList)
            {
                this.RemoveElement(cf);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Translate a fornula due to a sheet change, e.g. insertion of rows.
        /// </summary>
        /// <param name="formula">Formula, e.g. SUM(A1,Sheet2!B2)</param>
        /// <param name="sheetChange">Details of change</param>
        /// <param name="currentSheetName">The sheet where the range is, to determine if this range is affected. If sheetChange.SheetName is null and currentSheetName is null, translation is always applied.</param>
        /// <param name="currentSheetName">The sheet where the range is, to determine if this range is affected. If sheetChange.SheetName is null and currentSheetName is null, translation is always applied.</param>
        /// <returns></returns>
        public static string TranslateForSheetChange(string formula, SheetChange sheetChange, string currentSheetName)
        {
            if (formula == null)
            {
                return(null);
            }

            ParseTree     tree    = ExcelFormula.Parse(formula);
            StringBuilder rebuilt = new StringBuilder();

            if (tree.Errors.Count > 0)
            {
                throw new ArgumentException("Error in parsing formula");
            }
            BuildTranslated(rebuilt, tree,
                            n => TranslateRangeParseNodeForSheetChange(n, sheetChange, currentSheetName));
            return(rebuilt.ToString());
        }
Esempio n. 3
0
        /// <summary>
        /// Translate a formula.
        /// </summary>
        /// <param name="formula">Formula, e.g. SUM(A1,Sheet2!B2)</param>
        /// <param name="rowDelta">Number of rows to move up(+) or down(-)</param>
        /// <param name="colDelta">Number of columns to move right(+) or left(-)</param>
        /// <returns></returns>
        public static string Translate(string formula, int rowDelta, int colDelta)
        {
            if (formula == null)
            {
                return(null);
            }

            ParseTree     tree    = ExcelFormula.Parse(formula);
            StringBuilder rebuilt = new StringBuilder();

            if (tree.Errors.Count > 0)
            {
                throw new ArgumentException("Error in parsing formula");
            }
            BuildTranslated(rebuilt, tree,
                            n => TranslateRangeParseNodeWithOffset(n, rowDelta, colDelta));
            return(rebuilt.ToString());
        }