ติดตามการเปลี่ยนแปลงในเอกสาร

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

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

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

การแก้ไขคืออะไร

ก่อนที่จะเจาะลึกการแก้ไข เรามาอธิบายความหมายของการแก้ไขกันก่อน revision คือการเปลี่ยนแปลงที่เกิดขึ้นในโหนดหนึ่งของเอกสาร ในขณะที่กลุ่มการแก้ไขซึ่งแสดงโดยคลาส RevisionGroup คือกลุ่มของการแก้ไขตามลำดับที่เกิดขึ้นในหลายโหนดของเอกสาร โดยพื้นฐานแล้ว การแก้ไขเป็นเครื่องมือสำหรับการติดตามการเปลี่ยนแปลง

การแก้ไขจะใช้ในคุณลักษณะการติดตามการเปลี่ยนแปลงและภายในคุณลักษณะการเปรียบเทียบเอกสาร ซึ่งการแก้ไขจะปรากฏเป็นผลจากการเปรียบเทียบ ดังนั้น การแก้ไขภายในคุณลักษณะการติดตามการเปลี่ยนแปลงจะแสดงโดยใครและสิ่งที่เปลี่ยนแปลง

Aspose.Words รองรับการแก้ไขประเภทต่างๆ รวมถึงใน Microsoft Word เช่น การแทรก การลบ การเปลี่ยนรูปแบบ StyleDefinitionChange และการย้าย ประเภทการแก้ไขทั้งหมดจะแสดงด้วยการแจงนับ RevisionType

เริ่มต้นและหยุดการติดตามการเปลี่ยนแปลง

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

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
Body body = doc.FirstSection.Body;
Paragraph para = body.FirstParagraph;
// Add text to the first paragraph, then add two more paragraphs.
para.AppendChild(new Run(doc, "Paragraph 1. "));
body.AppendParagraph("Paragraph 2. ");
body.AppendParagraph("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.StartTrackRevisions("John Doe", DateTime.Now);
// This paragraph is a revision and will have the according "IsInsertRevision" flag set.
para = body.AppendParagraph("Paragraph 4. ");
Assert.True(para.IsInsertRevision);
// Get the document's paragraph collection and remove a paragraph.
ParagraphCollection paragraphs = body.Paragraphs;
Assert.AreEqual(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.
Assert.AreEqual(4, paragraphs.Count);
Assert.True(para.IsDeleteRevision);
// The delete revision paragraph is removed once we accept changes.
doc.AcceptAllRevisions();
Assert.AreEqual(3, paragraphs.Count);
Assert.That(para, Is.Empty);
// Stopping the tracking of revisions makes this text appear as normal text.
// Revisions are not counted when the document is changed.
doc.StopTrackRevisions();
// Save the document.
doc.Save(ArtifactsDir + "WorkingWithRevisions.AcceptRevisions.docx");

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new 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 body = doc.FirstSection.Body;
Console.WriteLine("Paragraph count: {0}", body.Paragraphs.Count);
// Start tracking revisions.
doc.StartTrackRevisions("Author", new DateTime(2020, 12, 23, 14, 0, 0));
// Generate revisions when moving a node from one location to another.
Node node = body.Paragraphs[3];
Node endNode = body.Paragraphs[5].NextSibling;
Node referenceNode = body.Paragraphs[0];
while (node != endNode)
{
Node nextNode = node.NextSibling;
body.InsertBefore(node, referenceNode);
node = nextNode;
}
// Stop the process of tracking revisions.
doc.StopTrackRevisions();
// There are 3 additional paragraphs in the move-from range.
Console.WriteLine("Paragraph count: {0}", body.Paragraphs.Count);
doc.Save(ArtifactsDir + "WorkingWithRevisions.MoveNodeInTrackedDocument.docx");

จัดการและจัดเก็บการเปลี่ยนแปลงเป็นการแก้ไข

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

Aspose.Words ช่วยให้คุณตรวจสอบว่าเอกสารมีการแก้ไขหรือไม่โดยใช้คุณสมบัติ HasRevision หากคุณไม่ต้องการติดตามการเปลี่ยนแปลงในเอกสารของคุณโดยอัตโนมัติผ่านเมธอด StartTrackRevisions และ StopTrackRevisions คุณสามารถใช้คุณสมบัติ TrackRevisions เพื่อตรวจสอบว่าการเปลี่ยนแปลงได้รับการติดตามในขณะที่แก้ไขเอกสารใน Microsoft Word และจัดเก็บเป็นการแก้ไขหรือไม่

คุณลักษณะ TrackRevisions ทำการแก้ไขแทนการเปลี่ยนแปลง DOM จริง แต่การแก้ไขนั้นแยกจากกัน ตัวอย่างเช่น หากคุณลบย่อหน้าใดๆ Aspose.Words จะทำให้เป็นการแก้ไข โดยทำเครื่องหมายว่าเป็นการลบ แทนที่จะลบออก

นอกจากนี้ Aspose.Words ยังช่วยให้คุณตรวจสอบว่าวัตถุถูกแทรก ลบ หรือเปลี่ยนการจัดรูปแบบโดยใช้คุณสมบัติ IsDeleteRevision, IsFormatRevision, IsInsertRevision, IsMoveFromRevision และ IsMoveToRevision หรือไม่

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการใช้คุณสมบัติที่แตกต่างกับการแก้ไข:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
// Insert an inline shape without tracking revisions.
Assert.False(doc.TrackRevisions);
Shape shape = new Shape(doc, ShapeType.Cube);
shape.WrapType = WrapType.Inline;
shape.Width = 100.0;
shape.Height = 100.0;
doc.FirstSection.Body.FirstParagraph.AppendChild(shape);
// Start tracking revisions and then insert another shape.
doc.StartTrackRevisions("John Doe");
shape = new Shape(doc, ShapeType.Sun);
shape.WrapType = WrapType.Inline;
shape.Width = 100.0;
shape.Height = 100.0;
doc.FirstSection.Body.FirstParagraph.AppendChild(shape);
// Get the document's shape collection which includes just the two shapes we added.
List<Shape> shapes = doc.GetChildNodes(NodeType.Shape, true).Cast<Shape>().ToList();
Assert.AreEqual(2, shapes.Count);
// Remove the first shape.
shapes[0].Remove();
// Because we removed that shape while changes were being tracked, the shape counts as a delete revision.
Assert.AreEqual(ShapeType.Cube, shapes[0].ShapeType);
Assert.True(shapes[0].IsDeleteRevision);
// And we inserted another shape while tracking changes, so that shape will count as an insert revision.
Assert.AreEqual(ShapeType.Sun, shapes[1].ShapeType);
Assert.True(shapes[1].IsInsertRevision);
// 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 = new Document(MyDir + "Revision shape.docx");
shapes = doc.GetChildNodes(NodeType.Shape, true).Cast<Shape>().ToList();
Assert.AreEqual(2, shapes.Count);
// This is the move to revision, also the shape at its arrival destination.
Assert.False(shapes[0].IsMoveFromRevision);
Assert.True(shapes[0].IsMoveToRevision);
// This is the move from revision, which is the shape at its original location.
Assert.True(shapes[1].IsMoveFromRevision);
Assert.False(shapes[1].IsMoveToRevision);