ドキュメント内の変更を追跡する
変更の追跡機能 (レビューとも呼ばれます) を使用すると、自分または他のユーザーが行ったコンテンツおよび書式設定の変更を追跡できます。 Aspose.Words によるこのトラック変更機能は、Microsoft Word でのトラック変更をサポートします。この機能を使用すると、ドキュメント内の個々のリビジョンにアクセスし、それらにさまざまなプロパティを適用できます。
変更の追跡機能を有効にすると、文書の挿入、削除、および変更されたすべての要素が、誰が、いつ、何を変更したかに関する情報とともに視覚的に強調表示されます。何が変更されたかに関する情報を保持するオブジェクトは、「変更の追跡」と呼ばれます。たとえば、ドキュメントをレビューして重要な変更を加えたいとします。これは、改訂が必要になることを意味する場合があります。また、一部の変更について説明するためにコメントを挿入する必要がある場合もあります。そこで重要になるのが、ドキュメントの変更の追跡です。
この記事では、同じ文書に対して多数のレビュー担当者によって作成された変更を管理および追跡する方法と、変更を追跡するためのプロパティについて説明します。
リビジョンとは
リビジョンに入る前に、リビジョンの意味を説明しましょう。 revision はドキュメントの 1 つのノードで発生する変更ですが、RevisionGroup クラスで表されるリビジョン グループはドキュメントの多くのノードで発生する連続したリビジョンのグループです。基本的に、リビジョンは変更を追跡するためのツールです。
リビジョンは、変更の追跡機能およびドキュメントの比較機能内で使用され、比較の結果としてリビジョンが表示されます。したがって、変更追跡機能内のリビジョンには、誰が何を変更したかが表示されます。
Aspose.Words は、Microsoft Word だけでなく、挿入、削除、FormatChange、StyleDefinitionChange、Moving などのさまざまなリビジョン タイプをサポートしています。すべてのリビジョン タイプは RevisionType 列挙で表されます。
変更の追跡を開始および停止する
通常、ドキュメントの編集は、追跡を開始するまでリビジョンとしてカウントされません。 Aspose.Words を使用すると、簡単な手順でドキュメント内のすべての変更を自動的に追跡できます。 StartTrackRevisions メソッドを使用すると、変更を追跡するプロセスを簡単に開始できます。今後の編集がリビジョンとみなされないように変更の追跡プロセスを停止する必要がある場合は、StopTrackRevisions メソッドを使用する必要があります。
StartTrackingRevisions
メソッドは TrackRevisions プロパティのステータスを変更せず、リビジョン追跡の目的でその値を使用しないことに注意してください。さらに、追跡されているドキュメント内でノードが 1 つの場所から別の場所に移動された場合は、移動元範囲と移動先範囲を含む移動リビジョンが作成されます。
文書の変更追跡プロセスの最後には、すべての改訂を受け入れるか、拒否して文書を元の形式に戻すこともできます。これは、AcceptAllRevisions または RejectAll 方式のいずれかを使用して実現できます。さらに、Accept または Reject 方式を使用して、各リビジョンを個別に承認または拒否できます。
すべての変更は、プロセスを開始した瞬間からプロセスを停止する瞬間まで、1 回の反復で追跡されます。異なる反復間の関係は、次のシナリオで表されます。追跡プロセスを完了し、いくつかの変更を加え、再び変更の追跡を開始します。このシナリオでは、承認または拒否しなかったすべての変更が再度表示されます。
AcceptAllRevisions
メソッドは、Microsoft Word の「すべての変更を受け入れる」に似ていることに注意してください。
次のコード例は、変更の追跡を操作する方法を示しています。
// 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 プロパティを使用して、オブジェクトが挿入、削除、またはフォーマット変更されたかどうかを確認できます。
TrackRevisions
プロパティの間には関連性がないことに注意してください。さらに、変更の追跡機能に関係なく、リビジョンを承認/拒否できます。
次のコード例は、リビジョンに応じてさまざまなプロパティを適用する方法を示しています。
// 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); |