我可以使用Google Apps脚本在Google文档中为某些字词添加颜色吗? [英] Can I color certain words in Google Document using Google Apps Script?

查看:107
本文介绍了我可以使用Google Apps脚本在Google文档中为某些字词添加颜色吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图突出显示Google文档中的某些字词。我知道我可以使用document.replace替换文本,但它只替换字符串本身,而不是格式。有没有一种方法可以使用Google Apps脚本来替换带有彩色字符串的字符串?

解决方案

这是更好的解决方案:


$ b $

 函数highlightTextTwo(){
var doc = DocumentApp.openById('< your document id');
var textToHighlight ='尘土飞逝';
var highlightStyle = {};
highlightStyle [DocumentApp.Attribute.FOREGROUND_COLOR] ='#FF0000';
var paras = doc.getParagraphs();
var textLocation = {};
var i;

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


$ b 关键是能够引用您想要着色的单词。



我的解决方案是:



获取包含您想要着色的单词的段落文本,删除原始段落,然后将文本的每个部分添加回来。在你添加每个部分的时候,appendText会返回一个只添加文本的引用,然后你可以用setForegroundColor()指定它的颜色:

  function highlightText(){
var doc = DocumentApp.openById('< your document id>');
var textToHighlight ='尘土飞逝';
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(第[i]段);
var newPara = doc.insertParagraph(i,preText);
newPara.appendText(text).setForegroundColor('#FF0000');
newPara.appendText(postText).setForegroundColor('#000000');
}
}
}


I'm trying to highlight certain words in my Google Document. I know I can replace text using document.replace, but it only replaces string itself, not formatting. Is there a way to replace string with colored string using Google Apps Script?

解决方案

This is a better solution:

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

Previous Answer:

The key is to being able to reference just the words you want to color.

My solution is to:

Get the text of the paragraph that contains the words you wish to color, remove the original paragraph, then add each part of the text back. As you add each part back the appendText returns a reference to just the text added, you then can specify its color with 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');
    }
  }
}

这篇关于我可以使用Google Apps脚本在Google文档中为某些字词添加颜色吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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