简体   繁体   English

在 Google Docs 中创建彩虹文本

[英]Creating Rainbow Text in Google Docs

var selectedElements = selection.getSelectedElements();
for (var i = 0; i < selectedElements.length; ++i) {
var selectedElement = selectedElements[i];

// Only modify elements that can be edited as text; skip images and other
// non-text elements.
var text = selectedElement.getElement().editAsText();

// Change the background color of the selected part of the element, or the
// full element if it's completely selected.
if (selectedElement.isPartial()) {
  text.setColor(selectedElement.getStartOffset(),
      selectedElement.getEndOffsetInclusive(), '#69359c');
    }
  }
}

The above text takes a selection inside of a Google Doc and changes it to the hex code #69359c (a dark purple).上面的文本在 Google Doc 中进行了选择,并将其更改为十六进制代码 #69359c(深紫色)。 I have searched many websites, many gits, and asked many friends for help with my project.我搜索了很多网站,很多gits,也向很多朋友寻求我的项目帮助。

My end project is this:我的最终项目是这样的:

  1. Create a menu for Google Docs with my selector (DONE)使用我的选择器为 Google 文档创建菜单(完成)
  2. Be able to highlight a certain amount of text and change it to an array of colors (ROY G. BIV / the rainbow).能够突出显示一定数量的文本并将其更改为一系列颜色(ROY G. BIV / 彩虹)。
  3. Have the format be only for Google Documents.格式仅适用于 Google 文档。

If anyone can help me it would be highly appreciated.如果有人可以帮助我,将不胜感激。

I just found this question and am happy to provide some working code from my Rainbow Font Google Docs add-on ( Magic Rainbow Unicorns ).我刚刚发现了这个问题,很高兴从我的 Rainbow Font Google Docs 插件( Magic Rainbow Unicorns )中提供一些工作代码。

The first problem is that you need to set the foreground color on the text, and the second is that the code above only allows for partial paragraph selections.第一个问题是你需要在文本上设置前景色,第二个问题是上面的代码只允许部分段落选择。

For whole selections use this code:对于整个选择,请使用以下代码:

var elementText = element.editAsText();
if (elementText) {
  var paragraph = elementText.getText();
  for (var j = 0; j < paragraph.length; j++) {
    elementText.setForegroundColor(j, j, getNextRainbowColour(...));
  }
}

For partial selections, I used this:对于部分选择,我使用了这个:

var elementText = element.asText();
var startIndex = element.getStartOffset();
var endIndex = elements.getEndOffsetInclusive();
for (var j = startIndex; j < endIndex+1; j++) {
  elementText.setForegroundColor(j, j, getNextRainbowColour(...));
}

You are pretty close to the answer already.你已经非常接近答案了。 Try iterating over the elements within your 'text' variable, so you can change the background on each one.尝试迭代“text”变量中的元素,以便您可以更改每个元素的背景。

You could use something like this to iterate over each letter:你可以使用这样的东西来迭代每个字母:

var letters =  elementText.getText();
for(var j = 0 ; j< letters.length-1; j++)
{
     elementText.setBackgroundColor(j, j+1, getRandomColor())
}

Here is a sample of a function to use different colors:以下是使用不同颜色的函数示例:

function getRandomColor() {
   var letters = '0123456789ABCDEF'.split('');
   var color = '#';
   for (var i = 0; i < 6; i++ ) 
   {
       color += letters[Math.floor(Math.random() * 16)];
   }
   return color;
}

For your last question, since the Text class is not part of Javascript, but from app-script library, this will not work outside Google environment.对于您的最后一个问题,由于 Text 类不是 Javascript 的一部分,而是来自 app-script 库,因此这在 Google 环境之外不起作用。

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

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