简体   繁体   English

使用谷歌应用脚本格式化谷歌文档中的关键字

[英]Format keywords in google docs with google apps script

i'm trying to make a google app script for google docs that puts all the occurrences of a string in a document in bold style, for example, for this text:我正在尝试为谷歌文档制作一个谷歌应用脚本,它将所有出现的字符串以粗体样式放在文档中,例如,对于这个文本:

hello你好

hello你好

good by有效期

000000000000000000 hello 00000 000000000000000000 你好 00000

Hello hello hello你好你好你好

000000000000000001111111111111111111111111222222222222222222222233333333333333333444444444444444444444445555555555555 hello 000000000000000000111111111111111111111111122222222222222222222223333333333333333344444444444444444444444555555555555你好

If the string is hello , I would like the script to put it like this:如果字符串是hello ,我希望脚本这样写:

hello你好

hello你好

good by有效期

000000000000000000 hello 00000 000000000000000000你好00000

Hello hello hello你好你好你好

000000000000000001111111111111111111111111222222222222222222222233333333333333333444444444444444444444445555555555555 hello 000000000000000000111111111111111111111111122222222222222222222223333333333333333344444444444444444444444555555555555你好

I made this code:我做了这个代码:

  var document = DocumentApp.getActiveDocument();
  var body = document.getBody();

  var Style = {};
  Style[DocumentApp.Attribute.BOLD] = true;

  var found = body.findText("hola");

  while(found != null) {
    found.getElement().setAttributes(Style);
    found = body.findText("hello", found);
  }

But what I obtain after running the code, is this:但是我在运行代码后得到的是:

hello你好

hello你好

good by有效期

000000000000000000 hello 00000 000000000000000000 你好 00000

Hello hello hello你好你好你好

000000000000000001111111111111111111111111222222222222222222222233333333333333333444444444444444444444445555555555555 hello 000000000000000000111111111111111111111111122222222222222222222223333333333333333344444444444444444444444555555555555你好

So the code puts in bold all the line that contains the string hello , and not only the string hello因此,代码将包含字符串hello的所有行都加粗,而不仅仅是字符串hello

Somebody knows how could I do to put in bold only the string hello and not all the line that contains hello ?有人知道我该怎么做才能只将字符串hello而不是包含hello的所有行都加粗?

You need to specify the start and end of the text you are trying to edit by using getStartOffset() and getEndOffsetInclusive().您需要使用 getStartOffset() 和 getEndOffsetInclusive() 指定尝试编辑的文本的开始和结束。 This will prevent your script to format the entire text block to bold .这将阻止您的脚本将整个文本块格式化为粗体 Please see below code:请看下面的代码:

function myFunction() {
  var document = DocumentApp.getActiveDocument();
  var body = document.getBody();

  var Style = {};
  Style[DocumentApp.Attribute.BOLD] = true;

  var found = body.findText("hello");

  while(found != null) {
    var foundText = found.getElement().asText();
    var start = found.getStartOffset();
    var end = found.getEndOffsetInclusive();
    foundText.setAttributes(start, end, Style)
    found = body.findText("hello", found);
  }
}

Running this resulted to this:运行这个结果是: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM