Accept or Reject Tracked Changes in Word using Node.js

Microsoft Word provides a built-in functionality to track changes and keep revisions in Word documents. However, we may accept or reject the tracked changes of Word documents (DOC or DOCX) programmatically on the cloud. In this article, we will learn how to accept or reject tracked changes in a Word document using a REST API in Node.js.

The following topics shall be covered in this article:

REST API and Node.js SDK to Accept or Reject Changes

For accepting or rejecting the tracked changes in a Word document, we will be using the Node.js SDK of GroupDocs.Comparison Cloud API. It allows comparing two or more documents of the supported formats and tracking their changes. Please install it using the following command in the console:

npm install groupdocs-comparison-cloud

Please get your Client ID and Secret from the dashboard before following the mentioned steps. Once you have your ID and secret, add in the code as shown below:

global.clientId = "112f0f38-9dae-42d5-b4fc-cc84ae644972";
global.clientSecret = "16ad3fe0bdc39c910f57d2fd48a5d618";
global.myStorage = "";
const config = new groupdocs_comparison_cloud.Configuration(clientId, clientSecret);
config.apiBaseUrl = "https://api.groupdocs.cloud";

Accept or Reject Tracked Changes using REST API in Node.js

We can accept or reject specific revisions in Word documents by following the simple steps given below:

  1. Upload the DOCX files to the Cloud
  2. Accept or Reject Changes in Word Documents
  3. Download the output file

Upload the Document

Firstly, we will upload the Word document with revisions to the cloud using the code example given below:

// Construct FileApi
let fileApi = groupdocs_comparison_cloud.FileApi.fromConfig(config);
// Input file path
let file = 'C:\\Files\\source_with_revisions.docx';
// Read file
fs.readFile(file, (err, fileStream) => {
// Create upload file request
let request = new groupdocs_comparison_cloud.UploadFileRequest("source_with_revisions.docx", fileStream, myStorage);
// Upload file
fileApi.uploadFile(request);
});

As a result, the uploaded DOCX file will be available in the files section of the dashboard on the cloud.

Accept or Reject Changes in Word Documents

Now, we will accept or reject tracked changes programmatically by following the steps given below:

  • Firstly, create an instance of the ReviewApi.
  • Next, provide the uploaded DOCX file path.
  • Then, get revisions and accept or reject the desired revisions in a loop.
  • Finally, apply revisions and save the updated file as “output.docx”.

The following code sample shows how to accept the tracked changes in a Word document using a REST API in Node.js.

// Create an instance of the API
let reviewApi = groupdocs_comparison_cloud.ReviewApi.fromKeys(clientId, clientSecret);
// Input source file
let source = new groupdocs_comparison_cloud.FileInfo();
source.filePath = "source_with_revisions.docx";
// Define apply revisions options
let options = new groupdocs_comparison_cloud.ApplyRevisionsOptions();
options.sourceFile = source;
options.outputPath = "output.docx";
// Create get revisions request
let request = new groupdocs_comparison_cloud.GetRevisionsRequest(source);
// Get all revisions
let revisions = await reviewApi.getRevisions(request);
// Accept or Reject revisions
revisions.forEach(revision => {
revision.action = groupdocs_comparison_cloud.RevisionInfo.ActionEnum.Accept;
});
options.revisions = revisions;
// Create apply revisions request
let applyRequest = new groupdocs_comparison_cloud.ApplyRevisionsRequest(options);
// Apply revisions
let response = await reviewApi.applyRevisions(applyRequest);
console.log("Output file link: " + response.href);
Accept or Reject Tracked Changes using REST API in Node.js

Accept or Reject Tracked Changes using REST API in Node.js

Similarly, we can reject any changes by following the steps mentioned earlier. However, we just need to apply the following revision options:

revisions.forEach(revision => {
	revision.action = groupdocs_comparison_cloud.RevisionInfo.ActionEnum.Reject;
});

Download the Resultant File

As a result, the above code example will save a newly created DOCX file with changes on the cloud. It can be downloaded using the following code sample:

// Construct FileApi
let fileApi = groupdocs_comparison_cloud.FileApi.fromConfig(config);
// Create download file request
let request = new groupdocs_comparison_cloud.DownloadFileRequest("output.docx", myStorage);
// Download file
let response = await fileApi.downloadFile(request);
// Save in the working directory
fs.writeFile("C:\\Files\\output.docx", response, "binary", function (err) { });

Accept or Reject All the Changes in Node.js

We can accept or reject all the changes at once by following the steps given below:

  • Firstly, create an instance of the ReviewApi.
  • Next, provide the uploaded DOCX file path.
  • Then, get revisions and accept or reject all the revisions.
  • Finally, apply revisions and save the updated file as “output.docx”.

The following code sample shows how to accept all the changes using a REST API. Please follow the steps mentioned earlier to upload and download the file.

// Create an instance of the API
let reviewApi = groupdocs_comparison_cloud.ReviewApi.fromKeys(clientId, clientSecret);
// Input source file
let source = new groupdocs_comparison_cloud.FileInfo();
source.filePath = "source_with_revisions.docx";
// Define apply revision options
let options = new groupdocs_comparison_cloud.ApplyRevisionsOptions();
options.sourceFile = source;
options.outputPath = "output.docx";
// Accept all revisions
options.acceptAll = true;
// Reject all revisions
// options.rejectAll = true;
// Create apply revision request
let request = new groupdocs_comparison_cloud.ApplyRevisionsRequest(options);
// Apply revisions
let response = await reviewApi.applyRevisions(request);
console.log("Output file link: " + response.href);

Try Online

Please try the following free online DOCX comparison tool, which is developed using the above API. https://products.groupdocs.app/comparison/docx

Conclusion

In this article, we have learned how to accept or reject tracked changes in Word documents using a REST API on the cloud. We have also seen how to accept or reject all the revisions in one go programmatically. This article also explained how to programmatically upload a DOCX file to the cloud and then download the resultant file from the cloud. Besides, you can learn more about GroupDocs.Comparison Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser. In case of any ambiguity, please feel free to contact us on the forum.

See Also