/// <summary>
        /// Method for handling the opening of files when the Open button is clicked.
        /// When a file is opened, all previous data in the current spreadsheet is lost and replaced with the data from the file being opened.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Create new OpenFileDialog object
            OpenFileDialog openFile = new OpenFileDialog();

            // Set filter for which files can be shown
            openFile.Filter = "Spreadsheet File (*.ss)|*.ss|All Files |*.*";
            // Set default filter to .ss files
            openFile.FilterIndex = 1;
            openFile.Multiselect = false;

            // Try to open the file
            try
            {
                // If Open has been selected
                if (openFile.ShowDialog() == DialogResult.OK)
                {
                    // Read in the file
                    StreamReader reader = new StreamReader(openFile.FileName);
                    // Replace the old spreadSheet object with the new one created by the file opener
                    this.spreadSheet = new Spreadsheet(reader);
                    // Clear the spreadsheet panel to get the old cells off of it
                    spreadsheetPanel1.Clear();
                    // Update the fileName in the instance variable
                    currentFileName = openFile.FileName;

                    // Add all of the cells in the spreadsheet into the spreadsheet panel
                    IEnumerable <String> cells = spreadSheet.GetNamesOfAllNonemptyCells();
                    foreach (String cellName in cells)
                    {
                        // Parse cell's row and column information
                        String parsedCol = cellName.Substring(0, 1);
                        String parsedRow = cellName.Substring(1, cellName.Length - 1);

                        Char parsedChar = parsedCol[0];
                        int  col        = (int)parsedChar - 65;
                        int  row        = Convert.ToInt32(parsedRow) - 1;

                        // Add cell into the spreadsheet panel
                        spreadsheetPanel1.SetValue(col, row, spreadSheet.GetCellValue(cellName).ToString());

                        // Set the values for the default selected cell A1
                        cellContentsValue.Text = spreadSheet.GetCellContents("A1").ToString();
                        cellValueBox.Text      = spreadSheet.GetCellValue("A1").ToString();
                        spreadsheetPanel1.SetSelection(0, 0);
                    }

                    // Close the reader
                    reader.Close();
                }
            }
            // Catch any SpreadsheetReadExceptions and show the exception in a dialog box
            catch (SpreadsheetReadException rexception)
            {
                MessageBox.Show(rexception.Message);
                closeToolStripMenuItem_Click(sender, e);
            }
        }
Пример #2
0
 public void Formula1(AbstractSpreadsheet ss)
 {
     Set(ss, "a1", "= a2 + a3");
     Set(ss, "a2", "= b1 + b2");
     Assert.IsInstanceOfType(ss.GetCellValue("a1"), typeof(FormulaError));
     Assert.IsInstanceOfType(ss.GetCellValue("a2"), typeof(FormulaError));
     Set(ss, "a3", "5.0");
     Set(ss, "b1", "2.0");
     Set(ss, "b2", "3.0");
     VV(ss, "a1", 10.0, "a2", 5.0);
     Set(ss, "b2", "4.0");
     VV(ss, "a1", 11.0, "a2", 6.0);
 }
Пример #3
0
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        // Verifies cells and their values, which must alternate.
        public void VV(AbstractSpreadsheet sheet, params object[] constraints)
        {
            for (int i = 0; i < constraints.Length; i += 2)
            {
                if (constraints[i + 1] is double)
                {
                    Assert.AreEqual((double)constraints[i + 1], (double)sheet.GetCellValue((string)constraints[i]), 1e-9);
                }
                else
                {
                    Assert.AreEqual(constraints[i + 1], sheet.GetCellValue((string)constraints[i]));
                }
            }
        }
Пример #4
0
 // Verifies cells and their values, which must alternate.
 public void VV(AbstractSpreadsheet sheet, params object[] constraints)
 {
     for (int i = 0; i < constraints.Length; i += 2)
     {
         if (constraints[i + 1] is double)
         {
             Assert.AreEqual((double)constraints[i + 1], (double)sheet.GetCellValue((string)constraints[i]), 1e-9);
         }
         else
         {
             Assert.AreEqual(constraints[i + 1], sheet.GetCellValue((string)constraints[i]));
         }
     }
 }
Пример #5
0
 public void DivisionByZero1(AbstractSpreadsheet ss)
 {
     Set(ss, "A1", "4.1");
     Set(ss, "B1", "0.0");
     Set(ss, "C1", "= A1 / B1");
     Assert.IsInstanceOfType(ss.GetCellValue("C1"), typeof(FormulaError));
 }
Пример #6
0
 public void StringArgument(AbstractSpreadsheet ss)
 {
     Set(ss, "A1", "4.1");
     Set(ss, "B1", "hello");
     Set(ss, "C1", "= A1 + B1");
     Assert.IsInstanceOfType(ss.GetCellValue("C1"), typeof(FormulaError));
 }
Пример #7
0
 public void DivisionByZero1(AbstractSpreadsheet ss)
 {
     Set(ss, "A1", "4.1");
     Set(ss, "B1", "0.0");
     Set(ss, "C1", "= A1 / B1");
     Assert.IsInstanceOfType(ss.GetCellValue("C1"), typeof(FormulaError));
 }
Пример #8
0
        /// <summary>
        /// Called when a cell on the spreadsheet is selected.
        /// </summary>
        /// <param name="ss">Spreadsheet that called the method.</param>
        private void cellSelected(SpreadsheetPanel ss)
        {
            int row, col;

            ss.GetSelection(out col, out row);
            String _cellName = "" + ((char)('A' + col)) + (row + 1);

            if (shiftPressed) //If shift is pressed we are only adding the cell name to the contents box.
            {
                int selectionStart = contentsBox.SelectionStart;
                contentsBox.Text = contentsBox.Text.Insert(selectionStart, _cellName);
                contentsBox.Select(selectionStart + _cellName.Length, 0);
                ss.SetSelection(previousCellX, previousCellY);
                return;
            }
            if (contentButton.Enabled) // If we click away submit what we've got if there has been a change.
            {
                contentButton_Click(null, null);
            }
            previousCellX = col;
            previousCellY = row;
            cellName.Text = _cellName;
            String contents = ourSpreadsheet.GetCellContents(_cellName).ToString();
            String value    = ourSpreadsheet.GetCellValue(_cellName).ToString();

            contentsBox.Text = contents;
            previousContents = contents;
            if (value == "SpreadsheetUtilities.FormulaError")
            {
                value = "!FORMULA_ERROR"; //Change the name to something more readable
                if (heroMode)             // HERO MODE Stuff
                {
                    HeroModeReset();
                }
            }
            toolTip.SetToolTip(valueBox, value); //Set the tooltip to be the full value
            if (value.Length > 16)               // Trim it down to fit in the contents box
            {
                value = value.Substring(0, 15);
            }
            valueBox.Text = value;

            //Pretty Colorized stuff
            //colorizeDependencies(_cellName);

            contentButton.Enabled = false;//Keep the contents button disabled
        }
        /// <summary>
        /// Helper method for updating the cell's value boxes when the selection changes
        /// This method correctly sets the value textboxes: cellNameValue, columnValue, rowValue, cellContentsValue, and the cellValueBox textboxes
        /// </summary>
        /// <param name="sheet"></param>
        private void displaySelection(SpreadsheetPanel sheet)
        {
            // Create variables for holding the row, column, and cell values
            int    row;
            int    col;
            String value;

            // Get the current selection
            sheet.GetSelection(out col, out row);
            // Get the cell's value once the selection is obtained
            sheet.GetValue(col, row, out value);

            // Add ascii offset to the column number
            int asciiCol = col + 65;

            // Convert the ascii value into a char and set it as the columnValue text box
            columnValue.Text = Char.ConvertFromUtf32(asciiCol);

            // Add 1 to the row number since rows and columns starting index is 0
            int actualRow = row + 1;

            // Convert row number to a string and store it in the rowValue text box
            rowValue.Text = actualRow.ToString();

            // Combine the column and row values as a string and set them as the cellNameValue text box
            cellNameValue.Text = Char.ConvertFromUtf32(asciiCol) + actualRow.ToString();

            // Get the contents of the cell and convert it to a string
            String cellContents = spreadSheet.GetCellContents(cellNameValue.Text).ToString();

            // Set the cell's contents in the cellContentsValue text box
            cellContentsValue.Text = cellContents;

            // Get the vlaue of the cell and convert it to a string
            String cellValue = spreadSheet.GetCellValue(cellNameValue.Text).ToString();

            // Set the clel's value in the cellValueBox textbox
            cellValueBox.Text = cellValue;
        }
Пример #10
0
 public void StringArgument(AbstractSpreadsheet ss)
 {
     Set(ss, "A1", "4.1");
     Set(ss, "B1", "hello");
     Set(ss, "C1", "= A1 + B1");
     Assert.IsInstanceOfType(ss.GetCellValue("C1"), typeof(FormulaError));
 }
Пример #11
0
 public void Formula1(AbstractSpreadsheet ss)
 {
     Set(ss, "a1", "= a2 + a3");
     Set(ss, "a2", "= b1 + b2");
     Assert.IsInstanceOfType(ss.GetCellValue("a1"), typeof(FormulaError));
     Assert.IsInstanceOfType(ss.GetCellValue("a2"), typeof(FormulaError));
     Set(ss, "a3", "5.0");
     Set(ss, "b1", "2.0");
     Set(ss, "b2", "3.0");
     VV(ss, "a1", 10.0, "a2", 5.0);
     Set(ss, "b2", "4.0");
     VV(ss, "a1", 11.0, "a2", 6.0);
 }
Пример #12
0
        /// <summary>
        /// deals with open a new file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {

            //check if orignal file has been changed if so ask if they want to save.
            String message = "Would you like to save before closing?";
            var result1 = MessageBox.Show(message, "Closing", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result1 == DialogResult.Yes)
                saveFile();
           
            
            //With help from www.dotnetperls.com
            //show dialog
            DialogResult result = openFileDialog1.ShowDialog();
            //check result
            if (result == DialogResult.OK)
            {
                string fileName = openFileDialog1.FileName;
                
                try
                {
                    //make new spreadsheet
                    SSModel = new Spreadsheet(fileName, s => Regex.IsMatch(s, @"^[a-zA-Z][1-9][0-9]?$"), s => s.ToUpper(), "ps6");
                    //fill in the cells in the GUI
                    foreach (String cellName in SSModel.GetNamesOfAllNonemptyCells())
                    {
                        setRowAndCol(cellName);
                        spreadsheetPanel1.SetValue(col, row, SSModel.GetCellValue(cellName).ToString());
                    }

                    //set up the spreadsheet
                    spreadsheetPanel1.SetSelection(0, 0);
                    displayCurrentCell(0, 0);

                }catch(Exception ex )
                {
                    MessageBox.Show("An error occured:\n "+ ex.Message);
                }
            }
        }
Пример #13
0
        private void SYNCEvent(String command)
        {
            //you need to create a spreadsheet here.
            ss = new Spreadsheet();
            String[] temp = CommandParser(command);
            ss.Version = temp[1];

            for (int i = 2; i < temp.Length && (i + 1) < temp.Length; i+=2)
            {
                String cell_name = temp[i];
                String cell_content = temp[i+1];
                ISet<string> CellToRecalculate = ss.SetContentsOfCell(cell_name, cell_content);

                //update the all the relative cell value on the spreadsheet panel
                //get the value of the namede cells, and convert to string
                object value = ss.GetCellValue(cell_name);
                string v;
                if (value is double)
                    v = (double)value + "";
                else if (value is string)
                    v = (string)value;
                else
                    v = "FormulaError";

                //set the value to the panel
                int col = cell_name.First() - 'A';
                int row = row = Int32.Parse(cell_name.Substring(1, cell_name.Length - 1)) - 1;
                spreadsheetPanel1.SetValue(col, row, v);

                //value of each dependent cell in the spreadsheet will be updated
                foreach (string s in CellToRecalculate)
                {
                    value = ss.GetCellValue(s);
                    col = s.First() - 'A';
                    row = Int32.Parse(s.Substring(1, s.Length - 1)) - 1;
                    spreadsheetPanel1.SetValue(col, row, "" + value);
                }
            }
        }