การทำงานกับความคิดเห็น

Aspose.Wordsอนุญาตให้ผู้ใช้สามารถทำงานกับความคิดเห็น-ความคิดเห็นในเอกสารในAspose.WordsจะแสดงโดยCommentชั้ ใช้ชั้นเรียนCommentRangeStartและCommentRangeEndเพื่อระบุพื้นที่ของข้อความที่ควรจะเชื่อมโยงกับข้อคิดเห็น.

เพิ่มความคิดเห็น

Aspose.Wordsช่วยให้คุณสามารถเพิ่มความคิดเห็นในหลายวิธี:

  1. การใช้Commentคลาส
  2. การใช้ชั้นเรียนCommentRangeStartและCommentRangeEnd

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการเพิ่มความคิดเห็นในย่อหน้าโดยใช้คลาสComment:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directory.
System::String outputDataDir = GetOutputDataDir_WorkingWithComments();
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
builder->Write(u"Some text is added.");
System::SharedPtr<Comment> comment = System::MakeObject<Comment>(doc, u"Awais Hafeez", u"AH", System::DateTime::get_Today());
builder->get_CurrentParagraph()->AppendChild(comment);
comment->get_Paragraphs()->Add(System::MakeObject<Paragraph>(doc));
comment->get_FirstParagraph()->get_Runs()->Add(System::MakeObject<Run>(doc, u"Comment text."));
System::String outputPath = outputDataDir + u"AddComments.doc";
// Save the document.
doc->Save(outputPath);

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการเพิ่มความคิดเห็นในย่อหน้าโดยใช้พื้นที่ของข้อความและCommentRangeStartและCommentRangeEndคลาส:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directory.
System::String outputDataDir = GetOutputDataDir_WorkingWithComments();
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<Paragraph> para1 = System::MakeObject<Paragraph>(doc);
System::SharedPtr<Run> run1 = System::MakeObject<Run>(doc, u"Some ");
System::SharedPtr<Run> run2 = System::MakeObject<Run>(doc, u"text ");
para1->AppendChild(run1);
para1->AppendChild(run2);
doc->get_FirstSection()->get_Body()->AppendChild(para1);
System::SharedPtr<Paragraph> para2 = System::MakeObject<Paragraph>(doc);
System::SharedPtr<Run> run3 = System::MakeObject<Run>(doc, u"is ");
System::SharedPtr<Run> run4 = System::MakeObject<Run>(doc, u"added ");
para2->AppendChild(run3);
para2->AppendChild(run4);
doc->get_FirstSection()->get_Body()->AppendChild(para2);
System::SharedPtr<Comment> comment = System::MakeObject<Comment>(doc, u"Awais Hafeez", u"AH", System::DateTime::get_Today());
comment->get_Paragraphs()->Add(System::MakeObject<Paragraph>(doc));
comment->get_FirstParagraph()->get_Runs()->Add(System::MakeObject<Run>(doc, u"Comment text."));
System::SharedPtr<CommentRangeStart> commentRangeStart = System::MakeObject<CommentRangeStart>(doc, comment->get_Id());
System::SharedPtr<CommentRangeEnd> commentRangeEnd = System::MakeObject<CommentRangeEnd>(doc, comment->get_Id());
run1->get_ParentNode()->InsertAfter(commentRangeStart, run1);
run3->get_ParentNode()->InsertAfter(commentRangeEnd, run3);
commentRangeEnd->get_ParentNode()->InsertAfter(comment, commentRangeEnd);
System::String outputPath = outputDataDir + u"AnchorComment.doc";
// Save the document.
doc->Save(outputPath);

แยกหรือลบความคิดเห็น

ใช้ความคิดเห็นในเอกสารคำ(นอกเหนือจากการติดตามการเปลี่ยนแปลง)เป็นวิธีปฏิบัติทั่วไ สามารถมีสถานการณ์ที่สิ่งเดียวที่คุณต้องการจากเอกสารเป็นความคิดเห็น. สมมติว่าคุณต้องการที่จะสร้างรายการของการค้นพบการตรวจสอบหรือบางทีคุณอาจจะได้ คุณอาจต้องการดูหรือลบความคิดเห็นของผู้ตรวจทานรายใดรายหนึ่ง.

ในตัวอย่างนี้,เราจะไปดูที่วิธีการบางอย่างง่ายสำหรับทั้งการรวบรวมข้อมูลจากความคิดเห็นภายในเอกสารและสำหรับการลบความคิดเห็นจากเอกสาร. โดยเฉพาะอย่างยิ่งเราจะครอบคลุมถึงวิธีการ:

  • สารสกัดจากความคิดเห็นทั้งหมดจากเอกสารหรือเฉพาะคนที่ทำโดยผู้เขียนโดยเฉพาะอย่.
  • ลบความคิดเห็นทั้งหมดจากเอกสารหรือจากผู้เขียนโดยเฉพาะอย่างยิ่ง.

วิธีการแยกหรือลบความคิดเห็น

รหัสในตัวอย่างนี้เป็นจริงค่อนข้างง่ายและวิธีการทั้งหมดจะขึ้นอยู่กับวิธีการเดียวกัน ความคิดเห็นในเอกสารคำจะแสดงโดยวัตถุCommentในรูปแบบวัตถุเอกสารAspose.Words ในการรวบรวมความคิดเห็นทั้งหมดในเอกสารให้ใช้วิธีการGetChildNodesกับพารามิเตอร์แรกที่ตั้งไว้ที่NodeType.Comment ตรวจสอบให้แน่ใจว่าพารามิเตอร์ที่สองของวิธีการGetChildNodesถูกตั้งค่าเป็นจริง:สิ่งนี้บังคับให้GetChildNodesเลือกจา.

เพื่อแสดงให้เห็นถึงวิธีการแยกและลบความคิดเห็นจากเอกสาร,เราจะไปผ่านขั้นตอนต่อ:

  1. เปิดเอกสารคำโดยใช้คลาสDocument
  2. รับความคิดเห็นทั้งหมดจากเอกสารลงในคอลเลกชัน
  3. การแยกความคิดเห็น:
    1. ไปผ่านคอลเลกชันโดยใช้ผู้ประกอบการฟอเรช
    2. สารสกัดจากและรายชื่อชื่อผู้เขียนวันที่และเวลาและข้อความของความคิดเห็นทั้งหมด
    3. สารสกัดจากและรายชื่อชื่อผู้เขียนวันที่และเวลาและข้อความแสดงความคิดเห็นที่เขียนโดย
  4. วิธีลบความคิดเห็น:
    1. ไปข้างหลังผ่านคอลเลกชันโดยใช้สำหรับผู้ประกอบการ
    2. ลบความคิดเห็น
  5. บันทึกการเปลี่ยนแปลง

วิธีการแยกความคิดเห็นทั้งหมด

วิธีGetChildNodesมีประโยชน์มากและคุณสามารถใช้มันทุกครั้งที่คุณต้องการที่จะได้รับรายชื่อของโหนดเ คอลเล็กชันผลลัพธ์ไม่ได้สร้างค่าใช้จ่ายในทันทีเนื่องจากโหนดถูกเลือกไว้ในคอลเล็กชันนี้เ.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแยกชื่อผู้เขียนวันที่และเวลาและข้อความของความคิดเห็นทั้งหมดในเอกสาร:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
std::vector<System::String> ExtractComments(const System::SharedPtr<Document>& doc)
{
std::vector<System::String> collectedComments;
// Collect all comments in the document
System::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true);
// Look through all comments and gather information about them.
for (System::SharedPtr<Comment> comment : System::IterateOver<System::SharedPtr<Comment>>(comments))
{
collectedComments.push_back(comment->get_Author() + u" " + comment->get_DateTime() + u" " + System::StaticCast<Node>(comment)->ToString(SaveFormat::Text));
}
return collectedComments;
}

วิธีการแยกความคิดเห็นของผู้เขียนที่ระบุ

หลังจากที่คุณได้เลือกโหนดความคิดเห็นลงในคอลเลกชันทั้งหมดที่คุณต้องทำคือการแยกข้ ในตัวอย่างนี้ผู้เขียนชื่อย่อวันที่เวลาและข้อความธรรมดาของข้อคิดเห็นจะรวมเป็นสตริงหนึ่.

วิธีการมากเกินไปที่แยกความคิดเห็นจากผู้เขียนโดยเฉพาะอย่างยิ่งเกือบจะเหมือนกันก็เ.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแยกชื่อผู้เขียนวันที่และเวลาและข้อความของความคิดเห็นโดยผู้เขียนที่ระบุ:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
std::vector<System::String> ExtractComments(const System::SharedPtr<Document>& doc, const System::String& authorName)
{
std::vector<System::String> collectedComments;
// Collect all comments in the document
System::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true);
// Look through all comments and gather information about those written by the authorName author.
for (System::SharedPtr<Comment> comment : System::IterateOver<System::SharedPtr<Comment>>(comments))
{
if (comment->get_Author() == authorName)
{
collectedComments.push_back(comment->get_Author() + u" " + comment->get_DateTime() + u" " + System::StaticCast<Node>(comment)->ToString(SaveFormat::Text));
}
}
return collectedComments;
}

วิธีการลบความคิดเห็น

หากคุณกำลังลบความคิดเห็นทั้งหมดไม่จำเป็นต้องย้ายผ่านคอลเล็กชันการลบความคิดเห็นทีละรายการคุณสามารถลบความคิดเห็นได้โดยโทรNodeCollection.Clearบนคอลเล็กชันความคิดเห็น.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการลบความคิดเห็นทั้งหมดในเอกสาร:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
void RemoveComments(const System::SharedPtr<Document>& doc)
{
// Collect all comments in the document
System::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true);
// Remove all comments.
comments->Clear();
}

เมื่อคุณจำเป็นต้องเลือกลบความคิดเห็น,กระบวนการจะกลายเป็นคล้ายกับรหัสที่เราใช้ส.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการลบความคิดเห็นโดยผู้เขียนที่ระบุ:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
void RemoveComments(const System::SharedPtr<Document>& doc, const System::String& authorName)
{
// Collect all comments in the document
System::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true);
// Look through all comments and remove those written by the authorName author.
for (int32_t i = comments->get_Count() - 1; i >= 0; i--)
{
System::SharedPtr<Comment> comment = System::DynamicCast<Comment>(comments->idx_get(i));
if (comment->get_Author() == authorName)
{
comment->Remove();
}
}
}

จุดหลักที่จะเน้นที่นี่คือการใช้สำหรับผู้ประกอบการ ซึ่งแตกต่างจากการสกัดง่ายที่นี่คุณต้องการที่จะลบความคิดเห็น เคล็ดลับที่เหมาะสมคือการย้ำคอลเลกชันย้อนหลังจากความคิดเห็นล่าสุดไปยังคนแรก. ย้ายไปข้างหลังดัชนีของรายการก่อนหน้านี้ยังคงไม่เปลี่ยนแปลงและคุณสามารถทำงานใน.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการสำหรับการแยกความคิดเห็นและการเอาออก:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directories.
System::String inputDataDir = GetInputDataDir_WorkingWithComments();
System::String outputDataDir = GetOutputDataDir_WorkingWithComments();
// Open the document.
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"TestFile.doc");
// Extract the information about the comments of all the authors.
for (System::String const &comment : ExtractComments(doc))
{
std::cout << comment.ToUtf8String();
}
// Remove comments by the "pm" author.
RemoveComments(doc, u"pm");
std::cout << "Comments from \"pm\" are removed!" << std::endl;
// Extract the information about the comments of the "ks" author.
for (System::String const &comment: ExtractComments(doc, u"ks"))
{
std::cout << comment.ToUtf8String();
}
//Read the comment's reply and resolve them.
CommentResolvedandReplies(doc);
// Remove all comments.
RemoveComments(doc);
std::cout << "All comments are removed!" << std::endl;
System::String outputPath = outputDataDir + u"ProcessComments.doc";
// Save the document.
doc->Save(outputPath);

วิธีลบความคิดเห็นระหว่างCommentRangeStartและCommentRangeEnd

ใช้Aspose.Wordsคุณยังสามารถลบความคิดเห็นระหว่างCommentRangeStartและCommentRangeEndโหนด.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการลบข้อความระหว่างCommentRangeStartและCommentRangeEnd:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directories.
System::String inputDataDir = GetInputDataDir_WorkingWithComments();
System::String outputDataDir = GetOutputDataDir_WorkingWithComments();
// Open the document.
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"TestFile.doc");
System::SharedPtr<CommentRangeStart> commentStart = System::DynamicCast<CommentRangeStart>(doc->GetChild(NodeType::CommentRangeStart, 0, true));
System::SharedPtr<CommentRangeEnd> commentEnd = System::DynamicCast<CommentRangeEnd>(doc->GetChild(NodeType::CommentRangeEnd, 0, true));
System::SharedPtr<Node> currentNode = commentStart;
bool isRemoving = true;
while (currentNode != nullptr && isRemoving)
{
if (currentNode->get_NodeType() == NodeType::CommentRangeEnd)
{
isRemoving = false;
}
System::SharedPtr<Node> nextNode = currentNode->NextPreOrder(doc);
currentNode->Remove();
currentNode = nextNode;
}
System::String outputPath = outputDataDir + u"RemoveRegionText.doc";
// Save the document.
doc->Save(outputPath);

เพิ่มและลบคำตอบของความคิดเห็น

วิธีการAddReplyเพิ่มการตอบกลับความคิดเห็นนี้ โปรดทราบว่าเนื่องจากข้อจำกัดของสำนักงานMicrosoftที่มีอยู่เท่านั้น1ระดับของการตอบกลับที่ได้รับอนุญาตในเอกสาร ข้อยกเว้นของชนิดInvalidOperationExceptionจะถูกยกขึ้นถ้าวิธีนี้ถูกเรียกใช้ในการตอบกลับความคิดเห็นที่มีอยู่.

คุณสามารถใช้วิธีการRemoveReplyเพื่อลบการตอบกลับที่ระบุในความคิดเห็นนี้.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการเพิ่มการตอบกลับความคิดเห็นและลบการตอบกลับของค:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directories.
System::String inputDataDir = GetInputDataDir_WorkingWithComments();
System::String outputDataDir = GetOutputDataDir_WorkingWithComments();
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"TestFile.doc");
System::SharedPtr<Comment> comment = System::DynamicCast<Comment>(doc->GetChild(NodeType::Comment, 0, true));
//Remove the reply
comment->RemoveReply(comment->get_Replies()->idx_get(0));
//Add a reply to comment
comment->AddReply(u"John Doe", u"JD", System::DateTime(2017, 9, 25, 12, 15, 0), u"New reply");
System::String outputPath = outputDataDir + u"CommentReply.doc";
// Save the document to disk.
doc->Save(outputPath);

อ่านความคิดเห็นตอบ

คุณสมบัติRepliesจะส่งคืนคอลเล็กชันของออบเจกต์Commentที่เป็นเด็กทันทีของข้อคิดเห็นที่ระบุ.

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการทำซ้ำผ่านการตอบกลับของความคิดเห็นและแก้ไขโค้ดเหล่:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
void CommentResolvedandReplies(const System::SharedPtr<Document>& doc)
{
System::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true);
System::SharedPtr<Comment> parentComment = System::DynamicCast<Comment>(comments->idx_get(0));
for (System::SharedPtr<Comment> childComment : System::IterateOver<System::SharedPtr<Comment>>(parentComment->get_Replies()))
{
// Get comment parent and status.
std::cout << childComment->get_Ancestor()->get_Id() << std::endl << childComment->get_Done() << std::endl;
// And update comment Done mark.
childComment->set_Done(true);
}
}