Exemplo n.º 1
0
        /// <summary>
        /// load xml to spreadsheet.
        /// </summary>
        /// <param name="stream">stream.</param>
        public void TestReader(Stream stream)
        {
            XmlReaderSettings settings = new XmlReaderSettings();

            settings.Async = true;

            this.Undos.Clear();
            this.Redos.Clear();

            for (int i = 0; i < this.rowcount; i++)
            {
                for (int j = 0; j < this.columcount; j++)
                {
                    this.cell[i, j] = new Cell(i, j);
                    this.cell[i, j].PropertyChanged      += new PropertyChangedEventHandler(this.CellPropertyChanges);
                    this.cell[i, j].ColorPropertyChanged += new PropertyChangedEventHandler(this.ColorPropertyChanged);
                }
            }

            using (XmlReader reader = XmlReader.Create(stream, settings))
            {
                reader.ReadStartElement("Spreadsheet");
                while (reader.Name == "cell")
                {
                    reader.ReadStartElement("cell");
                    reader.ReadStartElement("Name");
                    string          name = reader.ReadContentAsString();
                    SpreadsheetCell cell = this.GetSingleValue(name);
                    reader.ReadEndElement();
                    if (reader.Name == "BgColor")
                    {
                        reader.ReadStartElement("BgColor");
                        uint color = Convert.ToUInt32(reader.ReadContentAsString());
                        cell.Color = color;
                        reader.ReadEndElement();
                    }

                    if (reader.Name == "Text")
                    {
                        reader.ReadStartElement("Text");
                        string text = reader.ReadContentAsString();
                        cell.Text = text;
                        reader.ReadEndElement();
                    }

                    reader.ReadEndElement();
                }

                reader.ReadEndElement();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// save xml to stream.
        /// </summary>
        /// <param name="stream">stream.</param>
        public void TestWriter(Stream stream)
        {
            XmlWriterSettings settings = new XmlWriterSettings();

            settings.Async = true;

            Dictionary <int, char> alp = new Dictionary <int, char>();
            int count = 0;

            for (char i = 'A'; i <= 'Z'; i++)
            {
                alp[count] = i;
                count++;
            }

            using (XmlWriter writer = XmlWriter.Create(stream, settings))
            {
                writer.WriteStartElement("Spreadsheet");
                for (int i = 0; i < this.rowcount; i++)
                {
                    for (int j = 0; j < this.columcount; j++)
                    {
                        SpreadsheetCell cell = this.GetCell(i, j);
                        string          name = alp[cell.ColumIndex].ToString() + (cell.RowIndex + 1).ToString();
                        if (cell.Value != string.Empty || cell.Color != 0xFFFFFFFF)
                        {
                            writer.WriteStartElement("cell");
                            writer.WriteStartElement("Name");
                            writer.WriteString(name);
                            writer.WriteEndElement();
                            writer.WriteStartElement("BgColor");
                            writer.WriteString(cell.Color.ToString());
                            writer.WriteEndElement();
                            writer.WriteStartElement("Text");
                            writer.WriteString(cell.Value);
                            writer.WriteEndElement();
                            writer.WriteEndElement();
                        }
                    }
                }

                writer.WriteEndElement();
                writer.Close();
            }
        }
Exemplo n.º 3
0
        public TextChange(ref SpreadsheetCell cell, string oldval, string newval)
        {
            this.cell = cell;
            if (oldval == null)
            {
                this.before = string.Empty;
            }
            else
            {
                this.before = oldval;
            }

            if (newval == null)
            {
                this.after = string.Empty;
            }
            else
            {
                this.after = newval;
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Set varible when the property is text.
 /// </summary>
 /// <param name="changeCell">cell need change.</param>
 /// <param name="prev">prev text.</param>
 /// <param name="cur">current text.</param>
 public void SetTextVariable(SpreadsheetCell changeCell, string prev, string cur)
 {
     this.cell     = changeCell;
     this.prevText = prev;
     this.curText  = cur;
 }
Exemplo n.º 5
0
        /// <summary>
        /// Loads a file given a string.
        /// </summary>
        /// <param name="filename">filename.</param>
        public void LoadFile(Stream file)
        {
            int         colcount = 26, rowcount = 50;
            XmlDocument doc = new XmlDocument();

            doc.Load(file);

            foreach (XmlNode node in doc.DocumentElement.Attributes)
            {
                switch (node.Name)
                {
                case "columns":
                    colcount = int.Parse(node.Value);
                    break;

                case "rows":
                    rowcount = int.Parse(node.Value);
                    break;
                }
            }

            this.Reconstruct(rowcount, colcount);

            SpreadsheetCell cell = this.GetCell("A1");

            foreach (XmlNode node in doc.DocumentElement)
            {
                if (node.Name == "cell")
                {
                    foreach (XmlAttribute attribute in node.Attributes) //get the name
                    {
                        if (attribute.Name == "name")
                        {
                            cell = this.GetCell(attribute.Value);
                            break;
                        }
                    }

                    foreach (XmlNode child in node.ChildNodes)
                    {
                        switch (child.Name)
                        {
                        case "bgcolor":
                            cell.BGColor = uint.Parse(child.InnerText);
                            break;

                        case "text":
                            cell.Text = child.InnerText;
                            break;
                        }
                    }
                }
            }



            /*
             * while (textReader.Read())
             * {
             *  if (textReader.Name == "spreadsheet")
             *  {
             *      for (int i = 0; i < textReader.AttributeCount; i++)
             *      {
             *          textReader.MoveToAttribute(i);
             *          switch (textReader.Name)
             *          {
             *              case "Colums":
             *                  int.TryParse(textReader.Value, out colcount);
             *                  break;
             *              case "Rows":
             *                  int.TryParse(textReader.Value, out rowcount);
             *                  break;
             *              default:
             *                  break;
             *          }
             *      }
             *
             *      this.Reconstruct(rowcount, colcount);
             *  }
             *
             *  if (textReader.Name == "Cell")
             *  {
             *      for (int i = 0; i < textReader.AttributeCount; i++)
             *      {
             *          textReader.MoveToAttribute(i);
             *          switch (textReader.Name)
             *          {
             *              case "Name":
             *                  int.TryParse(textReader.Value, out colcount);
             *                  break;
             *              case "Rows":
             *                  int.TryParse(textReader.Value, out rowcount);
             *                  break;
             *              default:
             *                  break;
             *          }
             *      }
             *
             *      this.Reconstruct(rowcount, colcount);
             *  }
             * }*/
        }
Exemplo n.º 6
0
 private bool IsDefaultCell(SpreadsheetCell cell)
 {
     return(cell.Text == string.Empty && cell.BGColor == 0xFFFFFFFF);
 }
Exemplo n.º 7
0
            /// <summary>
            /// update refrence property.
            /// </summary>
            /// <param name="sender">sender.</param>
            /// <param name="e">e.</param>
            private void RefrencePropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                SpreadsheetCell cell = (SpreadsheetCell)sender;

                this.Value = this.parentSpreadsheet.GetCell(cell.RowIndex, cell.ColumnIndex).Value;
            }
Exemplo n.º 8
0
        /// <summary>
        /// envent to change cell.
        /// </summary>
        /// <param name="sender">sender.</param>
        /// <param name="e">e.</param>
        private void CellPropertyChanges(object sender, PropertyChangedEventArgs e)
        {
            SpreadsheetCell spreadsheet = sender as SpreadsheetCell;

            Dictionary <int, char> alp = new Dictionary <int, char>();
            int count = 0;

            for (char i = 'A'; i <= 'Z'; i++)
            {
                alp[count] = i;
                count++;
            }

            string changeCell = alp[spreadsheet.ColumIndex].ToString() + (spreadsheet.RowIndex + 1).ToString();

            if (e.PropertyName == "Text")
            {
                if (spreadsheet.Text.StartsWith("=") == false)
                {
                    spreadsheet.Value = spreadsheet.Text;
                }
                else
                {
                    // if equal to the expreesion which need to cal by expression tree.
                    if (spreadsheet.Text.Length > 3)
                    {
                        string         expression = spreadsheet.Text.Substring(1);
                        ExpressionTree tree       = new ExpressionTree(expression);

                        List <string> strlist = tree.GetVaribles(expression);
                        foreach (string var in strlist)
                        {
                            SpreadsheetCell cell = this.GetSingleValue(var);
                            if (cell != null)
                            {
                                if (this.GetSingleValue(changeCell) == this.GetSingleValue(var))
                                {
                                    spreadsheet.Value = "!self reference";
                                }
                                else if (this.GetSingleValue(var).Value != "!self reference")
                                {
                                    double value = Convert.ToDouble(this.GetSingleValue(var).Value);
                                    tree.SetVariable(var, value);
                                    spreadsheet.Value = tree.Evaluate().ToString();
                                }
                                else
                                {
                                    spreadsheet.Value = Convert.ToString(0);
                                }

                                cell.PropertyChanged += spreadsheet.CellPropertyChanged;
                            }
                            else
                            {
                                spreadsheet.Value = "!bad reference";
                            }
                        }
                    }

                    // if = like B1,A1 just single cell value.
                    else
                    {
                        string          name = spreadsheet.Text.Substring(1);
                        SpreadsheetCell cell = this.GetSingleValue(name);

                        // cell will be null if catch FormatException case which return to null,if not, just normall to get value else will be bad reference.
                        if (cell != null)
                        {
                            // if equal self cell value
                            if (this.GetSingleValue(changeCell) == this.GetSingleValue(name))
                            {
                                spreadsheet.Value = "!self reference";
                            }

                            // if equal the cell value which is no self reference, else is equal zero.
                            else if (this.GetSingleValue(name).Value != "!self reference")
                            {
                                spreadsheet.Value = this.GetSingleValue(name).Value;
                            }
                            else
                            {
                                spreadsheet.Value = Convert.ToString(0);
                            }

                            cell.PropertyChanged += spreadsheet.CellPropertyChanged;
                        }
                        else
                        {
                            spreadsheet.Value = "!bad reference";
                        }
                    }
                }
            }

            this.CellPropertyChanged?.Invoke(sender, new PropertyChangedEventArgs("Value"));
        }
Exemplo n.º 9
0
 public void AddCell(ref SpreadsheetCell newcell, uint oldcolor, uint newcolor)
 {
     this.cells.Add(newcell);
     this.before.Add(oldcolor);
     this.after.Add(newcolor);
 }