使用Google Apps脚本将标题样式应用于单词的所有实例 [英] Use Google Apps Script to apply heading style to all instances of a word

查看:109
本文介绍了使用Google Apps脚本将标题样式应用于单词的所有实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google Doc中使用Google App脚本,您如何编写一个函数来查找单词的所有实例并应用标题样式:例如,我希望每个狗的实例...







和样式狗与标题2,所以它看起来像:







在表格中使用Find in App脚本无处不在,但在Docs中使用App脚本的示例并不多。表格无法将文本重新格式化为标题,因此没有它的示例。

解决方案

使用的方法是:




  • findText ,应用于文档Body。它找到了第一场比赛;通过将前一个匹配作为第二个参数from发现后续匹配。搜索模式是以字符串形式呈现的正则表达式,在本例中(?i)\\\\\\\\\\\\\\\\\\\\'不区分大小写的搜索,并且 \\ b 正在转义为 \ b ,这意味着字边界—所以我们不会重新安排热狗和狗。

  • getElement ,应用于由findText返回的RangeElement。重点是,匹配的文本可能只是元素的一部分,这部分称为RangeElement。我们无法仅将标题样式应用于某个部分,因此获得了整个元素。

  • getParent ,应用于由getElement返回的Text元素。同样,这是因为标题样式适用于Text上方的一个级别。

  • setAttributes 使用正确的样式(事先创建的对象,使用适当的枚举 )。这适用于Text的父项,无论它发生什么 - 段落,项目符号等。人们可能希望对此有更多的选择,并检查元素的类型,但我不这样做。



示例:

 函数dog(){
var body = DocumentApp.getActiveDocument()。getBody();
var style = {};
style [DocumentApp.Attribute.HEADING] = DocumentApp.ParagraphHeading.HEADING2;
var pattern =(?i)\\bdogs\\;

var found = body.findText(pattern);
while(found){
found.getElement()。getParent()。setAttributes(style);
found = body.findText(pattern,found);
}
}


I am using Google App Scripts in a Google Doc, how do you write a function to find all instances of a word and apply a heading style to it:

For example, I want every instance of "Dogs"...

  • Cats
  • Dogs
  • Fish

and style "dogs" with "Heading 2" so it looks like:

  • Cats
  • Dogs

  • Fish

Using Find in App Scripts on Sheets is everywhere online, but there are not many examples of using App Scripts in Docs. Sheets does not have the option to reformat text as Headings, so there are no examples of it.

解决方案

The methods to use are:

  • findText, applied to document Body. It finds the first match; subsequent matches are found by passing the previous match as the second parameter "from". The search pattern is a regular expression presented as a string, in this example (?i)\\bdogs\\b where (?i) means case-insensitive search, and \\b is escaping for \b, meaning word boundary — so we don't restyle "hotdogs" along with "dogs".
  • getElement, applied to RangeElement returned by findText. The point is, the matching text could be only a part of the element, and this part is called RangeElement. We can't apply heading style to only a part, so the entire element is obtained.
  • getParent, applied to the Text element returned by getElement. Again, this is because heading style applies at a level above Text.
  • setAttributes with the proper style (an object created in advance, using appropriate enums). This is applied to the parent of Text, whatever it happened to be - a paragraph, a bullet item, etc. One may wish to be more selective about this, and check the type of the element first, but I don't do that here.

Example:

function dogs() {
  var body = DocumentApp.getActiveDocument().getBody();
  var style = {};
  style[DocumentApp.Attribute.HEADING] = DocumentApp.ParagraphHeading.HEADING2;
  var pattern = "(?i)\\bdogs\\b";

  var found = body.findText(pattern);
  while (found) {
    found.getElement().getParent().setAttributes(style);
    found = body.findText(pattern, found);
  }
}

这篇关于使用Google Apps脚本将标题样式应用于单词的所有实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆