ردیابی تغییرات در یک سند
عملکرد تغییرات آهنگ که به عنوان بازبینی نیز شناخته می شود، به شما امکان می دهد تغییرات محتوا و قالب بندی ایجاد شده توسط شما یا سایر کاربران را ردیابی کنید. این ویژگی تغییر آهنگ با Aspose.Words از تغییرات آهنگ در Microsoft Word پشتیبانی می کند. با استفاده از این قابلیت، میتوانید به ویرایشهای جداگانه در سند خود دسترسی داشته باشید و ویژگیهای مختلفی را برای آنها اعمال کنید.
هنگامی که ویژگی تغییرات آهنگ را فعال می کنید، همه عناصر درج شده، حذف شده، و اصلاح شده سند به صورت بصری با اطلاعاتی در مورد اینکه توسط چه کسی، چه زمانی و چه چیزی تغییر کرده است برجسته می شوند. به اشیایی که اطلاعات مربوط به تغییرات تغییر یافته را حمل می کنند “تغییرات ردیابی” می گویند. به عنوان مثال، فرض کنید که میخواهید یک سند را بررسی کنید و تغییرات مهمی را ایجاد کنید - این ممکن است به این معنی باشد که شما نیاز به اصلاح دارید. همچنین، ممکن است برای بحث در مورد برخی از تغییرات نیاز به درج نظرات داشته باشید. اینجاست که ردیابی تغییرات در اسناد وارد می شود.
این مقاله نحوه مدیریت و ردیابی تغییرات ایجاد شده توسط بسیاری از بازبینان در یک سند و همچنین ویژگی های ردیابی تغییرات را توضیح می دهد.
بازنگری چیست
قبل از پرداختن به اصلاحات، بیایید معنای تجدید نظرها را توضیح دهیم. یک revision تغییری است که در یک گره از یک سند رخ می دهد در حالی که یک گروه بازبینی که توسط کلاس RevisionGroup نشان داده می شود، گروهی از ویرایش های متوالی است که در بسیاری از گره های یک سند رخ می دهد. اساساً تجدید نظر ابزاری برای ردیابی تغییرات است.
ویرایشها در ویژگی ردیابی تغییرات و در ویژگی مقایسه اسناد استفاده میشوند، جایی که ویرایشها در نتیجه مقایسه ظاهر میشوند. بنابراین، تجدید نظر در ویژگی تغییرات ردیابی نشان می دهد که چه کسی و چه چیزی تغییر کرده است.
Aspose.Words از انواع ویرایش های مختلف و همچنین در Microsoft Word مانند Insertion، Deletion، FormatChange، StyleDefinitionChange و Moving پشتیبانی می کند. همه انواع ویرایش با شمارش RevisionType نشان داده می شوند.
شروع و توقف ردیابی تغییرات
ویرایش یک سند معمولاً تا زمانی که ردیابی آن را شروع نکنید به عنوان یک بازنگری به حساب نمی آید. Aspose.Words به شما این امکان را می دهد که با مراحل ساده تمام تغییرات سند خود را بطور خودکار پیگیری کنید. با استفاده از روش StartTrackRevisions می توانید به راحتی فرآیند ردیابی تغییرات را شروع کنید. اگر باید روند ردیابی تغییرات را متوقف کنید تا هر گونه ویرایش در آینده به عنوان ویرایش تلقی نشود، باید از روش StopTrackRevisions استفاده کنید.
StartTrackingRevisions
وضعیت ویژگی TrackRevisions را تغییر نمی دهد و از مقدار آن برای هدف ردیابی تجدید نظر استفاده نمی کند. علاوه بر این، اگر یک گره از یک مکان به مکان دیگر در سند ردیابی شده منتقل شده باشد، بازبینیهای حرکت ایجاد میشود، از جمله انتقال از و حرکت به محدوده.
در پایان فرآیند ردیابی تغییرات در سند خود، شما این توانایی را خواهید داشت که حتی تمام ویرایش ها را بپذیرید یا آنها را رد کنید تا سند به شکل اصلی خود بازگردد. این را می توان با استفاده از روش AcceptAllRevisions یا RejectAll به دست آورد. علاوه بر این، با استفاده از روش Accept یا Reject می توانید هر ویرایش را به طور جداگانه بپذیرید یا رد کنید.
همه تغییرات برای یک بار تکرار از لحظه شروع فرآیند تا لحظه توقف آن پیگیری می شود. ارتباط بین تکرارهای مختلف به صورت سناریوی زیر نشان داده می شود: شما فرآیند ردیابی را کامل می کنید، سپس تغییراتی را ایجاد می کنید و دوباره ردیابی تغییرات را شروع می کنید. با این سناریو تمامی تغییراتی که قبول یا رد نکرده اید دوباره نمایش داده می شود.
AcceptAllRevisions
شبیه به “Accept All Changes” در 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); |