Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get user-selected text

I want to select words or lines by mouse in a Google Doc, and by script, get these selected words or lines.

Example:

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

I tried, but I didn't find any methods for selection access like in VBA.

like image 770
Amany Avatar asked May 19 '13 20:05

Amany


People also ask

How do you highlight selected text in JavaScript?

getSelection() Method in JavaScript. The window. getSelection() method in JavaScript allows us to get the text highlighted or selected by the user on the screen. This method returns an object that contains information related to the text highlighted on the screen.

What is getSelection in JavaScript?

getSelection() method returns a Selection object representing the range of text selected by the user or the current position of the caret.

What is document getSelection?

getSelection() The getSelection() property of the Document interface returns a Selection object representing the range of text selected by the user, or the current position of the caret.


3 Answers

The ability to work with cursor position and selected text was added yesterday, addressing Issue 2865: Get current user location & state information in Document. See the blog post as well.

It turns out that there are some tricks to working with selections. I've tried to show them here - please add comments if you find any others, I'll gladly update.

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 );
}
like image 91
Mogsdad Avatar answered Oct 09 '22 11:10

Mogsdad


If you want to retrieve text you've highlighted, could 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 cursor position and selections is not yet available as of this writing.

UPDATE: Cursor selection is available now, see docs.

like image 26
Bryan P Avatar answered Oct 09 '22 11:10

Bryan P


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

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

I'm not familiar with VBA, but this will work to select text in a doc.

like image 37
rGil Avatar answered Oct 09 '22 12:10

rGil