internal void AddCommentFromXml(XmlElement copyFromElement) { var xmlNode = ThreadedCommentsXml.CreateElement("threadedComment", ExcelPackage.schemaThreadedComments); ThreadedCommentsXml.SelectSingleNode("tc:ThreadedComments", Worksheet.NameSpaceManager).AppendChild(xmlNode); foreach (XmlAttribute attr in copyFromElement.Attributes) { if (attr.LocalName == "ref") { xmlNode.SetAttribute("ref", CellAddress.Address); } else if (attr.LocalName == "id") { xmlNode.SetAttribute("id", ExcelThreadedComment.NewId()); } else { xmlNode.SetAttribute(attr.LocalName, attr.Value); } } xmlNode.InnerXml = copyFromElement.InnerXml; var tc = new ExcelThreadedComment(xmlNode, Worksheet.NameSpaceManager, Worksheet.Workbook, this); if (Comments.Count > 0) { tc.ParentId = Comments[0].Id; } foreach (var m in tc.Mentions) { m.MentionId = ExcelThreadedComment.NewId(); } AddComment(tc); }
internal ExcelThreadedComment AddComment(string personId, string text, bool replicateLegacyComment) { Require.That(text).Named("text").IsNotNullOrEmpty(); Require.That(personId).Named("personId").IsNotNullOrEmpty(); var parentId = string.Empty; if (Comments.Any()) { parentId = Comments.First().Id; } var xmlNode = ThreadedCommentsXml.CreateElement("threadedComment", ExcelPackage.schemaThreadedComments); ThreadedCommentsXml.SelectSingleNode("tc:ThreadedComments", Worksheet.NameSpaceManager).AppendChild(xmlNode); var newComment = new ExcelThreadedComment(xmlNode, Worksheet.NameSpaceManager, Worksheet.Workbook, this); newComment.Id = ExcelThreadedComment.NewId(); newComment.CellAddress = new ExcelCellAddress(CellAddress.Address); newComment.Text = text; newComment.PersonId = personId; newComment.DateCreated = DateTime.Now; if (!string.IsNullOrEmpty(parentId)) { newComment.ParentId = parentId; } Comments.Add(newComment); if (replicateLegacyComment) { ReplicateThreadToLegacyComment(); } return(newComment); }
private void AddCommentsFromXml() { foreach (XmlElement node in ThreadedCommentsXml.SelectNodes("tc:ThreadedComments/tc:threadedComment", _worksheet.NameSpaceManager)) { var comment = new ExcelThreadedComment(node, _worksheet.NameSpaceManager, _worksheet.Workbook); var cellAddress = comment.CellAddress; int i = -1; ExcelThreadedCommentThread thread; if (_worksheet._threadedCommentsStore.Exists(cellAddress.Row, cellAddress.Column, ref i)) { thread = _threads[_threadsIndex[i]]; } else { thread = new ExcelThreadedCommentThread(cellAddress, ThreadedCommentsXml, _worksheet); lock (_worksheet._threadedCommentsStore) { i = _threads.Count; _worksheet._threadedCommentsStore.SetValue(cellAddress.Row, cellAddress.Column, i); _threadsIndex.Add(i); _threads.Add(thread); } } comment.Thread = thread; thread.AddComment(comment); } }
/// <summary> /// Inserts mentions in the comment text and in the comment /// </summary> /// <param name="comment"></param> /// <param name="textWithFormats">A string with format placeholders with indexes, simlar to string.Format</param> /// <param name="personsToMention"><see cref="ExcelThreadedCommentPerson"/>s to mention</param> internal static void InsertMentions(ExcelThreadedComment comment, string textWithFormats, params ExcelThreadedCommentPerson[] personsToMention) { var str = textWithFormats; var isMentioned = new Dictionary <string, bool>(); for (var index = 0; index < personsToMention.Length; index++) { var person = personsToMention[index]; var format = "{" + index + "}"; while (str.IndexOf(format) > -1) { var placeHolderPos = str.IndexOf("{" + index + "}"); var regex = new Regex(@"\{" + index + @"\}"); str = regex.Replace(str, "@" + person.DisplayName, 1); // Excel seems to only support one mention per person, so we // add a mention object only for the first occurance per person... if (!isMentioned.ContainsKey(person.Id)) { comment.Mentions.AddMention(person, placeHolderPos); isMentioned[person.Id] = true; } } } comment.Mentions.SortAndAddMentionsToXml(); comment.Text = str; }
/// <summary> /// Removes a <see cref="ExcelThreadedComment"/> from the thread. /// </summary> /// <param name="comment">The comment to remove</param> /// <returns>true if the comment was removed, otherwise false</returns> public bool Remove(ExcelThreadedComment comment) { if (Comments.Remove(comment)) { ReplicateThreadToLegacyComment(); return(true); } return(false); }
internal void AddComment(ExcelThreadedComment comment) { Comments.Add(comment); ReplicateThreadToLegacyComment(); }