使用 ONLYOFFICE 宏快速删除多余空格
  PUvuOydIGKgJ 2023年11月19日 29 0

在文本编辑方面,要是有一个宏能够高效删除多余的空格,那一定是非常必要的,可以提升工作效率,简化工作流。在这篇博文中,我们会指导您创建一个 ONLYOFFICE 宏,用于删除所选文本中多余的空格。

使用 ONLYOFFICE 宏快速删除多余空格_宏

什么是 ONLYOFFICE 宏

如果您是一名资深 Microsoft Excel 用户,那么相信您已对于 VBA 宏非常熟悉了。这些宏是帮助您自动执行日常任务的小型脚本。无论是重构数据,还是在单元格区域中插入多个值。ONLYOFFICE 宏的基础是 JavaScript 语法与文档生成器 API 方法。基于 JavaSript 的宏易于使用,具有跨平台特性且十分安全。这就使得其与 VBA 相比有着显著的优势。

构建宏

我们首先访问活动文档,捕获所选内容:

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

然后删除选中的文本。之后,相应部分会被经格式设置的文本所代替,没有多余的空格:

oRange.Delete();

我们根据换行符,将“rawText”分割成一个段落数组。段落数组的每个元素代表原始文本中的一个段落:

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

然后,循环遍历段落数组中的每个段落,对于有连续空格的地方,用单个空格做替换和清理。清理后的段落存储在“cleanedParagraphs”数组中:

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(‘\n’)”方法,将清理过的段落合并为一个字符串,其中每个清理过的段落都用换行符“(\n)”分隔。这一步至关重要,因为在将文本插入到文档中时,我们需要提供一个带有适当分段符的单个字符串:

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

最后,我们创建一个新段落“(oParagraph)”,并将“cleanedText”插入文档。这个“cleanedText”包含所有已清理的段落,合并成一个单个字符串,并加上换行符以保留原来的段落结构:

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

这个宏的完整代码如下:

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

我们希望这个宏能快速成为您武器库中的宝贵资产,让您的工作效率提升到新的高度。您可以使用 ONLYOFFICE 宏,获得高效和自动化的解决方案,提升效率,释放无限潜力。

在您探索怎么编写宏时,不要忘了 ONLYOFFICE API 为您提供的无限可能。如果您有问题或新创意,欢迎发表评论,或联系我们分享出来。我们非常重视您的洞见,十分期待与您合作。祝您探索之旅好运!

相关链接

使用 ONLYOFFICE 宏生成和插入单词定义

使用 ONLYOFFICE 宏检索网站详细信息

ONLYOFFICE API 文档

宏的样例

GitHub 上的 ONLYOFFICE

ONLYOFFICE 文档中的宏(免费课程)

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月19日 0

暂无评论

推荐阅读
PUvuOydIGKgJ