我可以用颜色使用谷歌Apps脚本在谷歌文档的某些词语?(Can I color certain wo

2019-06-17 19:41发布

我想强调我在谷歌文档的某些词。 我知道我可以使用document.replace替换文本,但它只能替换字符串本身,而不是格式化。 有没有一种方法来替代字符串使用谷歌Apps脚本有色字符串?

Answer 1:

这是一个更好的解决方案:

function highlightTextTwo() {
  var doc  = DocumentApp.openById('<your document id');
  var textToHighlight = 'dusty death';
  var highlightStyle = {};
  highlightStyle[DocumentApp.Attribute.FOREGROUND_COLOR] = '#FF0000';
  var paras = doc.getParagraphs();
  var textLocation = {};
  var i;

  for (i=0; i<paras.length; ++i) {
    textLocation = paras[i].findText(textToHighlight);
    if (textLocation != null && textLocation.getStartOffset() != -1) {
      textLocation.getElement().setAttributes(textLocation.getStartOffset(),textLocation.getEndOffsetInclusive(), highlightStyle);
    }
  }
}

以前的答案:

关键是要能够引用只要你想要的颜色的话。

我的解决办法是:

获取包含您想要的颜色的话段落的文字,删除原来的段落,然后添加文本的每个部分后面。 当您添加的每个部分回AppendText通过返回一个引用只是文本添加,然后你可以用setForegroundColor()指定其颜色:

function highlightText() {
  var doc = DocumentApp.openById('<your document id>');
  var textToHighlight = 'dusty death';
  var textLength = textToHighlight.length;
  var paras = doc.getParagraphs();
  var paraText = '';
  var start;
  for (var i=0; i<paras.length; ++i) {
    paraText = paras[i].getText();
    start = paraText.indexOf(textToHighlight);
    if (start >= 0) {
      var preText = paraText.substr(0, start);
      var text = paraText.substr(start, textLength);
      var postText = paraText.substr(start + textLength, paraText.length);
      doc.removeChild(paras[i]);
      var newPara = doc.insertParagraph(i, preText);
      newPara.appendText(text).setForegroundColor('#FF0000');
      newPara.appendText(postText).setForegroundColor('#000000');
    }
  }
}


Answer 2:

通过引入文档绑定脚本,它现在可能使这是一个从一个自定义菜单调用的文本高亮功能。

当然是最好的答案吧! 8 ^)

这个脚本是从一个在修改这个答案 ,并可以从UI(不带参数)或脚本调用。

/**
 * Find all matches of target text in current document, and highlight them.
 *
 * @param {String} target     (Optional) The text or regex to search for. 
 *                            See Body.findText() for details.
 * @param {String} background (Optional) The desired highlight color.
 *                            A default orange is provided.
 */
function highlightText(target,background) {
  // If no search parameter was provided, ask for one
  if (arguments.length == 0) {
    var ui = DocumentApp.getUi();
    var result = ui.prompt('Text Highlighter',
      'Enter text to highlight:', ui.ButtonSet.OK_CANCEL);
    // Exit if user hit Cancel.
    if (result.getSelectedButton() !== ui.Button.OK) return;
    // else
    target = result.getResponseText();
  }
  var background = background || '#F3E2A9';  // default color is light orangish.
  var doc = DocumentApp.getActiveDocument();
  var bodyElement = DocumentApp.getActiveDocument().getBody();
  var searchResult = bodyElement.findText(target);

  while (searchResult !== null) {
    var thisElement = searchResult.getElement();
    var thisElementText = thisElement.asText();

    //Logger.log(url);
    thisElementText.setBackgroundColor(searchResult.getStartOffset(), searchResult.getEndOffsetInclusive(),background);

    // search for next match
    searchResult = bodyElement.findText(target, searchResult);
  }
}

/**
 * Create custom menu when document is opened.
 */
function onOpen() {
  DocumentApp.getUi().createMenu('Custom')
      .addItem('Text Highlighter', 'highlightText')

      .addToUi();
}


Answer 3:

我认为这是可能的类文本的DocumentApp方法setBackgroundColor: https://developers.google.com/apps-script/class_text#setBackgroundColor

你必须找回你的话作为文本元素。 为了做到这一点,你可以用你的对象文件的查找方法,然后遍历搜索结果,并使用getElement 。 最后,你的元素对象转换为文本对象,你可以使用asText()

希望能对大家的工作! ;)



Answer 4:

这是作为一个谷歌文档附加名为多实例文本突出显示。 提示:起初,它似乎并没有工作,但我闭上我的文档,并重新打开它,然后它的工作。 然后,它似乎没有再到现在的工作,但是我发现在你的文本字符串可以打破它的特殊字符; 我觉得我在串有一个+,它只是没有做任何事情。 但是,如果没有特殊字符,它的伟大工程。 真的帮了我。



文章来源: Can I color certain words in Google Document using Google Apps Script?