How to Get Macro Information from an XLSX or XLSM file using Node.js
Macros are still a commonly exploited security vulnerability in Office files, XLSX included. Thankfully, we can tell if macros are enabled in an XLSX or XLSM document with a quick API call.
Using the below code, we can take advantage of a free API that quickly identifies if macros are enabled/present within an XLSX or XLSM document. Our API response will contain a simple Boolean titled “ContainsVbaMacros”, so we can easily take subsequent actions in our application based on whether files have macros enabled.
To structure our API call, let’s start by grabbing a free-tier API key. We can get one of these on the Cloudmersive website by registering a free account, and they will allow a limit of 800 API calls per month with no additional commitment.
Once we have one ready to go, we can install the SDK by either running this command:
npm install cloudmersive-convert-api-client --save
Or by adding this snippet to our package.json:
"dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}
Right after that, we can include the below code to call the function:
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.EditDocumentApi();
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.editDocumentXlsxGetMacroInformation(inputFile, callback);
And that’s all the code we’ll need. We can now easily validate the existence of macros.