ติดตามการเปลี่ยนแปลงในเอกสาร
ฟังก์ชันการทำงานของการติดตามการเปลี่ยนแปลงหรือที่เรียกว่าการตรวจสอบ ช่วยให้คุณสามารถติดตามการเปลี่ยนแปลงในเนื้อหาและการจัดรูปแบบที่คุณหรือผู้ใช้รายอื่นทำ คุณลักษณะการเปลี่ยนแปลงแทร็กนี้ด้วย Aspose.Words รองรับการเปลี่ยนแปลงแทร็กใน Microsoft Word ด้วยฟังก์ชันนี้ คุณสามารถเข้าถึงการแก้ไขแต่ละรายการในเอกสารของคุณ และใช้คุณสมบัติที่แตกต่างกันกับการแก้ไขเหล่านั้นได้
เมื่อคุณเปิดใช้งานคุณสมบัติการติดตามการเปลี่ยนแปลง องค์ประกอบที่แทรก ลบ และแก้ไขทั้งหมดของเอกสารจะถูกเน้นด้วยสายตาพร้อมข้อมูลเกี่ยวกับใคร เมื่อใด และสิ่งที่เปลี่ยนแปลง ออบเจ็กต์ที่มีข้อมูลเกี่ยวกับสิ่งที่เปลี่ยนแปลงเรียกว่า “การติดตามการเปลี่ยนแปลง” ตัวอย่างเช่น สมมติว่าคุณต้องการตรวจทานเอกสารและทำการเปลี่ยนแปลงที่สำคัญ ซึ่งอาจหมายความว่าคุณต้องทำการแก้ไข นอกจากนี้ คุณอาจต้องแทรกความคิดเห็นเพื่อหารือเกี่ยวกับการเปลี่ยนแปลงบางอย่าง นั่นคือที่มาของการติดตามการเปลี่ยนแปลงในเอกสาร
บทความนี้จะอธิบายวิธีจัดการและติดตามการเปลี่ยนแปลงที่สร้างโดยผู้ตรวจสอบจำนวนมากในเอกสารเดียวกัน รวมถึงคุณสมบัติสำหรับการติดตามการเปลี่ยนแปลง
การแก้ไขคืออะไร
ก่อนที่จะเจาะลึกการแก้ไข เรามาอธิบายความหมายของการแก้ไขกันก่อน Revision คือการเปลี่ยนแปลงที่เกิดขึ้นในโหนดหนึ่งของเอกสาร ในขณะที่กลุ่มการแก้ไขซึ่งแสดงโดยคลาส RevisionGroup คือกลุ่มของการแก้ไขตามลำดับที่เกิดขึ้นในหลายโหนดของเอกสาร โดยพื้นฐานแล้ว การแก้ไขเป็นเครื่องมือสำหรับการติดตามการเปลี่ยนแปลง
การแก้ไขจะใช้ในคุณลักษณะการติดตามการเปลี่ยนแปลงและภายในคุณลักษณะการเปรียบเทียบเอกสาร ซึ่งการแก้ไขจะปรากฏเป็นผลจากการเปรียบเทียบ ดังนั้น การแก้ไขภายในคุณลักษณะการติดตามการเปลี่ยนแปลงจะแสดงโดยใครและสิ่งที่เปลี่ยนแปลง
Aspose.Words รองรับการแก้ไขประเภทต่างๆ รวมถึงใน Microsoft Word เช่น การแทรก การลบ การเปลี่ยนรูปแบบ StyleDefinitionChange และการย้าย ประเภทการแก้ไขทั้งหมดจะแสดงด้วยการแจงนับ RevisionType
เริ่มต้นและหยุดการติดตามการเปลี่ยนแปลง
การแก้ไขเอกสารมักจะไม่นับเป็นการแก้ไขจนกว่าคุณจะเริ่มติดตาม Aspose.Words ช่วยให้คุณติดตามการเปลี่ยนแปลงทั้งหมดในเอกสารของคุณโดยอัตโนมัติด้วยขั้นตอนง่ายๆ คุณสามารถเริ่มกระบวนการติดตามการเปลี่ยนแปลงได้อย่างง่ายดายโดยใช้วิธี start_track_revisions หากคุณต้องการหยุดกระบวนการติดตามการเปลี่ยนแปลง เพื่อไม่ให้การแก้ไขใดๆ ในอนาคตถือเป็นการแก้ไข คุณจะต้องใช้วิธี stop_track_revisions
เมื่อสิ้นสุดกระบวนการติดตามการเปลี่ยนแปลงในเอกสารของคุณ คุณจะสามารถยอมรับการแก้ไขทั้งหมดหรือปฏิเสธการแก้ไขเพื่อเปลี่ยนเอกสารกลับเป็นรูปแบบเดิมได้ ซึ่งสามารถทำได้โดยใช้วิธี accept_all_revisions หรือ reject_all นอกจากนี้ คุณสามารถยอมรับหรือปฏิเสธการแก้ไขแต่ละรายการแยกกันได้โดยใช้วิธี accept หรือ reject
การเปลี่ยนแปลงทั้งหมดจะถูกติดตามสำหรับการวนซ้ำหนึ่งครั้งตั้งแต่วินาทีที่คุณเริ่มกระบวนการจนถึงช่วงเวลาที่คุณหยุด การเชื่อมต่อระหว่างการวนซ้ำต่างๆ จะแสดงเป็นสถานการณ์ต่อไปนี้: คุณดำเนินการตามกระบวนการติดตามให้เสร็จสิ้น จากนั้นทำการเปลี่ยนแปลงบางอย่าง และเริ่มติดตามการเปลี่ยนแปลงอีกครั้ง ด้วยสถานการณ์นี้ การเปลี่ยนแปลงทั้งหมดที่คุณไม่ยอมรับหรือปฏิเสธจะถูกแสดงอีกครั้ง
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการทำงานกับการติดตามการเปลี่ยนแปลง:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document() | |
body = doc.first_section.body | |
para = body.first_paragraph | |
# Add text to the first paragraph, then add two more paragraphs. | |
para.append_child(aw.Run(doc, "Paragraph 1. ")) | |
body.append_paragraph("Paragraph 2. ") | |
body.append_paragraph("Paragraph 3. ") | |
# We have three paragraphs, none of which registered as any type of revision | |
# If we add/remove any content in the document while tracking revisions, | |
# they will be displayed as such in the document and can be accepted/rejected. | |
doc.start_track_revisions("John Doe", datetime.today()) | |
# This paragraph is a revision and will have the according "IsInsertRevision" flag set. | |
para = body.append_paragraph("Paragraph 4. ") | |
self.assertTrue(para.is_insert_revision) | |
# Get the document's paragraph collection and remove a paragraph. | |
paragraphs = body.paragraphs | |
self.assertEqual(4, paragraphs.count) | |
para = paragraphs[2] | |
para.remove() | |
# Since we are tracking revisions, the paragraph still exists in the document, will have the "IsDeleteRevision" set | |
# and will be displayed as a revision in Microsoft Word, until we accept or reject all revisions. | |
self.assertEqual(4, paragraphs.count) | |
self.assertTrue(para.is_delete_revision) | |
# The delete revision paragraph is removed once we accept changes. | |
doc.accept_all_revisions() | |
self.assertEqual(3, paragraphs.count) | |
# Stopping the tracking of revisions makes this text appear as normal text. | |
# Revisions are not counted when the document is changed. | |
doc.stop_track_revisions() | |
# Save the document. | |
doc.save(docs_base.artifacts_dir + "WorkingWithRevisions.accept_revisions.docx") |
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการสร้างการแก้ไขเมื่อมีการย้ายโหนดภายในเอกสารที่ถูกติดตาม:
# 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.writeln("Paragraph 1") | |
builder.writeln("Paragraph 2") | |
builder.writeln("Paragraph 3") | |
builder.writeln("Paragraph 4") | |
builder.writeln("Paragraph 5") | |
builder.writeln("Paragraph 6") | |
body = doc.first_section.body | |
print(f"Paragraph count: {body.paragraphs.count}") | |
# Start tracking revisions. | |
doc.start_track_revisions("Author", datetime(2020, 12, 23, 14, 0, 0)) | |
# Generate revisions when moving a node from one location to another. | |
node = body.paragraphs[3] | |
endNode = body.paragraphs[5].next_sibling | |
referenceNode = body.paragraphs[0] | |
while (node != endNode) : | |
nextNode = node.next_sibling | |
body.insert_before(node, referenceNode) | |
node = nextNode | |
# Stop the process of tracking revisions. | |
doc.stop_track_revisions() | |
# There are 3 additional paragraphs in the move-from range. | |
print("Paragraph count: 0", body.paragraphs.count) | |
doc.save(docs_base.artifacts_dir + "WorkingWithRevisions.move_node_in_tracked_document.docx") |
จัดการและจัดเก็บการเปลี่ยนแปลงเป็นการแก้ไข
ด้วยคุณลักษณะการติดตามการเปลี่ยนแปลงก่อนหน้านี้ คุณสามารถเข้าใจได้ว่ามีการเปลี่ยนแปลงใดบ้างในเอกสารของคุณและใครเป็นผู้ทำการเปลี่ยนแปลงเหล่านั้น เมื่อใช้ฟีเจอร์ track_revisions คุณจะบังคับให้การเปลี่ยนแปลงใดๆ ภายในเอกสารถูกจัดเก็บเป็นการแก้ไข
Aspose.Words ช่วยให้คุณตรวจสอบว่าเอกสารมีการแก้ไขหรือไม่โดยใช้คุณสมบัติ has_revision หากคุณไม่ต้องการติดตามการเปลี่ยนแปลงในเอกสารของคุณโดยอัตโนมัติผ่านเมธอด start_track_revisions และ stop_track_revisions คุณสามารถใช้คุณสมบัติ track_revisions เพื่อตรวจสอบว่าการเปลี่ยนแปลงได้รับการติดตามในขณะที่แก้ไขเอกสารใน Microsoft Word และจัดเก็บเป็นการแก้ไขหรือไม่
คุณลักษณะ track_revisions ทำการแก้ไขแทนการเปลี่ยนแปลง DOM จริง แต่การแก้ไขนั้นแยกจากกัน ตัวอย่างเช่น หากคุณลบย่อหน้าใดๆ Aspose.Words จะทำให้เป็นการแก้ไข โดยทำเครื่องหมายว่าเป็นการลบ แทนที่จะลบออก
นอกจากนี้ Aspose.Words ยังช่วยให้คุณตรวจสอบว่าวัตถุถูกแทรก ลบ หรือเปลี่ยนการจัดรูปแบบโดยใช้คุณสมบัติ is_delete_revision, is_format_revision, is_insert_revision, is_move_from_revision และ is_move_to_revision หรือไม่
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการใช้คุณสมบัติที่แตกต่างกับการแก้ไข:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document() | |
# Insert an inline shape without tracking revisions. | |
self.assertFalse(doc.track_revisions) | |
shape = aw.drawing.Shape(doc, aw.drawing.ShapeType.CUBE) | |
shape.wrap_type = aw.drawing.WrapType.INLINE | |
shape.width = 100 | |
shape.height = 100 | |
doc.first_section.body.first_paragraph.append_child(shape) | |
# Start tracking revisions and then insert another shape. | |
doc.start_track_revisions("John Doe") | |
shape = aw.drawing.Shape(doc, aw.drawing.ShapeType.SUN) | |
shape.wrap_type = aw.drawing.WrapType.INLINE | |
shape.width = 100.0 | |
shape.height = 100.0 | |
doc.first_section.body.first_paragraph.append_child(shape) | |
# Get the document's shape collection which includes just the two shapes we added. | |
shapes = doc.get_child_nodes(aw.NodeType.SHAPE, True) | |
self.assertEqual(2, shapes.count) | |
# Remove the first shape. | |
shape0 = shapes[0].as_shape() | |
shape0.remove() | |
# Because we removed that shape while changes were being tracked, the shape counts as a delete revision. | |
self.assertEqual(aw.drawing.ShapeType.CUBE, shape0.shape_type) | |
self.assertTrue(shape0.is_delete_revision) | |
# And we inserted another shape while tracking changes, so that shape will count as an insert revision. | |
shape1 = shapes[1].as_shape() | |
self.assertEqual(aw.drawing.ShapeType.SUN, shape1.shape_type) | |
self.assertTrue(shape1.is_insert_revision) | |
# The document has one shape that was moved, but shape move revisions will have two instances of that shape. | |
# One will be the shape at its arrival destination and the other will be the shape at its original location. | |
doc = aw.Document(docs_base.my_dir + "Revision shape.docx") | |
shapes = doc.get_child_nodes(aw.NodeType.SHAPE, True) | |
self.assertEqual(2, shapes.count) | |
# This is the move to revision, also the shape at its arrival destination. | |
shape0 = shapes[0].as_shape() | |
self.assertFalse(shape0.is_move_from_revision) | |
self.assertTrue(shape0.is_move_to_revision) | |
# This is the move from revision, which is the shape at its original location. | |
shape1 = shapes[1].as_shape() | |
self.assertTrue(shape1.is_move_from_revision) | |
self.assertFalse(shape1.is_move_to_revision) |