Пример #1
0
 private static void updateUserData(CIQUser user)
 {
     foreach (var column in user.Columns)
     {
         for (var i = 0; i < column.CellDatas.Count; i++)
         {
             var cell = user.GetCell(column.ColumnLetter, i);
             if (!string.IsNullOrEmpty(cell.Formula))
             {
                 cell.Data = processCell(user, cell);
             }
         }
     }
 }
Пример #2
0
        private static string processCell(CIQUser user, CellData cell)
        {
            if (cell.Formula[0] != '=')
            {
                return(cell.Formula);
            }

            var delimiters = new char[] { '=', '+', '-', '/', '*' };
            var tokens     = cell.Formula.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
            var sum        = 0f;

            // Get operator tokens
            var operators = new List <char>();

            foreach (var c in cell.Formula)
            {
                if (c == '+' || c == '-' || c == '=' || c == '/')
                {
                    operators.Add(c);
                }
            }

            // Process cell index tokens
            for (var i = 0; i < tokens.Length; i++)
            {
                var token          = tokens[i];
                var column         = token[0];
                var indexStr       = token.Substring(1);
                var index          = int.Parse(indexStr);
                var additionalCell = user.GetCell(column, index);
                var valueParse     = float.TryParse(additionalCell.Data, out var cellValue);
                if (!valueParse)
                {
                    return("N/A");
                }
                if (i == 0)
                {
                    sum = cellValue;
                }
                else
                {
                    if (operators[i] == '+')
                    {
                        sum += cellValue;
                    }
                    else if (operators[i] == '-')
                    {
                        sum -= cellValue;
                    }
                    else if (operators[i] == '*')
                    {
                        sum *= cellValue;
                    }
                    else if (operators[i] == '/')
                    {
                        sum /= cellValue;
                    }
                    else
                    {
                        return("N/A");
                    }
                }
            }

            return(sum.ToString());
        }