Google脚本文档将附加文字斜体化 [英] Google scripts docs italicize appended text

查看:111
本文介绍了Google脚本文档将附加文字斜体化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Google电子表格创建了一个具有重复模式的报告。我想让每一块文本的第一行斜体。



文本如下所示:


  Re:Blah Blah Blah 
Blah,blah blahblahblah;等等等等等等;等等等等等等。
[blah blah 10小时$ 250]


我希望从Re:到 \\\
的所有内容都被斜体化。该模式在整个文档中重复。



以下是我目前的内容:

  var bodyText = body。 editAsText(); 
var stuff =;如果(ParNormal [i] .indexOf(Re:)< 0){
bodyText.appendText((i = 0; i< ParNormal.length; i ++) ParNormal [I] + '\\\
')setItalic(真)。
} else {
bodyText.appendText(ParNormal [i] +'\\\
');


每个ParNormal数组项包含一行文本和文本块之间的空格也包含在数组中(在上例中, ParNormal [3] ==



我上面的代码不起作用。该文档似乎表明appendText返回一个文本元素。所以至少我希望所有的东西都是斜体的,如果不是我刚刚添加的文字。添加段落似乎是最简单的方法,但它会将回车放在每个段落的末尾,这会弄乱演示文稿。



ParNormal看起来像这样:

  [1] == Re:Blah Blah Blah
[2] ==Blah,blah blahblahblah; blah blah blah; blahblah blah。
[3] ==[blah blah 10 hours at 250 $]
[4] ==
[5] ==Re:Blah Blah
[ 6] ==blahblah; blah。
[7] ==[blah blah blah 20];
[8] ==



等等。

解决方案

这可能是您要找的。尽可能使用你的var名字。您可以快速推断出您需要的内容。

  function someFunction(){
var d = DocumentApp.getActiveDocument();
var body = d.getBody();
var parNormal = body.editAsText();
var index = -1;
var textFound = body.findText(Re:);
var text = textFound.getElement()。asText();

while(true)
{
index = parNormal.getText()。indexOf(Re:,index + 1);
if(index == -1)
break;
else {
parNormal.setItalic(index + 4,index + text.getText()。length-1,true);
}
}


I'm creating a report from a google spreadsheet that has a repeating pattern. I want to make the first line of each block of text italicized.

the text looks like this:

Re: Blah Blah Blah
Blah, blah blahblahblah; blah blah blah; blahblah blah.
[blah blah 10 hours at $250]

And I want everything from the Re: to the \n to be italicized. This pattern repeats throughout the document.

Here's what I currently have:

  var bodyText = body.editAsText();
  var stuff = "";
  for (i=0; i<ParNormal.length; i++){
    if (ParNormal[i].indexOf("Re: ")<0){
      bodyText.appendText(ParNormal[i]+'\n').setItalic(true);
    } else {
      bodyText.appendText(ParNormal[i]+'\n');
    }
  }

Each ParNormal array item contains a line of text and the spaces between the blocks of text are also contained inside the array (in the above example, ParNormal[3]==" ")

The code I have above doesn't work. The documentation seems to indicate that appendText returns a text element. So at the least I'd expect everything to be italicized, if not the text that I just appended. Appending paragraphs would seem to be the easiest way to do it, but it puts carriage returns at the end of each paragraph and that would mess up the presentation.

ParNormal would look like this:

[1]=="Re: Blah Blah Blah"
[2]=="Blah, blah blahblahblah; blah blah blah; blahblah blah."
[3]=="[blah blah 10 hours at $250]"
[4]==" "
[5]=="Re: Blah Blah"
[6]=="blahblah; blah."
[7]=="[blah blah blah 20]";
[8]==" "

et cetera.

解决方案

This might be what your looking for. Used your var names where I could. Hopfully you can extrapolate what you need.

function someFunction(){
  var d = DocumentApp.getActiveDocument();
  var body = d.getBody(); 
  var parNormal = body.editAsText();
  var index = -1;
  var textFound = body.findText("Re: ");
  var text = textFound.getElement().asText();

  while(true)
 {
   index = parNormal.getText().indexOf("Re: ",index+1);
   if(index == -1)
     break;
   else {
        parNormal.setItalic(index+4, index+text.getText().length-1, true);
   }
 }

这篇关于Google脚本文档将附加文字斜体化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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