-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request 'feature/Remove_Extra_Spaces' from feature/Remove_…
…Extra_Spaces into feature/11ty Reviewed-on: https://git.onlyoffice.com/ONLYOFFICE/api.onlyoffice.com/pulls/86
- Loading branch information
Showing
5 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
68 changes: 68 additions & 0 deletions
68
site/pages/Docs/Plugin and Macros/Macros/Samples/Remove extra spaces/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters