Google script app Does the document app get selected lines or words?

I want to select words or lines with the mouse from a google document and script. I get the selected words or lines.

var doc=DocumentApp.getActiveDocument(); var docText = doc.editAsText(); var text=docText.getSelection(); 

I tried, but did not find methods to select, for example in VBA

+4
source share
4 answers

Added the ability to work with the cursor position and the selected text yesterday, referring to Problem 2865: Get the user's current location and status information in the document . See the blog post .

It turns out there are some tricks for working with elections. I tried to show them here - please add comments, if you find others, I will gladly update them.

 function onOpen() { DocumentApp.getUi().createMenu('Selection') .addItem("Report Selection", 'reportSelection' ) .addToUi(); } function reportSelection () { var doc = DocumentApp.getActiveDocument(); var selection = doc.getSelection(); var ui = DocumentApp.getUi(); var report = "Your Selection: "; if (!selection) { report += " No current selection "; } else { var elements = selection.getSelectedElements(); // Report # elements. For simplicity, assume elements are paragraphs report += " Paragraphs selected: " + elements.length + ". "; if (elements.length > 1) { } else { var element = elements[0].getElement(); var startOffset = elements[0].getStartOffset(); // -1 if whole element var endOffset = elements[0].getEndOffsetInclusive(); // -1 if whole element var selectedText = element.asText().getText(); // All text from element // Is only part of the element selected? if (elements[0].isPartial()) selectedText = selectedText.substring(startOffset,endOffset+1); // Google Doc UI "word selection" (double click) // selects trailing spaces - trim them selectedText = selectedText.trim(); endOffset = startOffset + selectedText.length - 1; // Now ready to hand off to format, setLinkUrl, etc. report += " Selected text is: '" + selectedText + "', "; report += " and is " + (elements[0].isPartial() ? "part" : "all") + " of the paragraph." } } ui.alert( report ); } 
+6
source

If you want to get selected text, you can try ...

 function findHighlighted() { var body = DocumentApp.getActiveDocument().getBody(), bodyTextElement = body.editAsText(), bodyString = bodyTextElement.getText(), char, len; for (char = 0, len = bodyString.length; char < len; char++) { if (bodyTextElement.getBackgroundColor(char) == '#ffff00') // Yellow Logger.log(bodyString.charAt(char))} } 

Derived from Jonathan's I / O example . However, note that working with the cursor position and selection is not yet available at the time of this writing.

UPDATE: A cursor is now available, see docs .

+3
source

You are close. I think you want the findText () method .

 var text = docText.findText("some string of text in the document") // for example 

I am not familiar with VBA, but this will work to select the text in the document.

+2
source

To add to Brian’s answer, I wrote this to convert individual characters to return an array of words or phrases that were highlighted.

  function findHighlighted() { var results = []; var body = DocumentApp.getActiveDocument().getBody(), bodyTextElement = body.editAsText(), bodyString = bodyTextElement.getText(), char, len; for (char = 0, len = bodyString.length; char < len; char++) { if (bodyTextElement.getBackgroundColor(char) !== null) results.push([char, bodyString.charAt(char)]); } return results; } function getWords() { var arr = findHighlighted(); var wordList = []; var holding = []; var nextNum, sum; for (var i = 0; i < arr.length; i++) { if (arr[i + 1] === undefined) { nextNum = 0; } else { nextNum = arr[i + 1][0]; } sum = (Number(arr[i][0]) + 1); if (nextNum === sum) { holding.push(arr[i][1]); } else { holding.push(arr[i][1]); wordList.push(holding.join("")); holding = []; } } Logger.log(wordList); } 
0
source

Source: https://habr.com/ru/post/1481661/


All Articles