Google Apps脚本 - 迭代段落中的字词

时间:2015-01-15 19:26:52

标签: javascript google-apps-script google-docs

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

基本上我希望带下划线的文字更加突出。有没有办法迭代段落并检查每个单词是否加下划线? GAS中的文本元素有一个isUnderlined()函数,但这对我没什么好处,因为我只知道如何抓取整个元素。

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

2 个答案:

答案 0 :(得分:3)

以下是一些评估段落中每个单词的代码。它使第三段中没有加下划线的每个单词都加粗。例如,代码获得第3段。您需要根据标准调整代码。该代码假定如果单词的第一个字母带下划线,则整个单词都带有下划线。每个单词都设置为粗体,带有开头和结尾索引。

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

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

答案 1 :(得分:1)

最简单的方法是迭代段落中的每个字符。

function notUnder() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var paragraph = body.getChild(0); //Analyze the first paragraph in document
  var txt = paragraph.asParagraph().getChild(0); //assumes the paragraph only has text
  var len = txt.asText().getText().length;
  for (var i = 0; i<len; i++) {
    var isUnder = txt.asText().isUnderline(i);
    if (!isUnder) {
      txt.asText().setFontSize(i,i,10); //sets the character to font size 10 if is not underlined
    };
  };
};

这将使所有未加下划线的字符的字体大小为10.如果一个单词只有一个带下划线的字符,则单词中的所有其他字符的大小为10.这可以用来表现不同,但是起点......