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

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-Python-via-.NET
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
builder.write("Some text is added.")
comment = aw.Comment(doc, "Awais Hafeez", "AH", datetime.today())
builder.current_paragraph.append_child(comment)
comment.paragraphs.add(aw.Paragraph(doc))
comment.first_paragraph.runs.add(aw.Run(doc, "Comment text."))
doc.save(docs_base.artifacts_dir + "WorkingWithComments.add_comments.docx")

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

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document()
para1 = aw.Paragraph(doc)
run1 = aw.Run(doc, "Some ")
run2 = aw.Run(doc, "text ")
para1.append_child(run1)
para1.append_child(run2)
doc.first_section.body.append_child(para1)
para2 = aw.Paragraph(doc)
run3 = aw.Run(doc, "is ")
run4 = aw.Run(doc, "added ")
para2.append_child(run3)
para2.append_child(run4)
doc.first_section.body.append_child(para2)
comment = aw.Comment(doc, "Awais Hafeez", "AH", datetime.today())
comment.paragraphs.add(aw.Paragraph(doc))
comment.first_paragraph.runs.add(aw.Run(doc, "Comment text."))
commentRangeStart = aw.CommentRangeStart(doc, comment.id)
commentRangeEnd = aw.CommentRangeEnd(doc, comment.id)
run1.parent_node.insert_after(commentRangeStart, run1)
run3.parent_node.insert_after(commentRangeEnd, run3)
commentRangeEnd.parent_node.insert_after(comment, commentRangeEnd)
doc.save(docs_base.artifacts_dir + "WorkingWithComments.anchor_comment.doc")

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

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

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

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

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

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

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

  1. เปิดเอกสาร Word โดยใช้คลาส Document
  2. รับความคิดเห็นทั้งหมดจากเอกสารลงในคอลเลกชัน
  3. หากต้องการแยกความคิดเห็น:
    1. ดำเนินการคอลเลกชันโดยใช้ตัวดำเนินการ foreach
    2. แยกและระบุชื่อผู้เขียน วันที่และเวลา และข้อความของความคิดเห็นทั้งหมด
    3. แยกและระบุชื่อผู้เขียน วันที่ & เวลา และข้อความความคิดเห็นที่เขียนโดยผู้เขียนเฉพาะเจาะจง ในกรณีนี้คือผู้เขียน ‘ks’
  4. หากต้องการลบความคิดเห็น:
    1. ย้อนกลับผ่านคอลเลกชันโดยใช้ตัวดำเนินการ for
    2. ลบความคิดเห็น
  5. บันทึกการเปลี่ยนแปลง

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

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

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

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
@staticmethod
def extract_comments(doc) :
collectedComments = []
comments = doc.get_child_nodes(aw.NodeType.COMMENT, True)
for node in comments :
comment = node.as_comment()
collectedComments.append(comment.author + " " + comment.date_time.strftime("%Y-%m-%d %H:%M:%S") + " " + comment.to_string(aw.SaveFormat.TEXT))
return collectedComments

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

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

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

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

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
@staticmethod
def extract_comments_by_author(doc, authorName) :
collectedComments = []
comments = doc.get_child_nodes(aw.NodeType.COMMENT, True)
for node in comments :
comment = node.as_comment()
if (comment.author == authorName) :
collectedComments.append(comment.author + " " + comment.date_time.strftime("%Y-%m-%d %H:%M:%S") + " " + comment.to_string(aw.SaveFormat.TEXT))
return collectedComments

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

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

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

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
@staticmethod
def remove_comments(doc) :
comments = doc.get_child_nodes(aw.NodeType.COMMENT, True)
comments.clear()

เมื่อคุณต้องการเลือกลบความคิดเห็น กระบวนการจะคล้ายกับโค้ดที่เราใช้ในการแยกความคิดเห็นมากขึ้น

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

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
@staticmethod
def remove_comments_by_author(doc, authorName) :
comments = doc.get_child_nodes(aw.NodeType.COMMENT, True)
# Look through all comments and remove those written by the authorName.
for i in range(comments.count, 0) :
print(i)
comment = comments[i].as_comment()
if (comment.author == authorName) :
comment.remove()

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

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

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "Comments.docx")
# Extract the information about the comments of all the authors.
for comment in self.extract_comments(doc) :
print(comment)
# Remove comments by the "pm" author.
self.remove_comments_by_author(doc, "pm")
print("Comments from \"pm\" are removed!")
# Extract the information about the comments of the "ks" author.
for comment in self.extract_comments_by_author(doc, "ks") :
print(comment)
# Read the comment's reply and resolve them.
self.comment_resolved_and_replies(doc)
# Remove all comments.
self.remove_comments(doc)
print("All comments are removed!")
doc.save(docs_base.artifacts_dir + "WorkingWithComments.process_comments.docx")

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

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

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

# Open the document.
doc = aw.Document(docs_base.my_dir + "Comments.docx")

commentStart = doc.get_child(aw.NodeType.COMMENT_RANGE_START, 0, True).as_comment_range_start()
commentEnd = doc.get_child(aw.NodeType.COMMENT_RANGE_END, 0, True).as_comment_range_end()

currentNode = commentStart
isRemoving = True
while (currentNode != None and isRemoving) :
    if (currentNode.node_type == aw.NodeType.COMMENT_RANGE_END) :
        isRemoving = False

    nextNode = currentNode.next_pre_order(doc)
    currentNode.remove()
    currentNode = nextNode

# Save the document.
doc.save(docs_base.artifacts_dir + "WorkingWithComments.remove_region_text.docx")

เพิ่มหรือลบการตอบกลับของความคิดเห็น

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

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

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

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "Comments.docx")
comment = doc.get_child(aw.NodeType.COMMENT, 0, True).as_comment()
comment.remove_reply(comment.replies[0])
comment.add_reply("John Doe", "JD", datetime(2017, 9, 25, 12, 15, 0), "New reply")
doc.save(docs_base.artifacts_dir + "WorkingWithComments.add_remove_comment_reply.docx")

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

คุณสมบัติ replies ส่งคืนคอลเลกชันของวัตถุ Comment ที่เป็นลูกของความคิดเห็นที่ระบุ

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

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
@staticmethod
def comment_resolved_and_replies(doc) :
comments = doc.get_child_nodes(aw.NodeType.COMMENT, True)
parentComment = comments[0].as_comment()
for child in parentComment.replies :
childComment = child.as_comment()
# Get comment parent and status.
print(childComment.ancestor.id)
print(childComment.done)
# And update comment Done mark.
childComment.done = True