【问题标题】:Iterating through words in a paragraph遍历段落中的单词
【发布时间】:2015-01-15 19:26:52
【问题描述】:

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

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

【问题讨论】:

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


【解决方案1】:

这是一些评估段落中每个单词的代码。它将第三段中没有下划线的每个单词加粗。例如,代码正在获取第 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 Script Text Class 方法的组合。

【讨论】:

    【解决方案2】:

    最简单的方法是遍历段落中的每个字符。

    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。这可以改变行为,但它是起点……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多