public static IEnumerable <string> SummonNames(CellDS cds) { //this overly complicated statement returns all nonempty cell names return((cds.getCells()).Select(c => c.contents is string?((string)c.contents == "" ? "" : c.name) : c.name).Except(new HashSet <string>() { "" })); }
/// Creates a Spreadsheet that is a duplicate of the spreadsheet saved in source. /// /// See the AbstractSpreadsheet.Save method and Spreadsheet.xsd for the file format /// specification. /// /// If there's a problem reading source, throws an IOException. /// /// Else if the contents of source are not consistent with the schema in Spreadsheet.xsd, /// throws a SpreadsheetReadException. /// /// Else if the IsValid string contained in source is not a valid C# regular expression, throws /// a SpreadsheetReadException. (If the exception is not thrown, this regex is referred to /// below as oldIsValid.) /// /// Else if there is a duplicate cell name in the source, throws a SpreadsheetReadException. /// (Two cell names are duplicates if they are identical after being converted to upper case.) /// /// Else if there is an invalid cell name or an invalid formula in the source, throws a /// SpreadsheetReadException. (Use oldIsValid in place of IsValid in the definition of /// cell name validity.) /// /// Else if there is an invalid cell name or an invalid formula in the source, throws a /// SpreadsheetVersionException. (Use newIsValid in place of IsValid in the definition of /// cell name validity.) /// /// Else if there's a formula that causes a circular dependency, throws a SpreadsheetReadException. /// /// Else, create a Spreadsheet that is a duplicate of the one encoded in source except that /// the new Spreadsheet's IsValid regular expression should be newIsValid. public Spreadsheet(TextReader source, Regex newIsValid) { cds = new CellDS(); LinkedList <string> names = new LinkedList <string>(); LinkedList <string> contentsList = new LinkedList <string>(); using (var xmr = XmlReader.Create(source)) { while (xmr.Read()) { if (xmr.IsStartElement()) { switch (xmr.Name) { case "spreadsheet": IsValid = xmr["IsValid"]; if (IsValid == null) { throw new SpreadsheetReadException("is valid didnt read properly"); } break; case "cell": var temp = xmr["name"]; if (names.Contains(temp)) { throw new SpreadsheetReadException("duplicite cell names"); } try { if (!Regex.IsMatch(temp, IsValid)) { throw new SpreadsheetReadException("source file unreadable"); } } catch (ArgumentException e) { throw new SpreadsheetReadException(e.Message); } names.AddLast(temp); if (temp == null) { throw new SpreadsheetReadException("a name didnt read properly"); } //deal with the contents string form = xmr["contents"]; if (form == null) { throw new SpreadsheetReadException("contents didnt read properly"); } if (form.Substring(0, 1).Equals("=")) { try { new Formula(form.Substring(1), (s => s), checkIfValidName); } catch (FormulaFormatException e) { throw new SpreadsheetReadException("source file unreadable, bad formula"); } } contentsList.AddLast(form); break; default: throw new IOException(); } } } } IsValid = newIsValid.ToString(); int i = 0; try { foreach (string s in names) { SetContentsOfCell(s, contentsList.ElementAt(i)); i++; } } catch (Exception) { throw new SpreadsheetVersionException("bad regex on new file"); } if (names.Count != contentsList.Count) { throw new SpreadsheetVersionException("uneven data matches"); } }
/// <summary> /// Creates an empty Spreadsheet whose IsValid regular expression accepts every string. /// </summary> /// <param name="isValid">The is valid.</param> public Spreadsheet(Regex isValid) { cds = new CellDS(); this.IsValid = isValid.ToString(); }
//dont forget to add the other constructors. /// <summary> /// Initializes a new instance of the <see cref="Spreadsheet"/> class. /// </summary> public Spreadsheet() { cds = new CellDS(); IsValid = "[A-Za-z]([A-Za-z][1-9]|[1-9][0-9]|[1-9]$)[0-9]*"; }