google doc脚本,搜索并替换文本字符串并更改字体(例如,粗体字) [英] google doc script, search and replace text string and change font (e.g., boldface)

查看:160
本文介绍了google doc脚本,搜索并替换文本字符串并更改字体(例如,粗体字)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Google文档脚本的新手.

i am new to google doc script.

在Google文档中,我需要搜索几个文本字符串(例如,以亮面字体显示的学生1"),以将这些文本字符串替换为另一个文本字符串(例如,"学生A ") ),但使用粗体字体.

in a google doc, i need to search for several text strings (e.g., "student 1" in lightface font) to replace these text strings with another text string (e.g., "Student A"), but in boldface font.

要搜索和替换,我使用以下代码:

to search and replace, i use the following code:

function docReplace() {

  var body = DocumentApp.getActiveDocument().getBody();
  // change "student 1" to "Student A" in boldface
  body.replaceText("student 1", "Student A");

}

以上代码仅使用Google文档的当前字体将学生1"替换为学生A",但我不知道如何将字体从lightface更改为粗体.

the above code only replaces "student 1" with "Student A" using the current font of google doc, but i don't know how to change the font from lightface to boldface.

我尝试过

body.replaceText("student 1", "<b>Student A</b>");

当然,上面的代码不起作用.

of course, the above code did not work.

任何帮助将不胜感激.谢谢.

any help would be much appreciated. thank you.

推荐答案

一种行人方式,用新的文本字符串(例如,"学生A ")以粗体显示,分为两个步骤:

a pedestrian way to replace a text string (e.g., "student 1") that has many occurrences in a google doc by a new text string (e.g., "Student A") in boldface, is two steps:

1-编写一个函数(称为docReplace)进行搜索并替换为常规/常规字体(无粗体):

1- write a function (called, say, docReplace) to do a search and replace in regular / normal font (no boldface):

function docReplace() {

  var body = DocumentApp.getActiveDocument().getBody();
  // change "student 1" to "Student A"
  body.replaceText("student 1", "Student A");

}

2-编写一个函数(例如,boldfaceText)以在每次出现的情况下搜索所需的文本(例如学生A")和该文本的两个偏移值(即startOffset和endOffsetInclusive).将这些偏移值内的字符的字体设置为粗体:

2- write a function (called, say, boldfaceText) to do a search for the desired text (e.g., "Student A") and the two offset values for this text (i.e., startOffset and endOffsetInclusive) at each occurrence to set the font for the characters within these offset values to boldface:

function boldfaceText(findMe) {

  // put to boldface the argument
  var body = DocumentApp.getActiveDocument().getBody();
  var foundElement = body.findText(findMe);

  while (foundElement != null) {
    // Get the text object from the element
    var foundText = foundElement.getElement().asText();

    // Where in the Element is the found text?
    var start = foundElement.getStartOffset();
    var end = foundElement.getEndOffsetInclusive();

    // Change the background color to yellow
    foundText.setBold(start, end, true);

    // Find the next match
    foundElement = body.findText(findMe, foundElement);
  }

}

上述boldfaceText代码的灵感来自帖子查找文本(多次) )并突出显示.

the above code for boldfaceText was inspired from that in the post Finding text (multiple times) and highlighting.

一个字符的偏移值只是描述该字符在文档中位置的整数,第一个字符的偏移值为1(就像字符的坐标).

an offset value for a character is simply the integer that describes the location of that character in the document, with the very first character having the offset value 1 (it's like the coordinate of the character).

使用学生A"作为对函数boldfaceText(即

use "Student A" as argument for a call to the function boldfaceText, i.e.,

boldfaceText("Student A");

可以嵌入到docReplace函数中,即

which could be embedded into the function docReplace, i.e.,

function docReplace() {

  var body = DocumentApp.getActiveDocument().getBody();
  // change "student 1" to "Student A"
  body.replaceText("student 1", "Student A");

  // set all occurrences of "Student A" to boldface
  boldfaceText("Student A");

}

在Google文档中,只需运行脚本docReplace即可将所有出现的学生1"更改为黑体字的"学生A ".

in the google doc, simply run the script docReplace to change all occurrences of "student 1" into "Student A" in boldface.

以上两个函数(docReplace和boldfaceText)可能是向Google doc脚本介绍新手(如我)的好方法.经过一段时间与Google Doc脚本一起使用以获得一定的熟悉度之后,请学习Robin更优雅,更高级的代码,该代码可以一次完成以上两个步骤.

the above two functions (docReplace and boldfaceText) could be a good way to introduce newbies (like me) to google doc scripts. after some time playing with google doc scripts to gain some familiarity, learn Robin's more elegant and advanced code that does the above two steps all at once.

这篇关于google doc脚本,搜索并替换文本字符串并更改字体(例如,粗体字)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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