How to Get Macro Information from an Excel File in Ruby

Cloudmersive
2 min readJun 28, 2021

Due to the increased presence of attackers in cyberspace and the popular use of online documents, pre-existing macros in a file run the risk of having pre-built malicious macros embedded in their code. These infected macros have the potential to run arbitrary commands or delete files on your hard drive, making a mess of your system. To assist with the protection of your documents, the following API can be used in Ruby to identify macro information in Excel files and allow you to determine if the commands are safe to enable.

We will begin by adding the Ruby client to our Gemfile:

gem 'cloudmersive-convert-api-client', '~> 2.1.6'

Following the client installation, we can call the function with the below code:

# load the gem
require 'cloudmersive-convert-api-client'
# setup authorization
CloudmersiveConvertApiClient.configure do |config|
# Configure API key authorization: Apikey
config.api_key['Apikey'] = 'YOUR API KEY'
# Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
#config.api_key_prefix['Apikey'] = 'Bearer'
end
api_instance = CloudmersiveConvertApiClient::EditDocumentApi.newinput_file = File.new('/path/to/inputfile') # File | Input file to perform the operation on.begin
#Get macro information from a Excel XLSX/XLSM spreadsheet, worksheet
result = api_instance.edit_document_xlsx_get_macro_information(input_file)
p result
rescue CloudmersiveConvertApiClient::ApiError => e
puts "Exception when calling EditDocumentApi->edit_document_xlsx_get_macro_information: #{e}"
end

With the information returned from the API, you can choose to enable or disable macros as needed. If you need to obtain an API key, visit the Cloudmersive website and register for a free account; this provides 800 monthly calls across our library of APIs.

--

--

Cloudmersive

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