การทำงานกับความคิดเห็น
ลองออนไลน์
คุณสามารถลองใช้ฟังก์ชันนี้กับ คำอธิบายประกอบการลบออนไลน์ฟรี ของเราได้
Aspose.Words อนุญาตให้ผู้ใช้ทำงานกับความคิดเห็น - ความคิดเห็นในเอกสารใน Aspose.Words จะแสดงโดยคลาส Comment ใช้คลาส CommentRangeStart และ CommentRangeEnd เพื่อระบุขอบเขตของข้อความที่ควรเชื่อมโยงกับความคิดเห็น
เพิ่มความคิดเห็น
Aspose.Words ช่วยให้คุณสามารถเพิ่มความคิดเห็นได้หลายวิธี:
- การใช้คลาส Comment
- การใช้คลาส CommentRangeStart และ CommentRangeEnd
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเพิ่มความคิดเห็นในย่อหน้าโดยใช้คลาส Comment:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithComments(); | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.Write("Some text is added."); | |
Comment comment = new Comment(doc, "Awais Hafeez", "AH", DateTime.Today); | |
builder.CurrentParagraph.AppendChild(comment); | |
comment.Paragraphs.Add(new Paragraph(doc)); | |
comment.FirstParagraph.Runs.Add(new Run(doc, "Comment text.")); | |
dataDir = dataDir + "Comments_out.doc"; | |
// Save the document. | |
doc.Save(dataDir); |
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเพิ่มความคิดเห็นในย่อหน้าโดยใช้ขอบเขตข้อความและคลาส CommentRangeStart และ CommentRangeEnd:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithComments(); | |
Document doc = new Document(); | |
Paragraph para1 = new Paragraph(doc); | |
Run run1 = new Run(doc, "Some "); | |
Run run2 = new Run(doc, "text "); | |
para1.AppendChild(run1); | |
para1.AppendChild(run2); | |
doc.FirstSection.Body.AppendChild(para1); | |
Paragraph para2 = new Paragraph(doc); | |
Run run3 = new Run(doc, "is "); | |
Run run4 = new Run(doc, "added "); | |
para2.AppendChild(run3); | |
para2.AppendChild(run4); | |
doc.FirstSection.Body.AppendChild(para2); | |
Comment comment = new Comment(doc, "Awais Hafeez", "AH", DateTime.Today); | |
comment.Paragraphs.Add(new Paragraph(doc)); | |
comment.FirstParagraph.Runs.Add(new Run(doc, "Comment text.")); | |
CommentRangeStart commentRangeStart = new CommentRangeStart(doc, comment.Id); | |
CommentRangeEnd commentRangeEnd = new CommentRangeEnd(doc, comment.Id); | |
run1.ParentNode.InsertAfter(commentRangeStart, run1); | |
run3.ParentNode.InsertAfter(commentRangeEnd, run3); | |
commentRangeEnd.ParentNode.InsertAfter(comment, commentRangeEnd); | |
dataDir = dataDir + "Anchor.Comment_out.doc"; | |
// Save the document. | |
doc.Save(dataDir); |
แยกหรือลบความคิดเห็น
การใช้ความคิดเห็นในเอกสาร Word (นอกเหนือจากการติดตามการเปลี่ยนแปลง) เป็นวิธีปฏิบัติทั่วไปเมื่อตรวจสอบเอกสาร โดยเฉพาะอย่างยิ่งเมื่อมีผู้ตรวจสอบหลายคน อาจมีสถานการณ์ที่สิ่งเดียวที่คุณต้องการจากเอกสารคือความคิดเห็น สมมติว่าคุณต้องการสร้างรายการผลการตรวจสอบ หรือบางทีคุณอาจรวบรวมข้อมูลที่เป็นประโยชน์ทั้งหมดจากเอกสาร และคุณเพียงต้องการลบความคิดเห็นที่ไม่จำเป็นออก คุณอาจต้องการดูหรือลบความคิดเห็นของผู้วิจารณ์รายใดรายหนึ่ง
ในตัวอย่างนี้ เราจะดูวิธีการง่ายๆ สำหรับทั้งการรวบรวมข้อมูลจากความคิดเห็นภายในเอกสารและการลบความคิดเห็นออกจากเอกสาร โดยเฉพาะเราจะกล่าวถึงวิธีการ:
- แยกความคิดเห็นทั้งหมดออกจากเอกสารหรือเฉพาะความคิดเห็นของผู้เขียนคนใดคนหนึ่งเท่านั้น
- ลบความคิดเห็นทั้งหมดออกจากเอกสารหรือเฉพาะจากผู้เขียนคนใดคนหนึ่งเท่านั้น
วิธีแยกหรือลบความคิดเห็น
จริงๆ แล้วโค้ดในตัวอย่างนี้ค่อนข้างเรียบง่าย และวิธีการทั้งหมดก็ใช้แนวทางเดียวกัน ความคิดเห็นในเอกสาร Word จะแสดงด้วยวัตถุ Comment ในรูปแบบวัตถุเอกสาร Aspose.Words หากต้องการรวบรวมความคิดเห็นทั้งหมดในเอกสาร ให้ใช้วิธี GetChildNodes โดยตั้งค่าพารามิเตอร์แรกเป็น NodeType.Comment ตรวจสอบให้แน่ใจว่าพารามิเตอร์ตัวที่สองของวิธี GetChildNodes ถูกตั้งค่าเป็น true ซึ่งจะบังคับให้ GetChildNodes เลือกจากโหนดย่อยทั้งหมดแบบวนซ้ำ แทนที่จะรวบรวมเฉพาะโหนดย่อยที่อยู่ติดกัน
เพื่ออธิบายวิธีการแยกและลบความคิดเห็นออกจากเอกสาร เราจะทำตามขั้นตอนต่อไปนี้:
- เปิดเอกสาร Word โดยใช้คลาส Document
- รับความคิดเห็นทั้งหมดจากเอกสารลงในคอลเลกชัน
- หากต้องการแยกความคิดเห็น:
- ดำเนินการคอลเลกชันโดยใช้ตัวดำเนินการ foreach
- แยกและระบุชื่อผู้เขียน วันที่และเวลา และข้อความของความคิดเห็นทั้งหมด
- แยกและระบุชื่อผู้เขียน วันที่ & เวลา และข้อความความคิดเห็นที่เขียนโดยผู้เขียนเฉพาะเจาะจง ในกรณีนี้คือผู้เขียน ‘ks’
- หากต้องการลบความคิดเห็น:
- ย้อนกลับผ่านคอลเลกชันโดยใช้ตัวดำเนินการ for
- ลบความคิดเห็น
- บันทึกการเปลี่ยนแปลง
วิธีแยกความคิดเห็นทั้งหมด
วิธีการ GetChildNodes มีประโยชน์มากและคุณสามารถใช้ทุกครั้งที่คุณต้องการรับรายการโหนดเอกสารประเภทใดก็ได้ คอลเลกชันผลลัพธ์ไม่ได้สร้างค่าใช้จ่ายทันที เนื่องจากโหนดจะถูกเลือกในคอลเลกชันนี้เฉพาะเมื่อคุณระบุหรือเข้าถึงรายการในนั้นเท่านั้น
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแยกชื่อผู้เขียน วันที่และเวลา และข้อความของความคิดเห็นทั้งหมดในเอกสาร:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
static ArrayList ExtractComments(Document doc) | |
{ | |
ArrayList collectedComments = new ArrayList(); | |
// Collect all comments in the document | |
NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true); | |
// Look through all comments and gather information about them. | |
foreach (Comment comment in comments) | |
{ | |
collectedComments.Add(comment.Author + " " + comment.DateTime + " " + comment.ToString(SaveFormat.Text)); | |
} | |
return collectedComments; | |
} |
วิธีแยกความคิดเห็นของผู้เขียนที่ระบุ
หลังจากที่คุณเลือกโหนด Comment ลงในคอลเลกชันแล้ว สิ่งที่คุณต้องทำคือดึงข้อมูลที่คุณต้องการ ในตัวอย่างนี้ ชื่อย่อของผู้เขียน วันที่ เวลา และข้อความธรรมดาของความคิดเห็นจะรวมกันเป็นสตริงเดียว คุณสามารถเลือกจัดเก็บด้วยวิธีอื่นแทนได้
วิธีการโอเวอร์โหลดที่แยกความคิดเห็นจากผู้เขียนคนใดคนหนึ่งนั้นแทบจะเหมือนกัน เพียงตรวจสอบชื่อผู้เขียนก่อนที่จะเพิ่มข้อมูลลงในอาร์เรย์
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแยกชื่อผู้เขียน วันที่และเวลา และข้อความความคิดเห็นโดยผู้เขียนที่ระบุ:
วิธีลบความคิดเห็น
หากคุณกำลังลบความคิดเห็นทั้งหมด ไม่จำเป็นต้องเลื่อนดูคอลเลกชันเพื่อลบความคิดเห็นทีละรายการ คุณสามารถลบออกได้โดยการเรียกวิธี Clear ในคอลเลกชันความคิดเห็น
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการลบความคิดเห็นทั้งหมดในเอกสาร:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
static void RemoveComments(Document doc) | |
{ | |
// Collect all comments in the document | |
NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true); | |
// Remove all comments. | |
comments.Clear(); | |
} |
เมื่อคุณต้องการเลือกลบความคิดเห็น กระบวนการจะคล้ายกับโค้ดที่เราใช้ในการแยกความคิดเห็นมากขึ้น
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการลบความคิดเห็นโดยผู้เขียนที่ระบุ:
ประเด็นหลักที่ต้องเน้นที่นี่คือการใช้ตัวดำเนินการ for ต่างจากการแยกข้อมูลแบบธรรมดาตรงที่คุณต้องการลบความคิดเห็นที่นี่ เคล็ดลับที่เหมาะสมคือการวนซ้ำคอลเลกชันย้อนหลังจากความคิดเห็นล่าสุดไปยังความคิดเห็นแรก สาเหตุนี้หากคุณเริ่มต้นจากจุดสิ้นสุดและย้อนกลับ ดัชนีของรายการก่อนหน้ายังคงไม่เปลี่ยนแปลง และคุณสามารถย้อนกลับไปที่รายการแรกในคอลเลกชันได้
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแยกและลบความคิดเห็น:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithComments(); | |
string fileName = "TestFile.doc"; | |
// Open the document. | |
Document doc = new Document(dataDir + fileName); | |
// Extract the information about the comments of all the authors. | |
foreach (string comment in ExtractComments(doc)) | |
Console.Write(comment); | |
// Remove comments by the "pm" author. | |
RemoveComments(doc, "pm"); | |
Console.WriteLine("Comments from \"pm\" are removed!"); | |
// Extract the information about the comments of the "ks" author. | |
foreach (string comment in ExtractComments(doc, "ks")) | |
Console.Write(comment); | |
//Read the comment's reply and resolve them. | |
CommentResolvedandReplies(doc); | |
// Remove all comments. | |
RemoveComments(doc); | |
Console.WriteLine("All comments are removed!"); | |
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName); | |
// Save the document. | |
doc.Save(dataDir); |
วิธีลบความคิดเห็นระหว่าง 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-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithComments(); | |
string fileName = "TestFile.doc"; | |
// Open the document. | |
Document doc = new Document(dataDir + fileName); | |
CommentRangeStart commentStart = (CommentRangeStart)doc.GetChild(NodeType.CommentRangeStart, 0, true); | |
CommentRangeEnd commentEnd = (CommentRangeEnd)doc.GetChild(NodeType.CommentRangeEnd, 0, true); | |
Node currentNode = commentStart; | |
Boolean isRemoving = true; | |
while (currentNode != null && isRemoving) | |
{ | |
if (currentNode.NodeType == NodeType.CommentRangeEnd) | |
isRemoving = false; | |
Node nextNode = currentNode.NextPreOrder(doc); | |
currentNode.Remove(); | |
currentNode = nextNode; | |
} | |
dataDir = dataDir + "RemoveRegionText_out.doc"; | |
// Save the document. | |
doc.Save(dataDir); |
เพิ่มหรือลบการตอบกลับของความคิดเห็น
วิธีการ AddReply เพิ่มการตอบกลับความคิดเห็นนี้ โปรดทราบว่าเนื่องจากข้อจำกัดของ Microsoft Office ที่มีอยู่ อนุญาตให้ตอบกลับได้เพียง 1 ระดับในเอกสารเท่านั้น ข้อยกเว้นประเภท InvalidOperationException จะถูกยกขึ้นหากมีการเรียกใช้เมธอดนี้ในความคิดเห็น Reply ที่มีอยู่
คุณสามารถใช้วิธี RemoveReply เพื่อลบการตอบกลับที่ระบุสำหรับความคิดเห็นนี้
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการเพิ่มการตอบกลับไปยังความคิดเห็นและลบการตอบกลับของความคิดเห็น:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(dataDir + "TestFile.doc"); | |
Comment comment = (Comment)doc.GetChild(NodeType.Comment, 0, true); | |
//Remove the reply | |
comment.RemoveReply(comment.Replies[0]); | |
//Add a reply to comment | |
comment.AddReply("John Doe", "JD", new DateTime(2017, 9, 25, 12, 15, 0), "New reply"); | |
dataDir = dataDir + "TestFile_Out.doc"; | |
// Save the document to disk. | |
doc.Save(dataDir); |
อ่านคำตอบของความคิดเห็น
คุณสมบัติ Replies ส่งคืนคอลเลกชันของวัตถุ Comment ที่เป็นลูกของความคิดเห็นที่ระบุ
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการวนซ้ำผ่านการตอบกลับของความคิดเห็นและแก้ไข:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
static void CommentResolvedandReplies(Document doc) | |
{ | |
NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true); | |
Comment parentComment = (Comment)comments[0]; | |
foreach (Comment childComment in parentComment.Replies) | |
{ | |
// Get comment parent and status. | |
Console.WriteLine(childComment.Ancestor.Id); | |
Console.WriteLine(childComment.Done); | |
// And update comment Done mark. | |
childComment.Done = true; | |
} | |
} |