private void StoreFormula( IEnumerable <TableCell> cells, string expression, FormulaFormat format, int dplaces, TableSelectionRange rangeType, string tagIndex) { if (rangeType == TableSelectionRange.Single) { var cell = cells.First(); cell.SetMeta("omfx", new Formula(format, dplaces, expression).ToString()); if (!string.IsNullOrEmpty(tagIndex)) { cell.SetTag(tagIndex); } return; } var regex = new Regex(@"([a-zA-Z]{1,3})(\d{1,3})"); int offset = 0; foreach (var cell in cells) { var builder = new StringBuilder(expression); if (offset > 0) { var matches = regex.Matches(expression); foreach (Match match in matches) { string col; string row; if (rangeType == TableSelectionRange.Columns) { col = TableCell.IndexToLetters( TableCell.LettersToIndex(match.Groups[1].Value) + offset); row = match.Groups[2].Value; } else { col = match.Groups[1].Value; row = (int.Parse(match.Groups[2].Value) + offset).ToString(); } builder.Replace(match.Value, $"{col}{row}", match.Index, match.Length); } } cell.SetMeta("omfx", new Formula(format, dplaces, builder.ToString()).ToString()); if (!string.IsNullOrEmpty(tagIndex)) { cell.SetTag(tagIndex); } offset++; } }
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = /// <summary> /// Initialize and new formula with the given properties. Used for formula creation /// </summary> /// <param name="format"></param> /// <param name="dplaces"></param> /// <param name="expression"></param> public Formula(FormulaFormat format, int dplaces, string expression) { Version = CurrentVersion; Format = format; DecimalPlaces = dplaces; Expression = expression; Valid = true; }