Skip to content

Commit

Permalink
Merge pull request 'feature/Remove_Extra_Spaces' from feature/Remove_…
Browse files Browse the repository at this point in the history
…Extra_Spaces into feature/11ty

Reviewed-on: https://git.onlyoffice.com/ONLYOFFICE/api.onlyoffice.com/pulls/86
  • Loading branch information
LinneyS committed Dec 12, 2024
2 parents 020da8d + f628690 commit 82c585e
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- 404 page
- legacy version link
- macros: added add comments and change cell colors in spreadsheet macro sample
- macros: added remove extra spaces in document macro sample

## 6.1.0
- docspace js sdk: react component
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
order:
---

## Description

Removes extra spaces in text document.

<!-- This code snippet is shown in the screenshot. -->

<!-- eslint-skip -->

```ts
(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 });
})();
```

Methods used: GetDocument, GetRangeBySelect, GetText, Delete, CreateParagraph, AddText, InsertContent

## Reference Microsoft VBA macro code

<!-- code generated with AI -->

```vb
Sub RemoveExtraSpaces()
Dim rng As Range

' Set the range to the entire document
Set rng = ActiveDocument.Content

' Replace multiple spaces with a single space
rng.Text = Replace(rng.Text, " ", " ")

MsgBox "Extra spaces removed!", vbInformation
End Sub
```

## Result

<!-- imgpath -->

![Remove extra spaces](/assets/images/plugins/remove-extra-spaces.png)
6 changes: 6 additions & 0 deletions site/pages/Docs/Plugin and Macros/Macros/Samples/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ In this example we are adding comments and changing the background colors of sel

[More](Add%20comments%20and%20change%20cell%20colors%20in%20spreadsheet/index.md)

## Remove extra spaces

In this example we are removing extra spaces in the text document.

[More](Remove%20extra%20spaces)

## Support

If you want to request a feature or report a bug regarding macros, use the issues section [on GitHub.](https://github.com/ONLYOFFICE/plugin-macros/issues)
Expand Down

0 comments on commit 82c585e

Please sign in to comment.