Originally published at: Generate and insert word definitions with ONLYOFFICE macro | ONLYOFFICE Blog
Adding clear definitions to your documents can significantly improve their overall quality. With ONLYOFFICE macros, automating these tasks becomes a breeze. In this blog post, we’ll show you how to create a macro that pulls word definitions from the external API and seamlessly inserts them into your document.
Accessing the API
API Ninjas is a powerful API service that offers various free APIs for developers to enhance their applications with features like dictionaries, language translation, and more. For this tutorial, we’ll be using the dictionary API, which provides word definitions. You’ll need to sign up for an API key from the API Ninjas to access their services.
Building the macro
Our goal is to create a macro that fetches the value of the selected word and retrieves its definition from an external API. We’ll then insert the definition into the document.
First, we initialize the necessary variables:
const oDocument = Api.GetDocument(); const oRange = oDocument.GetRangeBySelect(); const word = oRange.GetText();
Here, the oDocument variable represents the current document, the oRange variable holds the selected text range, and the word variable stores the value of the selected word.
Then we use AJAX to make a request to the dictionary API. Replace ‘your-api-key‘ with your actual API key:
success: function(result) { console.log(result); const text = result.definition; // Continue to the next step... }, error: function ajaxError(jqXHR) { console.error('Error: ', jqXHR.responseText); } });
Here, we send a GET request to the API endpoint with the selected word and your API key. In the success callback, we extract the definition property from it.
With the definition retrieved, let’s proceed to insert it into the document:
success: function(result) { console.log(result); const text = result.definition; const oParagraph = Api.CreateParagraph(); oParagraph.AddText(text); oDocument.InsertContent([oParagraph], { "KeepTextOnly": true }); }
In this section, we create a new paragraph by using Api.CreateParagraph(), add the fetched text to it with oParagraph.AddText(text), and finally insert the paragraph into the oDocument by using oDocument.InsertContent().
The entire macro code is the following:
(function() { const oDocument = Api.GetDocument(); const oRange = oDocument.GetRangeBySelect(); const word = oRange.GetText(); $.ajax({ method: 'GET', url: 'https://api.api-ninjas.com/v1/dictionary?word=' + word, headers: { 'X-Api-Key': 'your-api-key'}, contentType: 'application/json', success: function(result) { console.log(result); const text = result.definition; const oParagraph = Api.CreateParagraph(); oParagraph.AddText(text); oDocument.InsertContent([oParagraph], { "KeepTextOnly": true }); }, error: function ajaxError(jqXHR) { console.error('Error: ', jqXHR.responseText); } }); })();
Now, let’s run our macro and see how it works!
We genuinely hope you find this macro a valuable addition to your toolkit. Embracing ONLYOFFICE macros offers you the advantage of enhanced productivity, empowering you with efficient and automated solutions.
As you immerse yourself in macro creation, don’t miss out on the opportunities offered by the ONLYOFFICE API. If you ever have questions or inventive ideas, please share them through the comments or by reaching out to us. We welcome your input and look forward to the potential of working together! Best of luck in your exploratory endeavors!