How to Validate an XLSX File in Node.js

Cloudmersive
2 min readOct 4, 2023

--

Using code to identify invalid/corrupt XLSX files saves us a lot of time and hassle in the long run.

After all, we don’t want to lose important spreadsheets & reports because of avoidable oversights.

Thankfully, using the below ready-to-run code examples, we can easily take advantage of a free XLSX validation API. This will, first and foremost, identify if our document is valid, but it will also point out password-protection measures and callout any errors/warnings in the document.

To structure our API call, we can begin by installing the SDK. To do that, let’s either run the following command:

npm install cloudmersive-convert-api-client --save

Or add the following snippet to our package.json:

  "dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}

We can now copy the below code into our file, and we can authorize our requests with a free-tier API key (obtainable by registering a free account on the Cloudmersive website):

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;

// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';



var apiInstance = new CloudmersiveConvertApiClient.ValidateDocumentApi();

var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.


var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.validateDocumentXlsxValidation(inputFile, callback);

Our form-data request will return an object with all the important information we need. Easy!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet