/// <summary>
        /// Removes the comment
        /// </summary>
        /// <param name="comment">The comment to remove</param>
        public void Remove(ExcelComment comment)
        {
            ulong id = ExcelAddress.GetCellID(Worksheet.SheetID, comment.Range._fromRow, comment.Range._fromCol);
            //int ix=_comments.IndexOf(id);
            int          i = -1;
            ExcelComment c = null;

            if (Worksheet._commentsStore.Exists(comment.Range._fromRow, comment.Range._fromCol, ref i))
            {
                c = _list[i];
            }
            if (comment == c)
            {
                comment.TopNode.ParentNode.RemoveChild(comment.TopNode);                               //Remove VML
                comment._commentHelper.TopNode.ParentNode.RemoveChild(comment._commentHelper.TopNode); //Remove Comment

                Worksheet.VmlDrawingsComments._drawings.Delete(id);
                _list.RemoveAt(i);
                Worksheet._commentsStore.Delete(comment.Range._fromRow, comment.Range._fromCol, 1, 1, false);   //Issue 15549, Comments should not be shifted
                var ci = new CellsStoreEnumerator <int>(Worksheet._commentsStore);
                while (ci.Next())
                {
                    if (ci.Value > i)
                    {
                        ci.Value -= 1;
                    }
                }
            }
            else
            {
                throw (new ArgumentException("Comment does not exist in the worksheet"));
            }
        }
        /// <summary>
        /// Adds a comment to the top left cell of the range
        /// </summary>
        /// <param name="cell">The cell</param>
        /// <param name="Text">The comment text</param>
        /// <param name="author">Author</param>
        /// <returns>The comment</returns>
        public ExcelComment Add(ExcelRangeBase cell, string Text, string author)
        {
            var elem = CommentXml.CreateElement("comment", ExcelPackage.schemaMain);
            int ix   = _comments.IndexOf(ExcelAddress.GetCellID(Worksheet.SheetID, cell._fromRow, cell._fromCol));

            //Make sure the nodes come on order.
            if (ix < 0 && (~ix < _comments.Count))
            {
                ix = ~ix;
                var preComment = _comments[ix] as ExcelComment;
                preComment._commentHelper.TopNode.ParentNode.InsertBefore(elem, preComment._commentHelper.TopNode);
            }
            else
            {
                CommentXml.SelectSingleNode("d:comments/d:commentList", NameSpaceManager).AppendChild(elem);
            }
            elem.SetAttribute("ref", cell.Start.Address);
            ExcelComment comment = new ExcelComment(NameSpaceManager, elem, cell);

            comment.RichText.Add(Text);
            if (author != "")
            {
                comment.Author = author;
            }
            _comments.Add(comment);
            return(comment);
        }
Beispiel #3
0
        internal ExcelComment(XmlNamespaceManager ns, XmlNode commentTopNode, ExcelRangeBase cell)
            : base(null, cell, cell.Worksheet.VmlDrawingsComments.NameSpaceManager)
        {
            //_commentHelper = new XmlHelper(ns, commentTopNode);
            _commentHelper = XmlHelperFactory.Create(ns, commentTopNode);
            var textElem = commentTopNode.SelectSingleNode("d:text", ns);

            if (textElem == null)
            {
                textElem = commentTopNode.OwnerDocument.CreateElement("text", ExcelPackage.schemaMain);
                commentTopNode.AppendChild(textElem);
            }
            if (!cell.Worksheet._vmlDrawings.ContainsKey(ExcelAddress.GetCellID(cell.Worksheet.SheetID, cell.Start.Row, cell.Start.Column)))
            {
                cell.Worksheet._vmlDrawings.Add(cell);
            }

            TopNode  = cell.Worksheet.VmlDrawingsComments[ExcelCellBase.GetCellID(cell.Worksheet.SheetID, cell.Start.Row, cell.Start.Column)].TopNode;
            RichText = new ExcelRichTextCollection(ns, textElem);
            var tNode = textElem.SelectSingleNode("d:t", ns);

            if (tNode != null)
            {
                _text = tNode.InnerText;
            }
        }
        /// <summary>
        /// Removes the comment
        /// </summary>
        /// <param name="comment">The comment to remove</param>
        public void Remove(ExcelComment comment)
        {
            ulong id = ExcelAddress.GetCellID(Worksheet.SheetID, comment.Range._fromRow, comment.Range._fromCol);
            int   ix = _comments.IndexOf(id);

            if (ix >= 0 && comment == _comments[ix])
            {
                comment.TopNode.ParentNode.RemoveChild(comment.TopNode);                               //Remove VML
                comment._commentHelper.TopNode.ParentNode.RemoveChild(comment._commentHelper.TopNode); //Remove Comment

                Worksheet.VmlDrawingsComments._drawings.Delete(id);
                _comments.Delete(id);
            }
            else
            {
                throw (new ArgumentException("Comment does not exist in the worksheet"));
            }
        }