예제 #1
0
        /// <summary>
        /// this method evaluates the formula stored in the contents of the selected cell
        /// </summary>
        /// <param name="cellColumn"></param>
        /// <param name="cellRow"></param>
        /// <returns></returns>
        private object evaluateFormula(int cellColumn, int cellRow)
        {
            //storing the empty string
            object result = "";
            //getting the value of the previous cell
            bool passed = spreadsheetPanel1.GetValue(prevCol, prevRow, out string prevContent);
            //storing the previous contents
            object previousContent = prevContent;


            ISet <string> list = new HashSet <string>();

            //if the previous contents was a formula
            if (prevContent.StartsWith("="))
            {
                //surround in a try catch to catch different exceptions
                try
                {
                    //getting the dependent cells and putting them in a list
                    list = formSpreadsheet.SetContentsOfCell(asciiConverter(cellColumn, cellRow), prevContent);
                    //calculating the value of the selected cell
                    result = formSpreadsheet.calculateValue(asciiConverter(cellColumn, cellRow));
                    //evaluating all the cells in the given list
                    reEvaluateValues(list);
                }
                //if a circular exception is thrown
                catch (CircularException)
                {
                    int col;
                    int row;
                    spreadsheetPanel1.GetSelection(out col, out row);
                    //reset the cells value as an empty cell
                    spreadsheetPanel1.SetValue(col, row, "");
                    //showing the user the problem
                    MessageBox.Show("Circular Exception");
                }
                //if a formula format exception is thrown
                catch (SpreadsheetUtilities.FormulaFormatException)
                {
                    int col;
                    int row;
                    spreadsheetPanel1.GetSelection(out col, out row);
                    //reset the cells value as an empty cell
                    spreadsheetPanel1.SetValue(col, row, "");
                    //showing the user the problem
                    MessageBox.Show("Invalid Formula!!! Please Enter a valid Formula.");
                }
            }
            //return the result of the evaluated cell
            return(result);
        }