javascript - 遍历段落中的单词

标签 javascript google-apps-script google-docs

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

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

最佳答案

下面是一些代码,用于评估段落中的每个单词。它将第三段中没有下划线的每个单词加粗。例如,代码获取第 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 脚本文本类方法。

关于javascript - 遍历段落中的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27971423/

相关文章:

javascript - PHP数组转JS数组并重构

javascript - 将 HTML 对象添加到事件 javascript/meteor

javascript - 如何释放 "busy"c# webbrowser

google-apps-script - CalendarApp.getCalendarById 返回 null

javascript - 将 URL 参数传递给 Web App 脚本

javascript - Angular 2 multi-provider 用于导出依赖项的依赖项

google-apps-script - GAS 中的脚本和自定义函数有什么区别?

javascript - 如何测量 Google 文档中的单词/插入符位置?

google-analytics - 有人用过 Google 表格的 Google Analytics 插件吗?是否可以安排数据运行的频率高于每小时一次?

google-apps-script - 如何使用谷歌应用程序脚本阅读谷歌文档中的标题引用