Originally published at: Use ONLYOFFICE macros to easily add TextForms to master form templates | ONLYOFFICE Blog
When working with form template documents, we often use dummy placeholders, and end up replacing them later with form elements. In this blog post, we will automate this process by using a Document Editor macro that inserts a TextForm at every instance of a specific word.
Building the macro
const oDocument = Api.GetDocument(); const numberOfElements = oDocument.GetElementsCount();
Here, the current document is stored in the oDocument variable. Then, we retrieve the total number of elements in the document using the GetElementsCount() method.
for (let elementIndex = 0; elementIndex < numberOfElements; elementIndex++)
A for loop is employed in this section to cycle through each element. By doing so, we make sure that we insert the TextForm for the specified word in every element (paragraph).
const oElement = oDocument.GetElement(elementIndex); const rawText = oElement.GetText();
Inside the for loop, we acquire each document element using the GetElement() method with the elementIndex. Then, we extract the plain text, storing it in the rawText variable.
const targetWord = "sample"; let count = 0; for (let i = 0; i < targetWord.length; i++) { count++; } const wordLength = count - 1;
Here, we define the target word for our comments. Following that, we determine the length of the word using a for loop and store it in the variable wordLength.
const wordRegex = new RegExp("\\b" + targetWord + "\\b", "gi");
Then, a regular expression (wordRegex) is created using the target word to find all occurrences in the document.
let match; while ((match = wordRegex.exec(rawText)) !== null) { let matchPosition = match.index; let nextMatchPosition = matchPosition + wordLength;
Here, inside a while loop, we iterate through all the instances found and stored in wordRegex.
For each match, the starting position of the word (matchPosition) and the position of the next character after the word (nextMatchPosition) are calculated.
const insertRange = oElement.GetRange(matchPosition, nextMatchPosition); insertRange.Select();
Following that, we store the range to operate in insertRange. Then, we select the range using the Select() method.
oDocument.InsertTextForm({ key: "Personal information", tip: "Enter your first name", required: true, placeholder: "Name", comb: true, cellWidth: 3, multiLine: false, autoFit: false, placeholderFromSelection: true, keepSelectedTextInForm: false, });
Finally, we insert a TextForm at the selected range. The InsertTextForm() method has various parameters. You can modify them according to your needs.
The full macro code
Here is the code for the entire macro.
(function () { function addTextForm() { const oDocument = Api.GetDocument(); const numberOfElements = oDocument.GetElementsCount();//looping through all the elements in the document. for ( let elementIndex = 0; elementIndex < numberOfElements; elementIndex++ ) { const oElement = oDocument.GetElement(elementIndex); const rawText = oElement.GetText(); // Specify the target word and the length of the word const targetWord = "sample"; let count = 0; for (let i = 0; i < targetWord.length; i++) { count++; } const wordLength = count - 1; // Find all occurrences of the target word in the document const wordRegex = new RegExp("\\b" + targetWord + "\\b", "gi"); let match; while ((match = wordRegex.exec(rawText)) !== null) { let matchPosition = match.index; // returns the index where the word starts. let nextMatchPosition = matchPosition + wordLength; //the index where the word ends. console.log(matchPosition); console.log(nextMatchPosition); const insertRange = oElement.GetRange(matchPosition, nextMatchPosition); insertRange.Select(); //insert a text form at the selected range oDocument.InsertTextForm({ key: "Personal information", tip: "Enter your first name", required: true, placeholder: "Name", comb: true, cellWidth: 3, multiLine: false, autoFit: false, placeholderFromSelection: true, keepSelectedTextInForm: false, }); } } } addTextForm();
})();
Now, let us see how our macro performs.
We’re confident that integrating this macro into your oForm workflow will enhance your experience with Form Templates and oForms.
Don’t miss the chance to harness the power of the ONLYOFFICE API. Our extensive library of API methods is the key to transforming your ideas into reality. If you have any questions or innovative concepts, we encourage you to share them with us. Your input is highly valued, and we are excited about the possibility of collaborating with you. Best of luck in your exploratory endeavors!