Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
331 views
in Technique[技术] by (71.8m points)

javascript - Iterating through words in a paragraph

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...