Google Apps script - repeating words in a paragraph

So basically I have a paragraph with some underlined text, but not all text is underlined. I want to be able to repeat the selected text and resize the text of the underlined UN text.

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

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

+4
source share
2 answers

Here is some code that evaluates every word in a paragraph. He underlines every word in the third paragraph, which is not underlined. For example, the code receives the third paragraph. You will need to adjust the code according to your criteria. The code assumes that if the first letter of the word is underlined, the entire word is underlined. Each word has a bold font with a start and end 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, and the Script text class methods.

+3
source

The easiest way to do this is to iterate over each character in a paragraph.

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. , -, ...

+1

All Articles