Remove extra spaces with ONLYOFFICE macro

4 October 2023By Serge

In the realm of text editing, an efficient macro that removes unnecessary spaces is essential. It enhances productivity and streamlines the workflow. In this blog post, we’ll guide you through the creation of an ONLYOFFICE macro designed to remove excess spaces from selected text.

Remove extra spaces with ONLYOFFICE macro

Building the macro

We start by accessing the active document and capturing the selected content:

const oDocument = Api.GetDocument();
const oRange = oDocument.GetRangeBySelect();
const rawText = oRange.GetText();

Then we delete the selected text. Later it will be replaced with the formatted text without any extra spaces:

oRange.Delete();

We split the rawText into an array of paragraphs based on newline characters. Each element of the paragraphs array represents a paragraph from the original text:

// Split the original word into an array of paragraphs based on newline characters
const paragraphs = rawText.split('\n');

Then the loop iterates through each paragraph in the paragraphs array and cleans it by replacing consecutive whitespace with a single space. The cleaned paragraphs are stored in the cleanedParagraphs array:

const cleanedParagraphs = [];
// Clean each paragraph and store it in the cleanedParagraphs array
for (const paragraph of paragraphs) {
    // Use a regular expression to replace consecutive whitespaces with a single space
    const cleanedParagraph = paragraph.replace(/\s+/g, ' ');
    cleanedParagraphs.push(cleanedParagraph);
}

After cleaning each paragraph, we use the join(‘\n’) method to combine the cleaned paragraphs back into a single string, where each cleaned paragraph is separated by a newline character (\n). This step is crucial because when inserting text into a document, we need to provide a single string with appropriate paragraph breaks:

// Join the cleaned paragraphs back together with newline characters
const cleanedText = cleanedParagraphs.join('\n');

Finally, we create a new paragraph (oParagraph) and insert the cleanedText into the document. This cleanedText contains all the cleaned paragraphs combined into a single string, with newline characters to preserve the original paragraph structure:

// Insert the cleanedText with original paragraph structure
const oParagraph = Api.CreateParagraph();
oParagraph.AddText(cleanedText);
oDocument.InsertContent([oParagraph], { "KeepTextOnly": true });

The entire macro code is the following:

(function()
{
 const oDocument = Api.GetDocument();
const oRange = oDocument.GetRangeBySelect();
const rawText = oRange.GetText();
oRange.Delete();

// Split the original word into an array of paragraphs based on newline characters
const paragraphs = rawText.split('\n');

// Create an array to store cleaned paragraphs
const cleanedParagraphs = [];

// Clean each paragraph and store it in the cleanedParagraphs array
for (const paragraph of paragraphs) {
    // Use a regular expression to replace consecutive whitespaces with a single space
    const cleanedParagraph = paragraph.replace(/\s+/g, ' ');
    cleanedParagraphs.push(cleanedParagraph);
}

// Join the cleaned paragraphs back together with newline characters
const cleanedText = cleanedParagraphs.join('\n');

// Insert the cleanedText with original paragraph structure
const oParagraph = Api.CreateParagraph();
oParagraph.AddText(cleanedText);
oDocument.InsertContent([oParagraph], { "KeepTextOnly": true });   
})();

Let’s run our macro and see how it works!

We hope that this macro will be a valuable asset in your toolkit, streamlining your productivity to new heights. With ONLYOFFICE macros, you unlock the potential for heightened productivity, giving you access to efficient and automated solutions.

While you venture into macro creation, don’t overlook the possibilities made available by the ONLYOFFICE API. If you ever have queries or innovative thoughts, please don’t hesitate to share them, either through comments or by reaching out to us. We value your input and are excited about the potential for collaboration! Best of luck in your exploratory endeavors!