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>
        /// Removes the <see cref="ExcelThreadedCommentThread"/> supplied
        /// </summary>
        /// <param name="threadedComment">An existing <see cref="ExcelThreadedCommentThread"/> in the worksheet</param>
        public void Remove(ExcelThreadedCommentThread threadedComment)
        {
            int i = -1;
            ExcelThreadedCommentThread c = null;

            if (_worksheet._threadedCommentsStore.Exists(threadedComment.CellAddress.Row, threadedComment.CellAddress.Column, ref i))
            {
                c = _threads[i];
            }

            if (threadedComment == c)
            {
                var address = threadedComment.CellAddress;
                _worksheet.Comments.Remove(_worksheet.Comments[address]); //Remove the underlaying comment.
                var nodes = threadedComment.Comments.Select(x => x.TopNode);
                foreach (var node in nodes)
                {
                    node.ParentNode.RemoveChild(node); //Remove xml node
                }
                _worksheet._threadedCommentsStore.Delete(threadedComment.CellAddress.Row, threadedComment.CellAddress.Column, 1, 1, false);
                _threads[i] = null;
                _threadsIndex.Remove(i);
            }
            else
            {
                throw (new ArgumentException("Comment does not exist in the worksheet"));
            }
        }
Exemplo n.º 3
0
 internal ExcelThreadedComment(XmlNode topNode, XmlNamespaceManager namespaceManager, ExcelWorkbook workbook, ExcelThreadedCommentThread thread)
     : base(namespaceManager, topNode)
 {
     SchemaNodeOrder = new string[] { "text", "mentions" };
     _workbook       = workbook;
     _thread         = thread;
 }
        public ExcelThreadedCommentThread Add(ExcelCellAddress cellAddress)
        {
            Require.Argument(cellAddress).IsNotNull("cellAddress");
            if (_worksheet._threadedCommentsStore.Exists(cellAddress.Row, cellAddress.Column))
            {
                throw new ArgumentException("There is an existing threaded comment thread in cell " + cellAddress.Address);
            }
            if (_worksheet.Comments[cellAddress] != null)
            {
                throw new InvalidOperationException("There is an existing legacy comment/Note in this cell (" + cellAddress + "). See the Worksheet.Comments property. Legacy comments and threaded comments cannot reside in the same cell.");
            }
            if (ThreadedCommentsXml == null)
            {
                ThreadedCommentsXml = new XmlDocument();
                ThreadedCommentsXml.PreserveWhitespace = true;
                ThreadedCommentsXml.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?><ThreadedComments xmlns=\"http://schemas.microsoft.com/office/spreadsheetml/2018/threadedcomments\" xmlns:x=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\"/>");
            }
            var thread = new ExcelThreadedCommentThread(cellAddress, ThreadedCommentsXml, _worksheet);

            _worksheet._threadedCommentsStore.SetValue(cellAddress.Row, cellAddress.Column, _threads.Count);
            _threadsIndex.Add(_threads.Count);
            _threads.Add(thread);
            return(thread);
        }