Exemple #1
0
        /// <summary>
        /// A helper method that helps open a spreadsheet
        /// </summary>
        /// <Citation> https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.openfiledialog.openfile?view=netcore-3.1 </Citation>
        private void OpenSpreadsheet()
        {
            //A string to get the contents of the file
            string fileContent = null;

            //Initializes the openFileDialog
            OpenFileDialog openFileDialog = new OpenFileDialog();

            //Has two options in the openFileDialog (spreadsheet files and all files)
            openFileDialog.Filter = "Spreadsheet files (*.sprd)|*.sprd|All files (*.*)|*.*";

            //Keeps spreadsheet files as the default
            openFileDialog.FilterIndex      = 1;
            openFileDialog.RestoreDirectory = true;

            //If the user saves the file, then goes in here
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                //Get the path of specified file
                String filePath = openFileDialog.FileName;

                //Initializes a new spreadsheet using that filePath
                spreadsheet = new Spreadsheet(filePath, s => Regex.IsMatch(s, @"^([A-Z])[1-9][0-9]?$"), s => s.ToUpper(), "ps6");

                //Clears the grid, the contents and value text box
                spreadsheetPanel.Clear();
                cellContentsTextBox.Clear();
                cellValueTextBox.Clear();

                //Iterates through the cells in the spreadsheet we just opened and adds them to the current spreadsheet
                foreach (string cellName in spreadsheet.GetNamesOfAllNonemptyCells())
                {
                    //Gets the first character of the cellName
                    char firstChar = cellName[0];

                    //Parses the rest of the cellName and subtracts that parsed num by 1, because we are using 0
                    //based row and col to get the row
                    int tempRow = int.Parse(cellName.Substring(1)) - 1;

                    //Subtracts the firstChar by A to get the col because we are using 0 based rows and col to get the col
                    int tempCol = firstChar - 'A';

                    //Sets the value in the associated grid spot in the spreadsheet based on the col and row
                    spreadsheetPanel.SetValue(tempCol, tempRow, spreadsheet.GetCellValue(cellName).ToString());

                    //Sets the selection for the col and row in the spreadsheet
                    spreadsheetPanel.SetSelection(tempCol, tempRow);

                    //Sets the contents of the spreadsheet in the textBox based on the contents of the cellName associated
                    cellContentsTextBox.Text = spreadsheet.GetCellContents(cellName).ToString();

                    //If the cellName is a formula, then it appends a "=" in front of the contents of the cell
                    if (spreadsheet.GetCellContents(cellName) is Formula f)
                    {
                        cellContentsTextBox.Text = "=" + spreadsheet.GetCellContents(cellName).ToString();
                    }
                    //Sets the value of the spreadsheet in the textBox based on the value of the cellName associated
                    cellValueTextBox.Text = spreadsheet.GetCellValue(cellName).ToString();

                    //If the cellValue is a FormulaError, then it displays formulaError in as the value
                    if (spreadsheet.GetCellValue(cellName) is FormulaError fe)
                    {
                        cellValueTextBox.Text = "Formula Error!";
                    }
                }
                //Sets the text of the cellName as it is highlighted by getting the selection of the cell, add the column to 'A'
                //to account for the column num adding one to it as our panel is 0 based to get the row, and setting that as the
                //name of the cell in the text box
                int col;
                int row;
                spreadsheetPanel.GetSelection(out col, out row);
                int  rowNum = row + 1;
                char column = (char)('A' + Convert.ToChar(col));
                cellNameTextBox.Text = column.ToString() + rowNum.ToString() + "";


                //Reads the file and sets it as the fileContents
                Stream fileStream = openFileDialog.OpenFile();
                using (StreamReader reader = new StreamReader(fileStream))
                {
                    fileContent = reader.ReadToEnd();
                }
            }

            //Displays the contents of the file
            MessageBox.Show(fileContent, "Spreadsheet Opened was like so: ", MessageBoxButtons.OK);
        }
 /// <summary>
 /// Updates the cells in the view for all nonempty cells in the spreadsheet.
 /// </summary>
 private void UpdateCells()
 {
     UpdateCells(Spreadsheet.GetNamesOfAllNonemptyCells());
 }
Exemple #3
0
        /// <summary>
        /// This method is run when the 'Open' button is clicked.  The method opens
        /// a saved spreadsheet from a file.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void openMenuItem_Click(object sender, EventArgs e)
        {
            // Referenced MSDN library for Filter syntax
            openFileDialog1.Filter     = "Spreadsheet Files (*.sprd)|*.sprd|All Files (*.*)|*.*";
            openFileDialog1.DefaultExt = ".sprd";
            openFileDialog1.FileName   = "";
            openFileDialog1.Title      = "Open";
            openFileDialog1.ShowDialog();

            // get the name of the selected file
            string openname = openFileDialog1.FileName;

            // if the file name is empty, give a message and return
            if (openname == "")
            {
                return;
            }

            // if the user selected option 1 (needs to be .sprd) then if the selected
            // filename does not end with .sprd, tell it to add the default extension
            if (openFileDialog1.FilterIndex == 1)
            {
                openFileDialog1.AddExtension = true;
            }

            // if the spreadsheet has been changed, make sure the user wants to overwrite changes
            if (spreadsheet.Changed)
            {
                DialogResult result = MessageBox.Show("Your spreadsheet has unsaved changed. Opening " +
                                                      "this file will overwrite any changes that have not been saved. Open anyways?",
                                                      "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

                // if 'No' then we don't want to open the file so return
                if (result == DialogResult.No)
                {
                    return;
                }
            }

            try // Try to load a spreadsheet from a file
            {
                // clear each cell value in the spreadsheet
                foreach (string s in spreadsheet.GetNamesOfAllNonemptyCells())
                {
                    int cellColIndex = GetColIndex(s);
                    int cellRowIndex = GetRowIndex(s);
                    spreadsheetPanel1.SetValue(cellColIndex, cellRowIndex, "");
                }

                // update spreadsheet with new file data
                spreadsheet = new Spreadsheet(openname, IsValidName, s => s.ToUpper(), "ps6");
                fileName    = openname; // initialize to the filepath where opened
                this.Text   = openname; // set title to filename
                UpdateSpreadsheet();    // update the tool boxes

                // Update each cell in the GUI to show the values in the spreadsheet
                foreach (string s in spreadsheet.GetNamesOfAllNonemptyCells())
                {
                    UpdateCell(s);
                }

                // Changed should now be false, so show that the spreadsheet has been saved
                if (!spreadsheet.Changed)
                {
                    toolStripStatusLabel1.Text = "";
                }
            }
            catch (SpreadsheetReadWriteException s)
            {
                MessageBox.Show("Error: There was a problem reading the selected file. Please"
                                + " make sure the file exists with a valid name and try again.", "Error Reading Spreadsheet", MessageBoxButtons.OK, MessageBoxIcon.Error); // display the message from the exception
            }

            // clear existing spreadsheet panel
            // loop through existing one and clear all non-empty cells
        }
Exemple #4
0
        /// <summary>
        /// This method is run when the 'Open' button is clicked.  The method opens
        /// a saved spreadsheet from a file.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void openMenuItem_Click(object sender, EventArgs e)
        {
            // Referenced MSDN library for Filter syntax
            openFileDialog1.Filter     = "Spreadsheet Files (*.sprd)|*.sprd|All Files (*.*)|*.*";
            openFileDialog1.DefaultExt = ".sprd";
            openFileDialog1.FileName   = "";
            openFileDialog1.Title      = "Open";
            openFileDialog1.ShowDialog();

            // get the name of the selected file
            string openname = openFileDialog1.FileName;

            if (openname == "")
            {
                return;
            }


            if (openFileDialog1.FilterIndex == 1)
            {
                openFileDialog1.AddExtension = true;
            }


            if (spreadsheet.Changed)
            {
                DialogResult result = MessageBox.Show("your spreadsheet is not saved do you want to overwrite existing file",
                                                      "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

                // if 'No' then we don't want to open the file so return
                if (result == DialogResult.No)
                {
                    return;
                }
            }

            try // Try to load a spreadsheet from a file
            {
                foreach (string s in spreadsheet.GetNamesOfAllNonemptyCells())
                {
                    int CellColumnIndex = GetColIndex(s);
                    int CellRowIndex    = GetRowIndex(s);
                    spreadsheetPanel1.SetValue(CellColumnIndex, CellRowIndex, "");
                }



                spreadsheet = new Spreadsheet(openname, variablechecker, s => s.ToUpper(), "ps6");
                filename    = openname; // initialize to the filepath where opened
                this.Text   = openname; // set title to filename
                SpreadsheetUpdater();   // update the tool boxes

                // Update each cell in the GUI to show the values in the spreadsheet
                foreach (string s in spreadsheet.GetNamesOfAllNonemptyCells())
                {
                    UpdateCell(s);
                }

                // Changed should now be false, so show that the spreadsheet has been saved
                if (!spreadsheet.Changed)
                {
                    toolStripStatusLabel1.Text = "";
                }
            }
            catch (SpreadsheetReadWriteException s)
            {
                MessageBox.Show("no such file exist, please select another one", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); // display the message from the exception
            }
        }