Google Apps脚本 - 迭代段落中的单词 [英] Google Apps script- iterating through words in a paragraph

查看:108
本文介绍了Google Apps脚本 - 迭代段落中的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上我有一段文字用下划线标出,但并非所有文字都有下划线。我希望能够遍历所选文本并更改带下划线的文本的文本大小。

基本上我想让下划线的文字更加突出。是否有方法遍历该段落并检查每个单词是否加下划线? GAS中的文本元素有一个isUnderlined()函数,但这对我没有任何好处,因为我只知道如何获取整个元素。



感谢您的帮助提前!对不起,如果这是令人困惑。如果需要,我可以解释更多。

解决方案

以下是一些评估段落中每个单词的代码。它大胆地在第三段没有加下划线的每个字。作为一个例子,代码得到了第三段。您需要根据您的标准调整代码。该代码假定如果该单词的第一个字母带下划线,则整个单词带下划线。

 函数findAndBold(){
var allParagraphs,bodyElement ,endPosition,lengthOfThisWord,numberOfWordsInPara,
paragraphAsString,remainingTxtInParagraph,startPosition,text,theParagraph;

bodyElement = DocumentApp.getActiveDocument()。getBody();
allParagraphs = bodyElement.getParagraphs();

//按索引号E.g. 2获取第三段
theParagraph = allParagraphs [2];
//Logger.log(theParagraph:+ theParagraph);

//只修改可以编辑为文本的元素;跳过图像和其他
//非文本元素。
text = theParagraph.editAsText();
paragraphAsString = text.getText();
//Logger.log(\"paragraphAsString:+ paragraphAsString);

startPosition = 0;
endPosition = 0;
remainingTxtInParagraph = paragraphAsString;
lengthOfThisWord = 0;
numberOfWordsInPara = 0; //初始化为零值

while(remainingTxtInParagraph.length> 0){
Logger.log(remainingTxtInParagraph:+ remainingTxtInParagraph.length );
numberOfWordsInPara ++;

lengthOfThisWord = remainingTxtInParagraph.indexOf();
Logger.log(lengthOfThisWord:+ lengthOfThisWord);

if(lengthOfThisWord> -1){
endPosition = startPosition + lengthOfThisWord;
Logger.log(startPosition:+ startPosition);
Logger.log(endPosition:+ endPosition);
} else {
lengthOfThisWord = remainingTxtInParagraph.length;
Logger.log(lengthOfThisWord:+ lengthOfThisWord);
endPosition = startPosition + lengthOfThisWord - 1;
Logger.log(final end position:+ endPosition);
Logger.log(startPosition:+ startPosition);
};

remainingTxtInParagraph = remainingTxtInParagraph.substr(lengthOfThisWord +1); //长度被省略。提取字符到最后
Logger.log(remainingTxtInParagraph:+ remainingTxtInParagraph.length);

if(!text.isUnderline(startPosition)){
text.setBold(startPosition,endPosition,true);
};

startPosition = startPosition + lengthOfThisWord + 1;
Logger.log(next iteration startPosition:+ startPosition);
Logger.log();
};

Logger.log(numberOfWordsInPara:+ numberOfWordsInPara);
}

该代码使用JavaScript字符串方法,JavaScript字符串长度属性,以及Apps脚本文本类方法。


So basically I have a paragraph with some text underlined, but not all text is underlined. I want to be able to iterate through the selected text and change the text size of the UN-underlined text.

Essentially I want the underlined text to stick out more. Is there a way to iterate through the paragraph and check if each word is underlined? The text element in GAS has a isUnderlined() function but that doesn't do me any good since I only know how to grab the entire element.

Thanks for your help in advance! Sorry if this is confusing. I can explain more if needed.

解决方案

Here is some code that evaluates each word in a paragraph. It bolds every word in the third paragraph that is not underlined. As an example, the code is getting the 3rd paragraph. You will need to adjust the code to your criteria. The code assumes that if the first letter of the word is underlined, that the entire word is underlined. Each word is set to bold with a beginning and ending index.

function findAndBold() {
  var allParagraphs,bodyElement,endPosition,lengthOfThisWord ,numberOfWordsInPara ,
      paragraphAsString,remainingTxtInParagraph,startPosition,text ,theParagraph;

  bodyElement = DocumentApp.getActiveDocument().getBody();
  allParagraphs = bodyElement.getParagraphs();

  //Get a paragraph by index number  E.g. 2 Gets the third paragraph
  theParagraph = allParagraphs[2];
  //Logger.log("theParagraph: " + theParagraph);

  // Only modify elements that can be edited as text; skip images and other
  // non-text elements.
  text = theParagraph.editAsText();
  paragraphAsString = text.getText();
  //Logger.log("paragraphAsString: " + paragraphAsString);

  startPosition = 0;
  endPosition = 0;
  remainingTxtInParagraph = paragraphAsString;
  lengthOfThisWord = 0;
  numberOfWordsInPara = 0;//Initialize with a value of zero

  while (remainingTxtInParagraph.length > 0) {
    Logger.log("remainingTxtInParagraph: " + remainingTxtInParagraph.length);
    numberOfWordsInPara ++;

    lengthOfThisWord = remainingTxtInParagraph.indexOf(" ");
    Logger.log("lengthOfThisWord: " + lengthOfThisWord);

    if (lengthOfThisWord > -1) {
      endPosition = startPosition + lengthOfThisWord;
      Logger.log("startPosition: " + startPosition);
      Logger.log("endPosition: " + endPosition);
    } else {
      lengthOfThisWord = remainingTxtInParagraph.length;
      Logger.log("lengthOfThisWord: " + lengthOfThisWord);
      endPosition = startPosition + lengthOfThisWord - 1;
      Logger.log("final end position: " + endPosition);
      Logger.log("startPosition: " + startPosition);
    };

    remainingTxtInParagraph = remainingTxtInParagraph.substr(lengthOfThisWord + 1); //length is omitted.  Extracts characters to the end
    Logger.log("remainingTxtInParagraph: " + remainingTxtInParagraph.length);

    if (!text.isUnderline(startPosition)) {
      text.setBold(startPosition, endPosition, true);
    };

    startPosition = startPosition + lengthOfThisWord + 1;
    Logger.log("next iteration startPosition: " + startPosition);
    Logger.log(" ");
  };

  Logger.log("numberOfWordsInPara: " + numberOfWordsInPara);
}

The code uses a combination of JavaScript string methods, the JavaScript string length property, along with the Apps Script Text Class methods.

这篇关于Google Apps脚本 - 迭代段落中的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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